diff --git a/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/header/InspectionToolsConfigurable.java b/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/header/InspectionToolsConfigurable.java index c0a01e0a94a5..705f36e32456 100644 --- a/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/header/InspectionToolsConfigurable.java +++ b/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/header/InspectionToolsConfigurable.java @@ -152,7 +152,7 @@ public abstract class InspectionToolsConfigurable extends BaseConfigurable final SingleInspectionProfilePanel panel = createPanel(model, profile, modelName); myPanel.add(getCardName(model), panel); - myProfiles.getModel().addElement(model); + myProfiles.addProfile(model); putProfile(model, panel); myProfiles.selectProfile(model); } @@ -216,13 +216,15 @@ public abstract class InspectionToolsConfigurable extends BaseConfigurable } }); - myProfiles = new ProfilesConfigurableComboBox(new ListCellRendererWrapper() { + myProfiles = new ProfilesConfigurableComboBox(new ListCellRendererWrapper() { @Override - public void customize(final JList list, final Profile value, final int index, final boolean selected, final boolean hasFocus) { + public void customize(final JList list, final InspectionProfileImpl value, final int index, final boolean selected, final boolean hasFocus) { final SingleInspectionProfilePanel singleInspectionProfilePanel = getProfilePanel(value); if (singleInspectionProfilePanel != null) { - final boolean isShared = singleInspectionProfilePanel.isProjectLevel(); - setIcon(isShared ? AllIcons.General.ProjectSettings : AllIcons.General.Settings); + if (index == -1) { + final boolean isShared = singleInspectionProfilePanel.isProjectLevel(); + setIcon(isShared ? AllIcons.General.ProjectSettings : AllIcons.General.Settings); + } setText(singleInspectionProfilePanel.getCurrentProfileName()); } } @@ -232,6 +234,24 @@ public abstract class InspectionToolsConfigurable extends BaseConfigurable myLayout.show(myPanel, getCardName(inspectionProfile)); myAuxiliaryRightPanel.showDescription(inspectionProfile.getDescription()); } + + @Override + protected int compare(@NotNull InspectionProfileImpl p1, @NotNull InspectionProfileImpl p2) { + SingleInspectionProfilePanel panel1 = getProfilePanel(p1); + SingleInspectionProfilePanel panel2 = getProfilePanel(p2); + if (panel1.isProjectLevel() && !p2.isProjectLevel()) { + return -1; + } + if (panel2.isProjectLevel() && !p1.isProjectLevel()) { + return 1; + } + return panel1.getCurrentProfileName().compareTo(panel2.getCurrentProfileName()); + } + + @Override + protected boolean isProjectLevel(InspectionProfileImpl profile) { + return getProfilePanel(profile).isProjectLevel(); + } }; JPanel profilesHolder = new JPanel(); profilesHolder.setLayout(new CardLayout()); @@ -324,7 +344,7 @@ public abstract class InspectionToolsConfigurable extends BaseConfigurable @Override public void delete() { final InspectionProfileImpl selectedProfile = myProfiles.getSelectedProfile(); - myProfiles.getModel().removeElement(selectedProfile); + myProfiles.removeProfile(selectedProfile); myDeletedProfiles.add(selectedProfile); } @@ -573,12 +593,12 @@ public abstract class InspectionToolsConfigurable extends BaseConfigurable myDeletedProfiles.clear(); disposeUIResources(); final Collection profiles = getProfiles(); - final List modifiableProfiles = new ArrayList(profiles.size()); + final List modifiableProfiles = new ArrayList<>(profiles.size()); for (Profile profile : profiles) { final String profileName = profile.getName(); final ModifiableModel modifiableProfile = ((InspectionProfileImpl)profile).getModifiableModel(); - modifiableProfiles.add(modifiableProfile); final InspectionProfileImpl inspectionProfile = (InspectionProfileImpl)modifiableProfile; + modifiableProfiles.add(inspectionProfile); final SingleInspectionProfilePanel panel = createPanel(inspectionProfile, profile, profileName); putProfile(modifiableProfile, panel); myPanel.add(getCardName(inspectionProfile), panel); @@ -621,9 +641,7 @@ public abstract class InspectionToolsConfigurable extends BaseConfigurable boolean projectProfileFound = false; boolean ideProfileFound = false; - final ComboBoxModel model = myProfiles.getModel(); - for (int i = 0; i < model.getSize(); i++) { - Profile profile = (Profile)model.getElementAt(i); + for (InspectionProfileImpl profile : myProfiles.getProfiles()) { if (inspectionProfile == profile) continue; final boolean isProjectProfile = profile.getProfileManager() == myProjectProfileManager; projectProfileFound |= isProjectProfile; @@ -656,7 +674,7 @@ public abstract class InspectionToolsConfigurable extends BaseConfigurable @Override public void selectProfile(Profile profile) { - myProfiles.selectProfile(profile); + myProfiles.selectProfile((InspectionProfileImpl)profile); } @Override diff --git a/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/header/ProfilesConfigurableComboBox.java b/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/header/ProfilesConfigurableComboBox.java index 36148ffb920e..f36317726bf4 100644 --- a/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/header/ProfilesConfigurableComboBox.java +++ b/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/header/ProfilesConfigurableComboBox.java @@ -16,22 +16,25 @@ package com.intellij.profile.codeInspection.ui.header; import com.intellij.codeInspection.ex.InspectionProfileImpl; -import com.intellij.profile.Profile; -import com.intellij.ui.IdeBorderFactory; -import com.intellij.ui.ListCellRendererWrapper; +import com.intellij.icons.AllIcons; +import com.intellij.ui.*; +import com.intellij.util.ui.UIUtil; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -import java.util.Collection; +import java.util.*; +import java.util.List; /** * @author Dmitry Batkovich */ -public abstract class ProfilesConfigurableComboBox extends JPanel { +abstract class ProfilesConfigurableComboBox extends JPanel { private static final String COMBO_CARD = "combo.card"; private static final String EDIT_CARD = "edit.card"; + public static final String PROJECT_LEVEL_SEPARATOR_TEXT = "Project Level"; + public static final String GLOBAL_LEVEL_SEPARATOR_TEXT = "Global Level"; private final JComboBox myProfilesComboBox; private final CardLayout myCardLayout; @@ -39,7 +42,10 @@ public abstract class ProfilesConfigurableComboBox extends JPanel { private final SaveInputComponentValidator.Wrapper mySaveListener; private final JPanel myComboBoxPanel; - public ProfilesConfigurableComboBox(final ListCellRendererWrapper comboBoxItemsRenderer) { + private InspectionProfileImpl myFirstGlobalProfile; + private SortedComboBoxModel myComboModel; + + public ProfilesConfigurableComboBox(final ListCellRendererWrapper comboBoxItemsRenderer) { myComboBoxPanel = new JPanel(); myCardLayout = new CardLayout(); @@ -53,7 +59,49 @@ public abstract class ProfilesConfigurableComboBox extends JPanel { mySubmitNameComponent = new ValidatedTextField(mySaveListener); myComboBoxPanel.add(mySubmitNameComponent, EDIT_CARD); - myProfilesComboBox.setRenderer(comboBoxItemsRenderer); + myComboModel = new SortedComboBoxModel(new Comparator() { + @Override + public int compare(final InspectionProfileImpl p1, final InspectionProfileImpl p2) { + return ProfilesConfigurableComboBox.this.compare(p1, p2); + } + }); + myProfilesComboBox.setModel(myComboModel); + //noinspection GtkPreferredJComboBoxRenderer + myProfilesComboBox.setRenderer(new ListCellRenderer() { + + @Override + public Component getListCellRendererComponent(JList list, + Object o, + int index, + boolean isSelected, + boolean cellHasFocus) { + InspectionProfileImpl value = (InspectionProfileImpl) o; + TitledSeparator separator = null; + if (index != -1) { + if (!value.isProjectLevel()) { + if (value == myFirstGlobalProfile) { + separator = new TitledSeparator(GLOBAL_LEVEL_SEPARATOR_TEXT); + } + } else { + if (value == myComboModel.get(0)) { + separator = new TitledSeparator(PROJECT_LEVEL_SEPARATOR_TEXT); + } + } + } + Component renderedComponent = comboBoxItemsRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); + if (separator == null) { + return renderedComponent; + } + UIUtil.applyStyle(UIUtil.ComponentStyle.MINI, separator.getLabel()); + separator.getLabel().setIcon(separator.getText().equals(PROJECT_LEVEL_SEPARATOR_TEXT) ? AllIcons.General.ProjectSettings : AllIcons.General.Settings); + separator.setBorder(IdeBorderFactory.createEmptyBorder(2, 2, 0, 2)); + JPanel p = new JPanel(); + p.setLayout(new BorderLayout()); + p.add(separator, BorderLayout.NORTH); + p.add(renderedComponent, BorderLayout.CENTER); + return p; + } + }); myProfilesComboBox.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { @@ -73,7 +121,11 @@ public abstract class ProfilesConfigurableComboBox extends JPanel { protected abstract void onProfileChosen(final InspectionProfileImpl inspectionProfile); - public void showEditCard(final String initialValue, final SaveInputComponentValidator inputValidator) { + protected abstract int compare(final InspectionProfileImpl p1, final InspectionProfileImpl p2); + + protected abstract boolean isProjectLevel(final InspectionProfileImpl p1); + + void showEditCard(final String initialValue, final SaveInputComponentValidator inputValidator) { mySaveListener.setDelegate(inputValidator); mySubmitNameComponent.setText(initialValue); myCardLayout.show(myComboBoxPanel, EDIT_CARD); @@ -85,28 +137,52 @@ public abstract class ProfilesConfigurableComboBox extends JPanel { }); } - public void reset(final Collection profiles) { - final DefaultComboBoxModel aModel = new DefaultComboBoxModel(); - myProfilesComboBox.setModel(aModel); - for (Profile profile : profiles) { - aModel.addElement(profile); + void reset(final Collection profiles) { + myComboModel.clear(); + for (InspectionProfileImpl profile : profiles) { + myComboModel.add(profile); } myProfilesComboBox.setSelectedIndex(0); + findFirstGlobalProfile(); } - public DefaultComboBoxModel getModel() { - return (DefaultComboBoxModel)myProfilesComboBox.getModel(); + void addProfile(InspectionProfileImpl inspectionProfile) { + myComboModel.add(inspectionProfile); + if (!isProjectLevel(inspectionProfile)) { + findFirstGlobalProfile(); + } } - public InspectionProfileImpl getSelectedProfile() { + private void findFirstGlobalProfile() { + myFirstGlobalProfile = null; + for (InspectionProfileImpl profile : getProfiles()) { + if (!isProjectLevel(profile)) { + myFirstGlobalProfile = profile; + break; + } + } + } + + void removeProfile(InspectionProfileImpl profile) { + myComboModel.remove(profile); + if (!isProjectLevel(profile) && profile == myFirstGlobalProfile) { + findFirstGlobalProfile(); + } + } + + List getProfiles() { + return myComboModel.getItems(); + } + + InspectionProfileImpl getSelectedProfile() { return (InspectionProfileImpl)myProfilesComboBox.getSelectedItem(); } - public void selectProfile(Profile inspectionProfile) { + void selectProfile(InspectionProfileImpl inspectionProfile) { myProfilesComboBox.setSelectedItem(inspectionProfile); } - public void showComboBoxCard() { + void showComboBoxCard() { myCardLayout.show(myComboBoxPanel, COMBO_CARD); } }