package com.intellij.codeInspection.actions;

import com.intellij.CommonBundle;
import com.intellij.analysis.AnalysisScope;
import com.intellij.analysis.BaseAnalysisAction;
import com.intellij.analysis.BaseAnalysisActionDialog;
import com.intellij.codeInspection.InspectionManager;
import com.intellij.codeInspection.InspectionsBundle;
import com.intellij.codeInspection.ex.InspectionManagerEx;
import com.intellij.codeInspection.ex.InspectionProfileImpl;
import com.intellij.codeInspection.ui.InspectCodePanel;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.LabeledComponent;
import com.intellij.profile.codeInspection.InspectionProjectProfileManager;
import com.intellij.ui.ComboboxWithBrowseButton;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CodeInspectionAction extends BaseAnalysisAction {
  public CodeInspectionAction() {
    super(InspectionsBundle.message("inspection.action.title"), InspectionsBundle.message("inspection.action.noun"));
  }

  protected void analyze(Project project, AnalysisScope scope) {
    FileDocumentManager.getInstance().saveAllDocuments();
    final InspectionManagerEx inspectionManagerEx = ((InspectionManagerEx)InspectionManager.getInstance(project));
    inspectionManagerEx.setCurrentScope(scope);
    inspectionManagerEx.doInspections(scope);
  }

  protected JComponent getAdditionalActionSettings(final Project project, final BaseAnalysisActionDialog dialog) {
    final InspectionManagerEx manager = (InspectionManagerEx)InspectionManager.getInstance(project);
    LabeledComponent component = new LabeledComponent();
    component.setText(InspectionsBundle.message("inspection.action.profile.label"));
    component.setLabelLocation(BorderLayout.WEST);
    ComboboxWithBrowseButton comboboxWithBrowseButton = new ComboboxWithBrowseButton();
    component.setComponent(comboboxWithBrowseButton);
    final JComboBox profiles = comboboxWithBrowseButton.getComboBox();
    final InspectionProjectProfileManager projectProfileManager = InspectionProjectProfileManager.getInstance(project);
    reloadProfiles(profiles, projectProfileManager, manager);
    comboboxWithBrowseButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        InspectCodePanel inspectCodeDialog = new InspectCodePanel(manager, null, (String)profiles.getSelectedItem()){
          protected void init() {
            super.init();
            setOKButtonText(CommonBundle.getOkButtonText());
          }
        };
        inspectCodeDialog.show();
        if (inspectCodeDialog.isOK()){
          reloadProfiles(profiles, projectProfileManager, manager);
        } else {
          //if profile was disabled and cancel after apply was pressed
          final InspectionProfileImpl profile = (InspectionProfileImpl)projectProfileManager.getProfile((String)profiles.getSelectedItem());
          final boolean canExecute = profile != null && profile.isExecutable();
          dialog.setOKActionEnabled(canExecute);
        }
      }
    });
    profiles.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        final InspectionProfileImpl profile = (InspectionProfileImpl)projectProfileManager.getProfile((String)profiles.getSelectedItem());
        final boolean canExecute = profile != null && profile.isExecutable();
        dialog.setOKActionEnabled(canExecute);
        if (canExecute){
          manager.setProfile(profile);
        }
      }
    });
    final InspectionProfileImpl profile = (InspectionProfileImpl)projectProfileManager.getProfile((String)profiles.getSelectedItem());
    dialog.setOKActionEnabled(profile != null && profile.isExecutable());
    JPanel panel = new JPanel(new BorderLayout());
    panel.add(component, BorderLayout.NORTH);
    return panel;
  }

  private void reloadProfiles(JComboBox profiles, InspectionProjectProfileManager inspectionProjectProfileManager, InspectionManagerEx inspectionManager){
    final String selectedProfile = inspectionManager.getCurrentProfile().getName();
    final String[] avaliableProfileNames = inspectionProjectProfileManager.getAvailableProfileNames();
    final DefaultComboBoxModel model = (DefaultComboBoxModel)profiles.getModel();
    model.removeAllElements();
    for (String profile : avaliableProfileNames) {
      model.addElement(profile);
    }
    profiles.setSelectedItem(selectedProfile);
  }
}

@@ -707,39 +707,32 @@
 on.InspectionPro
-jectPro
 fileManager;%0Aimp
@@ -720,32 +720,32 @@
 ProfileManager;%0A
+
 import com.intel
@@ -2087,39 +2087,32 @@
 al InspectionPro
-jectPro
 fileManager proj
@@ -2106,31 +2106,24 @@
 eManager pro
-jectPro
 fileManager 
@@ -2129,39 +2129,32 @@
  = InspectionPro
-jectPro
 fileManager.getI
@@ -2157,31 +2157,24 @@
 getInstance(
-project
 );%0A    reloa
@@ -2188,39 +2188,32 @@
 es(profiles, pro
-jectPro
 fileManager, man
@@ -2713,23 +2713,16 @@
 les, pro
-jectPro
 fileMana
@@ -2890,39 +2890,32 @@
 nProfileImpl)pro
-jectPro
 fileManager.getP
@@ -3282,39 +3282,32 @@
 nProfileImpl)pro
-jectPro
 fileManager.getP
@@ -3633,23 +3633,16 @@
 Impl)pro
-jectPro
 fileMana
@@ -3942,39 +3942,32 @@
 s, InspectionPro
-jectPro
 fileManager insp
@@ -3967,39 +3967,32 @@
 er inspectionPro
-jectPro
 fileManager, Ins
@@ -4108,24 +4108,24 @@
 .getName();%0A
+
     final St
@@ -4172,15 +4172,8 @@
 nPro
-jectPro
 file

fix storing settings