From c6aee28809d3e32401608a2e71fc58a10436553b Mon Sep 17 00:00:00 2001 From: Alexander Lobas Date: Mon, 16 Jul 2012 11:48:16 +0400 Subject: [PATCH] EA-37439 --- .../editors/ChooseClassDialog.java | 253 +++++++++--------- 1 file changed, 128 insertions(+), 125 deletions(-) diff --git a/plugins/android-designer/src/com/intellij/android/designer/propertyTable/editors/ChooseClassDialog.java b/plugins/android-designer/src/com/intellij/android/designer/propertyTable/editors/ChooseClassDialog.java index a730c08153f1..56619f5dc061 100644 --- a/plugins/android-designer/src/com/intellij/android/designer/propertyTable/editors/ChooseClassDialog.java +++ b/plugins/android-designer/src/com/intellij/android/designer/propertyTable/editors/ChooseClassDialog.java @@ -1,125 +1,128 @@ -/* - * 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.android.designer.propertyTable.editors; - -import com.intellij.ide.util.DefaultPsiElementCellRenderer; -import com.intellij.openapi.module.Module; -import com.intellij.openapi.project.Project; -import com.intellij.openapi.ui.DialogWrapper; -import com.intellij.psi.JavaPsiFacade; -import com.intellij.psi.PsiClass; -import com.intellij.psi.search.GlobalSearchScope; -import com.intellij.psi.search.searches.ClassInheritorsSearch; -import com.intellij.ui.DoubleClickListener; -import com.intellij.ui.ListSpeedSearch; -import com.intellij.ui.ScrollPaneFactory; -import com.intellij.ui.components.JBList; -import org.jetbrains.annotations.Nullable; - -import javax.swing.*; -import javax.swing.event.ListSelectionEvent; -import javax.swing.event.ListSelectionListener; -import java.awt.event.MouseEvent; -import java.util.Collection; -import java.util.Collections; - -/** - * @author Alexander Lobas - */ -public class ChooseClassDialog extends DialogWrapper implements ListSelectionListener { - private final JList myList = new JBList(); - private final JScrollPane myComponent = ScrollPaneFactory.createScrollPane(myList); - private String myResultClassName; - - public ChooseClassDialog(Module module, String title, boolean includeAll, String... classes) { - super(module.getProject()); - - new DoubleClickListener() { - @Override - protected boolean onDoubleClick(MouseEvent e) { - if (myList.getSelectedValue() != null) { - close(OK_EXIT_CODE); - return true; - } - return false; - } - }.installOn(myList); - - DefaultListModel model = new DefaultListModel(); - for (String className : classes) { - for (PsiClass psiClass : findInheritors(module, className, includeAll)) { - model.addElement(psiClass); - } - } - myList.setModel(model); - myList.setCellRenderer(new DefaultPsiElementCellRenderer()); - - ListSelectionModel selectionModel = myList.getSelectionModel(); - selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); - selectionModel.addListSelectionListener(this); - - new ListSpeedSearch(myList) { - @Override - protected boolean isMatchingElement(Object element, String pattern) { - PsiClass psiClass = (PsiClass)element; - return compare(psiClass.getName(), pattern) || compare(psiClass.getQualifiedName(), pattern); - } - }; - - setTitle(title); - setOKActionEnabled(false); - - init(); - } - - private static Collection findInheritors(Module module, String name, boolean includeAll) { - PsiClass base = findClass(module, name); - if (base != null) { - GlobalSearchScope scope = includeAll ? - GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module, false) : - GlobalSearchScope.moduleScope(module); - return ClassInheritorsSearch.search(base, scope, true).findAll(); - } - return Collections.emptyList(); - } - - @Nullable - public static PsiClass findClass(Module module, String name) { - Project project = module.getProject(); - return JavaPsiFacade.getInstance(project).findClass(name, GlobalSearchScope.allScope(project)); - } - - @Override - public JComponent getPreferredFocusedComponent() { - return myList; - } - - @Override - protected JComponent createCenterPanel() { - return myComponent; - } - - public String getClassName() { - return myResultClassName; - } - - @Override - public void valueChanged(ListSelectionEvent e) { - PsiClass psiClass = (PsiClass)myList.getSelectedValue(); - setOKActionEnabled(psiClass != null); - myResultClassName = psiClass == null ? null : psiClass.getQualifiedName(); - } -} +/* + * 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.android.designer.propertyTable.editors; + +import com.intellij.ide.util.DefaultPsiElementCellRenderer; +import com.intellij.openapi.module.Module; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.ui.DialogWrapper; +import com.intellij.psi.JavaPsiFacade; +import com.intellij.psi.PsiClass; +import com.intellij.psi.search.GlobalSearchScope; +import com.intellij.psi.search.searches.ClassInheritorsSearch; +import com.intellij.ui.DoubleClickListener; +import com.intellij.ui.ListSpeedSearch; +import com.intellij.ui.ScrollPaneFactory; +import com.intellij.ui.components.JBList; +import org.jetbrains.annotations.Nullable; + +import javax.swing.*; +import javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; +import java.awt.event.MouseEvent; +import java.util.Collection; +import java.util.Collections; + +/** + * @author Alexander Lobas + */ +public class ChooseClassDialog extends DialogWrapper implements ListSelectionListener { + private final JList myList = new JBList(); + private final JScrollPane myComponent = ScrollPaneFactory.createScrollPane(myList); + private String myResultClassName; + + public ChooseClassDialog(Module module, String title, boolean includeAll, String... classes) { + super(module.getProject()); + + new DoubleClickListener() { + @Override + protected boolean onDoubleClick(MouseEvent e) { + if (myList.getSelectedValue() != null) { + close(OK_EXIT_CODE); + return true; + } + return false; + } + }.installOn(myList); + + DefaultListModel model = new DefaultListModel(); + for (String className : classes) { + for (PsiClass psiClass : findInheritors(module, className, includeAll)) { + model.addElement(psiClass); + } + } + myList.setModel(model); + myList.setCellRenderer(new DefaultPsiElementCellRenderer()); + + ListSelectionModel selectionModel = myList.getSelectionModel(); + selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); + selectionModel.addListSelectionListener(this); + + new ListSpeedSearch(myList) { + @Override + protected boolean isMatchingElement(Object element, String pattern) { + PsiClass psiClass = (PsiClass)element; + return compare(psiClass.getName(), pattern) || compare(psiClass.getQualifiedName(), pattern); + } + }; + + setTitle(title); + setOKActionEnabled(false); + + init(); + } + + private static Collection findInheritors(Module module, String name, boolean includeAll) { + PsiClass base = findClass(module, name); + if (base != null) { + GlobalSearchScope scope = includeAll ? + GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module, false) : + GlobalSearchScope.moduleScope(module); + return ClassInheritorsSearch.search(base, scope, true).findAll(); + } + return Collections.emptyList(); + } + + @Nullable + public static PsiClass findClass(Module module, @Nullable String name) { + if (name == null) { + return null; + } + Project project = module.getProject(); + return JavaPsiFacade.getInstance(project).findClass(name, GlobalSearchScope.allScope(project)); + } + + @Override + public JComponent getPreferredFocusedComponent() { + return myList; + } + + @Override + protected JComponent createCenterPanel() { + return myComponent; + } + + public String getClassName() { + return myResultClassName; + } + + @Override + public void valueChanged(ListSelectionEvent e) { + PsiClass psiClass = (PsiClass)myList.getSelectedValue(); + setOKActionEnabled(psiClass != null); + myResultClassName = psiClass == null ? null : psiClass.getQualifiedName(); + } +}