From fd3dfb56a0bd83035e7bf51b63f4e011da847d09 Mon Sep 17 00:00:00 2001 From: anna Date: Wed, 21 Mar 2012 17:45:04 +0100 Subject: [PATCH] inspections ui: forbid to delete the last profile of each type; check for all existing names during new profile creation (EA-34224 - assert: ProjectInspectionToolsConfigurable.apply) (cherry picked from commit 5da19c1) --- .../ui/InspectionToolsConfigurable.java | 34 ++++++++++++++++--- .../ui/SingleInspectionProfilePanel.java | 7 ++-- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/InspectionToolsConfigurable.java b/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/InspectionToolsConfigurable.java index 4f336fc92a80..f97a5e122cb1 100644 --- a/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/InspectionToolsConfigurable.java +++ b/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/InspectionToolsConfigurable.java @@ -49,6 +49,7 @@ import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.VfsUtil; import com.intellij.openapi.vfs.VirtualFile; 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.ui.components.JBScrollPane; @@ -99,7 +100,8 @@ public abstract class InspectionToolsConfigurable extends BaseConfigurable imple InspectionToolRegistrar.getInstance().buildInspectionSearchIndexIfNecessary(); myAddButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - final ModifiableModel model = SingleInspectionProfilePanel.createNewProfile(-1, getSelectedObject(), myWholePanel, ""); + final Set existingProfileNames = myPanels.keySet(); + final ModifiableModel model = SingleInspectionProfilePanel.createNewProfile(-1, getSelectedObject(), myWholePanel, "", existingProfileNames); if (model != null) { addProfile((InspectionProfileImpl)model); myDeletedProfiles.remove(model.getName()); @@ -113,7 +115,7 @@ public abstract class InspectionToolsConfigurable extends BaseConfigurable imple final InspectionProfileImpl selectedProfile = (InspectionProfileImpl)myProfiles.getSelectedItem(); ((DefaultComboBoxModel)myProfiles.getModel()).removeElement(selectedProfile); myDeletedProfiles.add(selectedProfile.getName()); - myDeleteButton.setEnabled(myProfiles.getModel().getSize() > 1); + myDeleteButton.setEnabled(isDeleteEnabled(selectedProfile)); } }); @@ -216,7 +218,9 @@ public abstract class InspectionToolsConfigurable extends BaseConfigurable imple myCopyButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - final InspectionProfileImpl model = (InspectionProfileImpl)SingleInspectionProfilePanel.createNewProfile(0, getSelectedObject(), myWholePanel, ""); + final Set existingProfileNames = myPanels.keySet(); + final InspectionProfileImpl model = (InspectionProfileImpl) + SingleInspectionProfilePanel.createNewProfile(0, getSelectedObject(), myWholePanel, "", existingProfileNames); if (model != null) { final InspectionProfileImpl modifiableModel = (InspectionProfileImpl)model.getModifiableModel(); modifiableModel.setModified(true); @@ -285,7 +289,7 @@ public abstract class InspectionToolsConfigurable extends BaseConfigurable imple myProfiles.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { final InspectionProfileImpl profile = (InspectionProfileImpl)myProfiles.getSelectedItem(); - myDeleteButton.setEnabled(myProfiles.getModel().getSize() > 1); + myDeleteButton.setEnabled(isDeleteEnabled(profile)); myLayout.show(myPanel, profile.getName()); SingleInspectionProfilePanel panel = getSelectedPanel(); if (panel != null) { @@ -352,7 +356,7 @@ public abstract class InspectionToolsConfigurable extends BaseConfigurable imple final InspectionProfileImpl inspectionProfile = getCurrentProfile(); myProfiles.setSelectedItem(inspectionProfile); myLayout.show(myPanel, inspectionProfile.getName()); - myDeleteButton.setEnabled(getProfiles().size() > 1 && inspectionProfile.getProfileManager() == myProfileManager); + myDeleteButton.setEnabled(isDeleteEnabled(inspectionProfile)); final SingleInspectionProfilePanel panel = getSelectedPanel(); if (panel != null) { panel.setVisible(true);//make sure that UI was initialized @@ -373,6 +377,26 @@ public abstract class InspectionToolsConfigurable extends BaseConfigurable imple } } + private boolean isDeleteEnabled(InspectionProfileImpl inspectionProfile) { + final ProfileManager profileManager = inspectionProfile.getProfileManager(); + + 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); + if (inspectionProfile == profile) continue; + final boolean isProjectProfile = profile.getProfileManager() == myProjectProfileManager; + projectProfileFound |= isProjectProfile; + ideProfileFound |= !isProjectProfile; + + if (ideProfileFound && projectProfileFound) break; + } + + return profileManager == myProjectProfileManager ? projectProfileFound : ideProfileFound; + } + protected Collection getProfiles() { final Collection result = new ArrayList(); result.addAll(myProfileManager.getProfiles()); diff --git a/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/SingleInspectionProfilePanel.java b/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/SingleInspectionProfilePanel.java index dd3ebd75c5a5..ba2c682d3ecf 100644 --- a/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/SingleInspectionProfilePanel.java +++ b/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/SingleInspectionProfilePanel.java @@ -59,7 +59,6 @@ import com.intellij.psi.search.scope.packageSet.NamedScope; import com.intellij.ui.*; import com.intellij.ui.treeStructure.Tree; import com.intellij.util.Alarm; -import com.intellij.util.ArrayUtil; import com.intellij.util.config.StorageAccessors; import com.intellij.util.containers.Convertor; import com.intellij.util.ui.UIUtil; @@ -262,12 +261,12 @@ public class SingleInspectionProfilePanel extends JPanel { public static ModifiableModel createNewProfile(final int initValue, ModifiableModel selectedProfile, JPanel parent, - String profileName) { - + String profileName, + Set existingProfileNames) { profileName = Messages.showInputDialog(parent, profileName, "Create New Inspection Profile", Messages.getQuestionIcon()); if (profileName == null) return null; final ProfileManager profileManager = selectedProfile.getProfileManager(); - if (ArrayUtil.find(profileManager.getAvailableProfileNames(), profileName) != -1) { + if (existingProfileNames.contains(profileName)) { Messages.showErrorDialog(InspectionsBundle.message("inspection.unable.to.create.profile.message", profileName), InspectionsBundle.message("inspection.unable.to.create.profile.dialog.title")); return null;