From adddfdfaae7e2ef6ee7d56ba5ea38d9c58e2a458 Mon Sep 17 00:00:00 2001 From: "Gregory.Shrago" Date: Wed, 3 Apr 2019 10:44:14 +0300 Subject: [PATCH] cleanup --- .../util/scopeChooser/IgnoringComboBox.java | 38 --------- .../util/scopeChooser/ScopeChooserCombo.java | 84 +++++++++++-------- 2 files changed, 48 insertions(+), 74 deletions(-) delete mode 100644 platform/lang-impl/src/com/intellij/ide/util/scopeChooser/IgnoringComboBox.java diff --git a/platform/lang-impl/src/com/intellij/ide/util/scopeChooser/IgnoringComboBox.java b/platform/lang-impl/src/com/intellij/ide/util/scopeChooser/IgnoringComboBox.java deleted file mode 100644 index 3aa67c83bf1f..000000000000 --- a/platform/lang-impl/src/com/intellij/ide/util/scopeChooser/IgnoringComboBox.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 2000-2012 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.ide.util.scopeChooser; - -import com.intellij.openapi.ui.ComboBox; - -public abstract class IgnoringComboBox extends ComboBox { - - @Override - public void setSelectedItem(final Object item) { - if (!(isIgnored(item))) { - super.setSelectedItem(item); - } - } - - @Override - public void setSelectedIndex(final int anIndex) { - final Object item = getItemAt(anIndex); - if (!isIgnored(item)) { - super.setSelectedIndex(anIndex); - } - } - - protected abstract boolean isIgnored(Object item); -} diff --git a/platform/lang-impl/src/com/intellij/ide/util/scopeChooser/ScopeChooserCombo.java b/platform/lang-impl/src/com/intellij/ide/util/scopeChooser/ScopeChooserCombo.java index fe8c49ad973f..421837a15bcf 100644 --- a/platform/lang-impl/src/com/intellij/ide/util/scopeChooser/ScopeChooserCombo.java +++ b/platform/lang-impl/src/com/intellij/ide/util/scopeChooser/ScopeChooserCombo.java @@ -25,7 +25,7 @@ import org.jetbrains.annotations.Nullable; import javax.swing.*; import java.awt.*; -import java.awt.event.ActionListener; +import java.awt.event.ActionEvent; import java.util.Comparator; import java.util.List; @@ -33,21 +33,14 @@ public class ScopeChooserCombo extends ComboboxWithBrowseButton implements Dispo private Project myProject; private boolean mySuggestSearchInLibs; private boolean myPrevSearchFiles; - private NamedScopeManager myNamedScopeManager; - private DependencyValidationManager myValidationManager; private boolean myCurrentSelection = true; private boolean myUsageView = true; - private Condition myScopeFilter; + private Condition myScopeFilter; private boolean myShowEmptyScopes; private BrowseListener myBrowseListener = null; public ScopeChooserCombo() { - super(new IgnoringComboBox(){ - @Override - protected boolean isIgnored(Object item) { - return item instanceof ScopeSeparator; - } - }); + super(new MyComboBox()); } public ScopeChooserCombo(final Project project, boolean suggestSearchInLibs, boolean prevSearchWholeFiles, String preselect) { @@ -67,7 +60,7 @@ public class ScopeChooserCombo extends ComboboxWithBrowseButton implements Dispo final boolean suggestSearchInLibs, final boolean prevSearchWholeFiles, final Object selection, - @Nullable Condition scopeFilter) { + @Nullable Condition scopeFilter) { mySuggestSearchInLibs = suggestSearchInLibs; myPrevSearchFiles = prevSearchWholeFiles; myProject = project; @@ -78,13 +71,11 @@ public class ScopeChooserCombo extends ComboboxWithBrowseButton implements Dispo selectItem(selectedScope); }; myScopeFilter = scopeFilter; - myNamedScopeManager = NamedScopeManager.getInstance(project); - myNamedScopeManager.addScopeListener(scopeListener, this); - myValidationManager = DependencyValidationManager.getInstance(project); - myValidationManager.addScopeListener(scopeListener, this); - addActionListener(createScopeChooserListener()); + NamedScopeManager.getInstance(project).addScopeListener(scopeListener, this); + DependencyValidationManager.getInstance(project).addScopeListener(scopeListener, this); + addActionListener(this::handleScopeChooserAction); - final ComboBox combo = (ComboBox)getComboBox(); + ComboBox combo = getComboBox(); combo.setMinimumAndPreferredWidth(JBUI.scale(300)); combo.setRenderer(new ScopeDescriptionWithDelimiterRenderer()); @@ -103,6 +94,12 @@ public class ScopeChooserCombo extends ComboboxWithBrowseButton implements Dispo }; } + @Override + public ComboBox getComboBox() { + //noinspection unchecked + return (ComboBox)super.getComboBox(); + } + public void setBrowseListener(BrowseListener browseListener) { myBrowseListener = browseListener; } @@ -134,20 +131,19 @@ public class ScopeChooserCombo extends ComboboxWithBrowseButton implements Dispo } } - private ActionListener createScopeChooserListener() { - return e -> { - final String selection = getSelectedScopeName(); - if (myBrowseListener != null) myBrowseListener.onBeforeBrowseStarted(); - final EditScopesDialog dlg = EditScopesDialog.showDialog(myProject, selection); - if (dlg.isOK()){ - rebuildModel(); - final NamedScope namedScope = dlg.getSelectedScope(); - if (namedScope != null) { - selectItem(namedScope.getName()); - } + /** @noinspection unused*/ + private void handleScopeChooserAction(ActionEvent ignore) { + String selection = getSelectedScopeName(); + if (myBrowseListener != null) myBrowseListener.onBeforeBrowseStarted(); + EditScopesDialog dlg = EditScopesDialog.showDialog(myProject, selection); + if (dlg.isOK()){ + rebuildModel(); + NamedScope namedScope = dlg.getSelectedScope(); + if (namedScope != null) { + selectItem(namedScope.getName()); } - if (myBrowseListener != null) myBrowseListener.onAfterBrowseFinished(); - }; + } + if (myBrowseListener != null) myBrowseListener.onAfterBrowseFinished(); } private void rebuildModel() { @@ -217,16 +213,14 @@ public class ScopeChooserCombo extends ComboboxWithBrowseButton implements Dispo @Nullable public SearchScope getSelectedScope() { - final JComboBox combo = getComboBox(); - int idx = combo.getSelectedIndex(); - return idx < 0 ? null : ((ScopeDescriptor)combo.getSelectedItem()).getScope(); + ScopeDescriptor item = (ScopeDescriptor)getComboBox().getSelectedItem(); + return item == null ? null : item.getScope(); } @Nullable public String getSelectedScopeName() { - final JComboBox combo = getComboBox(); - int idx = combo.getSelectedIndex(); - return idx < 0 ? null : ((ScopeDescriptor)combo.getSelectedItem()).getDisplay(); + ScopeDescriptor item = (ScopeDescriptor)getComboBox().getSelectedItem(); + return item == null ? null : item.getDisplay(); } private static class ScopeSeparator extends ScopeDescriptor { @@ -260,4 +254,22 @@ public class ScopeChooserCombo extends ComboboxWithBrowseButton implements Dispo void onBeforeBrowseStarted(); void onAfterBrowseFinished(); } + + private static class MyComboBox extends ComboBox { + + @Override + public void setSelectedItem(Object item) { + if (!(item instanceof ScopeSeparator)) { + super.setSelectedItem(item); + } + } + + @Override + public void setSelectedIndex(final int anIndex) { + Object item = getItemAt(anIndex); + if (!(item instanceof ScopeSeparator)) { + super.setSelectedIndex(anIndex); + } + } + } }