inspection: use ProfilesComboBox as analyze action combobox
authorDmitry Batkovich <dmitry.batkovich@jetbrains.com>
Tue, 7 Jun 2016 11:18:40 +0000 (14:18 +0300)
committerDmitry Batkovich <dmitry.batkovich@jetbrains.com>
Tue, 7 Jun 2016 12:31:43 +0000 (15:31 +0300)
platform/lang-impl/src/com/intellij/codeInspection/actions/CodeCleanupAction.java
platform/lang-impl/src/com/intellij/codeInspection/actions/CodeInspectionAction.java
platform/lang-impl/src/com/intellij/codeInspection/actions/CodeInspectionProfileChooserForm.form
platform/lang-impl/src/com/intellij/profile/codeInspection/ui/header/ProfilesComboBox.java

index 6d0d52bb2139510a4ef553c2aba8544bfc1845ae..5d802127efa488b9f7ed665357c9229d3b829e53 100644 (file)
@@ -25,8 +25,7 @@ import com.intellij.openapi.project.Project;
 import com.intellij.profile.codeInspection.InspectionProfileManager;
 import com.intellij.profile.codeInspection.InspectionProjectProfileManager;
 import com.intellij.profile.codeInspection.ui.header.InspectionToolsConfigurable;
-
-import javax.swing.*;
+import com.intellij.profile.codeInspection.ui.header.ProfilesComboBox;
 
 public class CodeCleanupAction extends CodeInspectionAction {
 
@@ -52,7 +51,7 @@ public class CodeCleanupAction extends CodeInspectionAction {
   @Override
   protected InspectionToolsConfigurable createConfigurable(InspectionProjectProfileManager projectProfileManager,
                                                            InspectionProfileManager profileManager,
-                                                           JComboBox profilesCombo) {
+                                                           ProfilesComboBox profilesCombo) {
     return new ExternalProfilesComboboxAwareInspectionToolsConfigurable(projectProfileManager, profileManager, profilesCombo) {
       @Override
       protected boolean acceptTool(InspectionToolWrapper entry) {
index 5c11f4b8c3e8a212e4524f13e35f94af7348881c..f0e3798e8166f1ffd74ac7cef1c8c39ace7d59ad 100644 (file)
@@ -25,16 +25,15 @@ import com.intellij.codeInspection.InspectionsBundle;
 import com.intellij.codeInspection.ex.GlobalInspectionContextImpl;
 import com.intellij.codeInspection.ex.InspectionManagerEx;
 import com.intellij.codeInspection.ex.InspectionProfileImpl;
-import com.intellij.icons.AllIcons;
 import com.intellij.openapi.fileEditor.FileDocumentManager;
 import com.intellij.openapi.options.ex.SingleConfigurableEditor;
 import com.intellij.openapi.project.Project;
 import com.intellij.profile.Profile;
-import com.intellij.profile.ProfileManager;
 import com.intellij.profile.codeInspection.InspectionProfileManager;
 import com.intellij.profile.codeInspection.InspectionProjectProfileManager;
 import com.intellij.profile.codeInspection.ui.ErrorsConfigurable;
 import com.intellij.profile.codeInspection.ui.header.InspectionToolsConfigurable;
+import com.intellij.profile.codeInspection.ui.header.ProfilesComboBox;
 import com.intellij.ui.ComboboxWithBrowseButton;
 import com.intellij.ui.ListCellRendererWrapper;
 import org.jetbrains.annotations.NonNls;
@@ -43,8 +42,8 @@ import org.jetbrains.annotations.NotNull;
 import javax.swing.*;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
-import java.util.Collection;
-import java.util.TreeSet;
+import java.util.ArrayList;
+import java.util.List;
 
 public class CodeInspectionAction extends BaseAnalysisAction {
   private GlobalInspectionContextImpl myGlobalInspectionContext;
@@ -102,17 +101,7 @@ public class CodeInspectionAction extends BaseAnalysisAction {
   protected JComponent getAdditionalActionSettings(@NotNull final Project project, final BaseAnalysisActionDialog dialog) {
     final AdditionalPanel panel = new AdditionalPanel();
     final InspectionManagerEx manager = (InspectionManagerEx)InspectionManager.getInstance(project);
-    final JComboBox profiles = panel.myBrowseProfilesCombo.getComboBox();
-    profiles.setRenderer(new ListCellRendererWrapper() {
-      @Override
-      public void customize(JList list, Object value, int index, boolean selected, boolean hasFocus) {
-        if (value instanceof Profile) {
-          Profile profile = (Profile)value;
-          setText(profile.getName());
-          setIcon(profile.isProjectLevel() ? AllIcons.General.ProjectSettings : AllIcons.General.Settings);
-        }
-      }
-    });
+    final ProfilesComboBox profiles = (ProfilesComboBox)panel.myBrowseProfilesCombo.getComboBox();
     final InspectionProfileManager profileManager = InspectionProfileManager.getInstance();
     final InspectionProjectProfileManager projectProfileManager = InspectionProjectProfileManager.getInstance(project);
     reloadProfiles(profiles, profileManager, projectProfileManager, manager);
@@ -150,16 +139,16 @@ public class CodeInspectionAction extends BaseAnalysisAction {
 
   protected InspectionToolsConfigurable createConfigurable(InspectionProjectProfileManager projectProfileManager,
                                                            InspectionProfileManager profileManager,
-                                                           final JComboBox profilesCombo) {
+                                                           final ProfilesComboBox profilesCombo) {
     return new ExternalProfilesComboboxAwareInspectionToolsConfigurable(projectProfileManager, profileManager, profilesCombo);
   }
 
   protected static class ExternalProfilesComboboxAwareInspectionToolsConfigurable extends InspectionToolsConfigurable {
-    private final JComboBox myProfilesCombo;
+    private final ProfilesComboBox myProfilesCombo;
 
     public ExternalProfilesComboboxAwareInspectionToolsConfigurable(@NotNull InspectionProjectProfileManager projectProfileManager,
                                                                     InspectionProfileManager profileManager,
-                                                                    JComboBox profilesCombo) {
+                                                                    ProfilesComboBox profilesCombo) {
       super(projectProfileManager, profileManager);
       myProfilesCombo = profilesCombo;
     }
@@ -172,13 +161,13 @@ public class CodeInspectionAction extends BaseAnalysisAction {
     @Override
     protected void addProfile(InspectionProfileImpl model, InspectionProfileImpl profile) {
       super.addProfile(model, profile);
-      ((DefaultComboBoxModel)myProfilesCombo.getModel()).addElement(model);
+      myProfilesCombo.addProfile(profile);
     }
 
     @Override
     protected void applyRootProfile(@NotNull String name, boolean isProjectLevel) {
       for (int i = 0; i < myProfilesCombo.getItemCount(); i++) {
-        final InspectionProfileImpl profile = (InspectionProfileImpl)myProfilesCombo.getItemAt(i);
+        final InspectionProfileImpl profile = myProfilesCombo.getItemAt(i);
         if (name.equals(profile.getName())) {
           myProfilesCombo.setSelectedIndex(i);
           break;
@@ -188,29 +177,42 @@ public class CodeInspectionAction extends BaseAnalysisAction {
   }
 
 
-  private void reloadProfiles(JComboBox profiles,
+  private void reloadProfiles(ProfilesComboBox profilesCombo,
                               InspectionProfileManager inspectionProfileManager,
                               InspectionProjectProfileManager inspectionProjectProfileManager,
                               InspectionManagerEx inspectionManager) {
     final InspectionProfile selectedProfile = getGlobalInspectionContext(inspectionManager.getProject()).getCurrentProfile();
-    final DefaultComboBoxModel model = (DefaultComboBoxModel)profiles.getModel();
-    model.removeAllElements();
-    fillModel(inspectionProfileManager, model);
-    fillModel(inspectionProjectProfileManager, model);
-    profiles.setSelectedItem(selectedProfile);
-  }
-
-  private static void fillModel(final ProfileManager inspectionProfileManager, final DefaultComboBoxModel model) {
-    Collection<Profile> profiles = new TreeSet<Profile>(inspectionProfileManager.getProfiles());
-    for (Profile profile : profiles) {
-      model.addElement(profile);
-    }
+    List<Profile> profiles = new ArrayList<>();
+    profiles.addAll(inspectionProfileManager.getProfiles());
+    profiles.addAll(inspectionProjectProfileManager.getProfiles());
+    profilesCombo.reset(profiles);
+    profilesCombo.selectProfile((InspectionProfileImpl)selectedProfile);
   }
 
 
   private static class AdditionalPanel {
     public ComboboxWithBrowseButton myBrowseProfilesCombo;
     public JPanel myAdditionalPanel;
+
+    private void createUIComponents() {
+      myBrowseProfilesCombo = new ComboboxWithBrowseButton(new ProfilesComboBox() {
+        @Override
+        protected boolean isProjectLevel(InspectionProfileImpl p) {
+          return p.isProjectLevel();
+        }
+
+        @NotNull
+        @Override
+        protected String getProfileName(InspectionProfileImpl p) {
+          return p.getDisplayName();
+        }
+
+        @Override
+        protected void onProfileChosen(InspectionProfileImpl inspectionProfile) {
+          //do nothing here
+        }
+      });
+    }
   }
 
   private static class MySingleConfigurableEditor extends SingleConfigurableEditor {
index 8afb79a93d36e6a2403cd31e8a8e4a3631f8fe54..7ac00e57cd51403694a7a4938e65bc702c996848 100644 (file)
@@ -19,7 +19,7 @@
         </clientProperties>
         <border type="none" title-resource-bundle="messages/InspectionsBundle" title-key="inspection.action.profile.label"/>
         <children>
-          <component id="aba18" class="com.intellij.ui.ComboboxWithBrowseButton" binding="myBrowseProfilesCombo">
+          <component id="aba18" class="com.intellij.ui.ComboboxWithBrowseButton" binding="myBrowseProfilesCombo" custom-create="true">
             <constraints>
               <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
             </constraints>
index e8eb1c062cea117a1384eb440a38e189ae501f89..09fae08bbd512f93b5718aadcf48f875ba0e2280 100644 (file)
@@ -138,7 +138,7 @@ public abstract class ProfilesComboBox extends ComboBox<InspectionProfileImpl> {
     }
   }
 
-  void addProfile(InspectionProfileImpl inspectionProfile) {
+  public void addProfile(InspectionProfileImpl inspectionProfile) {
     myComboModel.add(inspectionProfile);
     if (!isProjectLevel(inspectionProfile)) {
       findFirstGlobalProfile();