From efe0d5a7b5b8fc5bd8fb066ab51f9c6ad46ac7d0 Mon Sep 17 00:00:00 2001 From: Dmitry Batkovich Date: Tue, 22 Jul 2014 14:34:45 +0400 Subject: [PATCH] Inspections settings ui: filter actions in toolbar added --- .../ui/SingleInspectionProfilePanel.java | 32 ++++- .../ui/filter/InspectionFilterAction.java | 129 ++++++++++++++++++ .../ui/filter/InspectionsFilter.java | 98 +++++++++++++ 3 files changed, 252 insertions(+), 7 deletions(-) create mode 100644 platform/lang-impl/src/com/intellij/profile/codeInspection/ui/filter/InspectionFilterAction.java create mode 100644 platform/lang-impl/src/com/intellij/profile/codeInspection/ui/filter/InspectionsFilter.java 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 a910f6d596d2..62e29fc635ca 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 @@ -52,6 +52,8 @@ import com.intellij.profile.codeInspection.InspectionProfileManager; import com.intellij.profile.codeInspection.InspectionProfileManagerImpl; import com.intellij.profile.codeInspection.InspectionProjectProfileManager; import com.intellij.profile.codeInspection.SeverityProvider; +import com.intellij.profile.codeInspection.ui.filter.InspectionFilterAction; +import com.intellij.profile.codeInspection.ui.filter.InspectionsFilter; import com.intellij.profile.codeInspection.ui.inspectionsTree.InspectionConfigTreeNode; import com.intellij.profile.codeInspection.ui.inspectionsTree.InspectionsConfigTreeComparator; import com.intellij.profile.codeInspection.ui.inspectionsTree.InspectionsConfigTreeRenderer; @@ -105,6 +107,12 @@ public class SingleInspectionProfilePanel extends JPanel { private JPanel myOptionsPanel; private JPanel myInspectionProfilePanel = null; private FilterComponent myProfileFilter; + private final InspectionsFilter myInspectionsFilter = new InspectionsFilter() { + @Override + protected void filterChanged() { + filterTree(myProfileFilter.getFilter()); + } + }; private final InspectionConfigTreeNode myRoot = new InspectionConfigTreeNode(InspectionsBundle.message("inspection.root.node.title")); private final Alarm myAlarm = new Alarm(); @@ -308,7 +316,7 @@ public class SingleInspectionProfilePanel extends JPanel { myProfileFilter.setFilter(filter); } - public void filterTree(String filter) { + private void filterTree(@Nullable String filter) { if (myTreeTable != null) { getExpandedNodes(mySelectedProfile).saveVisibleState(myTreeTable.getTree()); fillTreeData(filter, true); @@ -320,6 +328,10 @@ public class SingleInspectionProfilePanel extends JPanel { } } + private void filterTree() { + filterTree(myProfileFilter != null ? myProfileFilter.getFilter() : null); + } + private void reloadModel() { try { myIsInRestore = true; @@ -346,6 +358,10 @@ public class SingleInspectionProfilePanel extends JPanel { final CommonActionsManager actionManager = CommonActionsManager.getInstance(); DefaultActionGroup actions = new DefaultActionGroup(); + + actions.add(new InspectionFilterAction(mySelectedProfile, myInspectionsFilter)); + actions.addSeparator(); + actions.add(actionManager.createExpandAllAction(myTreeExpander, myTreeTable)); actions.add(actionManager.createCollapseAllAction(myTreeExpander, myTreeTable)); @@ -392,8 +408,6 @@ public class SingleInspectionProfilePanel extends JPanel { } }); - actions.addSeparator(); - final ActionToolbar actionToolbar = ActionManager.getInstance().createActionToolbar(ActionPlaces.UNKNOWN, actions, true); actionToolbar.setTargetComponent(this); return actionToolbar; @@ -637,7 +651,7 @@ public class SingleInspectionProfilePanel extends JPanel { return forceInclude; } - private void fillTreeData(String filter, boolean forceInclude) { + private void fillTreeData(@Nullable String filter, boolean forceInclude) { if (mySelectedProfile == null) return; myRoot.removeAllChildren(); myRoot.dropCache(); @@ -646,11 +660,15 @@ public class SingleInspectionProfilePanel extends JPanel { if (filter != null && !filter.isEmpty()) { keySetList.addAll(SearchUtil.findKeys(filter, quoted)); } + Project project = myProjectProfileManager.getProject(); for (ToolDescriptors toolDescriptors : myInitialToolDescriptors) { final Descriptor descriptor = toolDescriptors.getDefaultDescriptor(); if (filter != null && !filter.isEmpty() && !isDescriptorAccepted(descriptor, filter, forceInclude, keySetList, quoted)) { continue; } + if (!myInspectionsFilter.matches(mySelectedProfile.getTools(toolDescriptors.getDefaultDescriptor().getKey().toString(), project))) { + continue; + } final InspectionConfigTreeNode node = new InspectionConfigTreeNode(toolDescriptors); getGroupNode(myRoot, toolDescriptors.getDefaultDescriptor().getGroup()).add(node); myRoot.dropCache(); @@ -908,7 +926,7 @@ public class SingleInspectionProfilePanel extends JPanel { myInitialProfile = mySelectedProfile.getName(); } initToolStates(); - filterTree(myProfileFilter != null ? myProfileFilter.getFilter() : null); + filterTree(); } @Override @@ -962,8 +980,8 @@ public class SingleInspectionProfilePanel extends JPanel { final JPanel northPanel = new JPanel(new GridBagLayout()); northPanel.setBorder(IdeBorderFactory.createEmptyBorder(2, 0, 2, 0)); - northPanel.add(createTreeToolbarPanel().getComponent(), new GridBagConstraints(0, 0, 1, 1, 0.5, 1, GridBagConstraints.BASELINE_LEADING, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); - northPanel.add(myProfileFilter, new GridBagConstraints(1, 0, 1, 1, 1, 1, GridBagConstraints.BASELINE_TRAILING, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); + northPanel.add(myProfileFilter, new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.BASELINE_TRAILING, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); + northPanel.add(createTreeToolbarPanel().getComponent(), new GridBagConstraints(1, 0, 1, 1, 0.5, 1, GridBagConstraints.BASELINE_LEADING, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); treePanel.add(northPanel, BorderLayout.NORTH); myMainSplitter = new Splitter(false); diff --git a/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/filter/InspectionFilterAction.java b/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/filter/InspectionFilterAction.java new file mode 100644 index 000000000000..cbd1c2482032 --- /dev/null +++ b/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/filter/InspectionFilterAction.java @@ -0,0 +1,129 @@ +/* + * Copyright 2000-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.profile.codeInspection.ui.filter; + +import com.intellij.codeHighlighting.HighlightDisplayLevel; +import com.intellij.codeInsight.daemon.impl.SeverityRegistrar; +import com.intellij.codeInspection.ex.InspectionProfileImpl; +import com.intellij.icons.AllIcons; +import com.intellij.lang.annotation.HighlightSeverity; +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.actionSystem.DefaultActionGroup; +import com.intellij.openapi.actionSystem.ex.CheckboxAction; +import com.intellij.profile.codeInspection.SeverityProvider; +import com.intellij.profile.codeInspection.ui.LevelChooserAction; +import com.intellij.profile.codeInspection.ui.SingleInspectionProfilePanel; +import org.jetbrains.annotations.Nullable; + +import java.util.SortedSet; + +/** + * @author Dmitry Batkovich + */ +public class InspectionFilterAction extends DefaultActionGroup { + + private final SeverityRegistrar mySeverityRegistrar; + private final InspectionsFilter myInspectionsFilter; + + public InspectionFilterAction(final InspectionProfileImpl profile, final InspectionsFilter inspectionsFilter) { + super("Filter Inspections", true); + myInspectionsFilter = inspectionsFilter; + mySeverityRegistrar = ((SeverityProvider)profile.getProfileManager()).getOwnSeverityRegistrar(); + getTemplatePresentation().setIcon(AllIcons.General.Filter); + tune(); + } + + private void tune() { + addAction(new ShowEnabledOrDisabledInspectionsAction(null)); + addAction(new ShowEnabledOrDisabledInspectionsAction(true)); + addAction(new ShowEnabledOrDisabledInspectionsAction(false)); + addSeparator(); + + final SortedSet severities = LevelChooserAction.getSeverities(mySeverityRegistrar); + for (final HighlightSeverity severity : severities) { + add(new ShowWithSpecifiedSeverityInspectionsAction(severity)); + } + addSeparator(); + + add(new ShowAvailableOnlyOnAnalyzeInspectionsAction()); + } + + private class ShowAvailableOnlyOnAnalyzeInspectionsAction extends CheckboxAction { + + public ShowAvailableOnlyOnAnalyzeInspectionsAction() { + super("Show Only \"Available only for Analyze | Inspect Code\""); + } + + @Override + public boolean isSelected(final AnActionEvent e) { + return myInspectionsFilter.isAvailableOnlyForAnalyze(); + } + + @Override + public void setSelected(final AnActionEvent e, final boolean state) { + myInspectionsFilter.setAvailableOnlyForAnalyze(state); + } + } + + private class ShowWithSpecifiedSeverityInspectionsAction extends CheckboxAction { + + private final HighlightSeverity mySeverity; + + private ShowWithSpecifiedSeverityInspectionsAction(final HighlightSeverity severity) { + super(SingleInspectionProfilePanel.renderSeverity(severity), + null, + HighlightDisplayLevel.find(severity).getIcon()); + mySeverity = severity; + } + + + @Override + public boolean isSelected(final AnActionEvent e) { + return myInspectionsFilter.containsSeverity(mySeverity); + } + + @Override + public void setSelected(final AnActionEvent e, final boolean state) { + if (state) { + myInspectionsFilter.add(mySeverity); + } else { + myInspectionsFilter.remove(mySeverity); + } + } + } + + private class ShowEnabledOrDisabledInspectionsAction extends CheckboxAction { + + private final Boolean myShowEnabledActions; + + public ShowEnabledOrDisabledInspectionsAction(@Nullable final Boolean showEnabledActions) { + super(showEnabledActions == null ? "All Inspections" : (showEnabledActions ? "Enabled" : "Disabled")); + myShowEnabledActions = showEnabledActions; + } + + + @Override + public boolean isSelected(final AnActionEvent e) { + return myInspectionsFilter.getSuitableInspectionsStates() == myShowEnabledActions; + } + + @Override + public void setSelected(final AnActionEvent e, final boolean state) { + myInspectionsFilter.setSuitableInspectionsStates(myShowEnabledActions); + } + } +} \ No newline at end of file diff --git a/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/filter/InspectionsFilter.java b/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/filter/InspectionsFilter.java new file mode 100644 index 000000000000..d3f18f8fdb57 --- /dev/null +++ b/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/filter/InspectionsFilter.java @@ -0,0 +1,98 @@ +/* + * Copyright 2000-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.profile.codeInspection.ui.filter; + +import com.intellij.codeInspection.ex.GlobalInspectionToolWrapper; +import com.intellij.codeInspection.ex.InspectionToolWrapper; +import com.intellij.codeInspection.ex.ScopeToolState; +import com.intellij.codeInspection.ex.Tools; +import com.intellij.lang.annotation.HighlightSeverity; +import com.intellij.util.containers.HashSet; +import org.jetbrains.annotations.Nullable; + +import java.util.Set; + +/** + * @author Dmitry Batkovich + */ +public abstract class InspectionsFilter { + + private final Set mySuitableSeverities = new HashSet(); + private Boolean mySuitableInspectionsStates; + private boolean myAvailableOnlyForAnalyze; + + public boolean isAvailableOnlyForAnalyze() { + return myAvailableOnlyForAnalyze; + } + + public Boolean getSuitableInspectionsStates() { + return mySuitableInspectionsStates; + } + + public boolean containsSeverity(final HighlightSeverity severity) { + return mySuitableSeverities.contains(severity); + } + + public void setAvailableOnlyForAnalyze(final boolean availableOnlyForAnalyze) { + myAvailableOnlyForAnalyze = availableOnlyForAnalyze; + filterChanged(); + } + + public void setSuitableInspectionsStates(@Nullable final Boolean suitableInspectionsStates) { + mySuitableInspectionsStates = suitableInspectionsStates; + filterChanged(); + } + + public void add(final HighlightSeverity severity) { + mySuitableSeverities.add(severity); + filterChanged(); + } + + public void remove(final HighlightSeverity severity) { + mySuitableSeverities.remove(severity); + filterChanged(); + } + + public boolean matches(final Tools tools) { + if (mySuitableInspectionsStates != null && mySuitableInspectionsStates != tools.isEnabled()) { + return false; + } + + if (myAvailableOnlyForAnalyze != isAvailableOnlyForAnalyze(tools)) { + return false; + } + + if (mySuitableSeverities.isEmpty()) { + return true; + } + for (final ScopeToolState state : tools.getTools()) { + if (mySuitableInspectionsStates != null && mySuitableInspectionsStates != state.isEnabled()) { + continue; + } + if (mySuitableSeverities.contains(tools.getDefaultState().getLevel().getSeverity())) { + return true; + } + } + return false; + } + + protected abstract void filterChanged(); + + private static boolean isAvailableOnlyForAnalyze(final Tools tools) { + final InspectionToolWrapper tool = tools.getTool(); + return tool instanceof GlobalInspectionToolWrapper && ((GlobalInspectionToolWrapper)tool).worksInBatchModeOnly(); + } +} \ No newline at end of file