Search in sources :

Example 6 with ClassFilter

use of com.intellij.ide.util.ClassFilter in project intellij-community by JetBrains.

the class UiUtils method createAddRemoveTreeClassChooserPanel.

public static JPanel createAddRemoveTreeClassChooserPanel(final ListTable table, final String chooserTitle, @NonNls String... ancestorClasses) {
    final ClassFilter filter;
    if (ancestorClasses.length == 0) {
        filter = ClassFilter.ALL;
    } else {
        filter = new SubclassFilter(ancestorClasses);
    }
    final JPanel panel = ToolbarDecorator.createDecorator(table).setAddAction(new AnActionButtonRunnable() {

        @Override
        public void run(AnActionButton button) {
            final DataContext dataContext = DataManager.getInstance().getDataContext(table);
            final Project project = CommonDataKeys.PROJECT.getData(dataContext);
            final int rowIndex;
            final ListWrappingTableModel tableModel = table.getModel();
            if (project == null) {
                tableModel.addRow();
                rowIndex = tableModel.getRowCount() - 1;
            } else {
                final TreeClassChooserFactory chooserFactory = TreeClassChooserFactory.getInstance(project);
                final TreeClassChooser classChooser = chooserFactory.createWithInnerClassesScopeChooser(chooserTitle, GlobalSearchScope.allScope(project), filter, null);
                classChooser.showDialog();
                final PsiClass selectedClass = classChooser.getSelected();
                if (selectedClass == null) {
                    return;
                }
                final String qualifiedName = selectedClass.getQualifiedName();
                final int index = tableModel.indexOf(qualifiedName, 0);
                if (index < 0) {
                    tableModel.addRow(qualifiedName);
                    rowIndex = tableModel.getRowCount() - 1;
                } else {
                    rowIndex = index;
                }
            }
            editTableCell(table, rowIndex, table.getColumnCount() > 1 && project != null ? 1 : 0);
        }
    }).setRemoveAction(button -> TableUtil.removeSelectedItems(table)).disableUpDownActions().createPanel();
    panel.setPreferredSize(JBUI.size(150, 100));
    return panel;
}
Also used : TreeClassChooser(com.intellij.ide.util.TreeClassChooser) PsiClass(com.intellij.psi.PsiClass) ListWrappingTableModel(com.intellij.codeInspection.ui.ListWrappingTableModel) Project(com.intellij.openapi.project.Project) DataContext(com.intellij.openapi.actionSystem.DataContext) TreeClassChooserFactory(com.intellij.ide.util.TreeClassChooserFactory) ClassFilter(com.intellij.ide.util.ClassFilter)

Example 7 with ClassFilter

use of com.intellij.ide.util.ClassFilter in project kotlin by JetBrains.

the class MoveKotlinNestedClassesDialog method initClassChooser.

private void initClassChooser(@NotNull KtClassOrObject initialTargetClass) {
    //noinspection ConstantConditions
    originalClassField.setText(originalClass.getFqName().asString());
    //noinspection ConstantConditions
    targetClassChooser = new ReferenceEditorComboWithBrowseButton(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            TreeClassChooser chooser = new TreeJavaClassChooserDialog(RefactoringBundle.message("choose.destination.class"), myProject, GlobalSearchScope.projectScope(myProject), new ClassFilter() {

                @Override
                public boolean isAccepted(PsiClass aClass) {
                    if (!(aClass instanceof KtLightClassForSourceDeclaration))
                        return false;
                    KtClassOrObject classOrObject = ((KtLightClassForSourceDeclaration) aClass).getKotlinOrigin();
                    if (classOrObject instanceof KtObjectDeclaration) {
                        return !((KtObjectDeclaration) classOrObject).isObjectLiteral();
                    }
                    if (classOrObject instanceof KtClass) {
                        KtClass ktClass = (KtClass) classOrObject;
                        return !(ktClass.isInner() || ktClass.isAnnotation());
                    }
                    return false;
                }
            }, null, null, true) {

                @Nullable
                @Override
                protected PsiClass getSelectedFromTreeUserObject(DefaultMutableTreeNode node) {
                    PsiClass psiClass = super.getSelectedFromTreeUserObject(node);
                    if (psiClass != null)
                        return psiClass;
                    Object userObject = node.getUserObject();
                    if (!(userObject instanceof KtClassOrObjectTreeNode))
                        return null;
                    return LightClassUtilsKt.toLightClass(((KtClassOrObjectTreeNode) userObject).getValue());
                }
            };
            chooser.selectDirectory((targetClass != null ? targetClass : originalClass).getContainingFile().getContainingDirectory());
            chooser.showDialog();
            PsiClass aClass = chooser.getSelected();
            if (aClass instanceof KtLightClassForSourceDeclaration) {
                targetClass = ((KtLightClassForSourceDeclaration) aClass).getKotlinOrigin();
                targetClassChooser.setText(aClass.getQualifiedName());
            }
        }
    }, initialTargetClass.getFqName().asString(), myProject, true, JavaCodeFragment.VisibilityChecker.PROJECT_SCOPE_VISIBLE, RECENTS_KEY);
    targetClassChooser.getChildComponent().getDocument().addDocumentListener(new DocumentAdapter() {

        @Override
        public void documentChanged(DocumentEvent e) {
            PsiClass aClass = JavaPsiFacade.getInstance(myProject).findClass(targetClassChooser.getText(), GlobalSearchScope.projectScope(myProject));
            targetClass = aClass instanceof KtLightClassForSourceDeclaration ? ((KtLightClassForSourceDeclaration) aClass).getKotlinOrigin() : null;
            validateButtons();
        }
    });
    targetClassChooserPanel.add(targetClassChooser);
}
Also used : KtClassOrObjectTreeNode(org.jetbrains.kotlin.idea.projectView.KtClassOrObjectTreeNode) TreeClassChooser(com.intellij.ide.util.TreeClassChooser) KtLightClassForSourceDeclaration(org.jetbrains.kotlin.asJava.classes.KtLightClassForSourceDeclaration) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) ActionEvent(java.awt.event.ActionEvent) PsiClass(com.intellij.psi.PsiClass) DocumentAdapter(com.intellij.openapi.editor.event.DocumentAdapter) DocumentEvent(com.intellij.openapi.editor.event.DocumentEvent) ReferenceEditorComboWithBrowseButton(com.intellij.ui.ReferenceEditorComboWithBrowseButton) TreeJavaClassChooserDialog(com.intellij.ide.util.TreeJavaClassChooserDialog) ActionListener(java.awt.event.ActionListener) ClassFilter(com.intellij.ide.util.ClassFilter)

Example 8 with ClassFilter

use of com.intellij.ide.util.ClassFilter in project android by JetBrains.

the class AndroidCreateOnClickHandlerAction method invoke.

@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
    final AndroidFacet facet = AndroidFacet.getInstance(file);
    assert facet != null;
    final XmlAttributeValue attrValue = getXmlAttributeValue(file, editor);
    assert attrValue != null;
    final String methodName = attrValue.getValue();
    assert methodName != null;
    final GenericAttributeValue domValue = DomManager.getDomManager(project).getDomElement((XmlAttribute) attrValue.getParent());
    assert domValue != null;
    final OnClickConverter converter = (OnClickConverter) domValue.getConverter();
    final PsiClass activityBaseClass = JavaPsiFacade.getInstance(project).findClass(AndroidUtils.ACTIVITY_BASE_CLASS_NAME, facet.getModule().getModuleWithDependenciesAndLibrariesScope(false));
    if (activityBaseClass == null) {
        return;
    }
    final GlobalSearchScope scope = facet.getModule().getModuleScope(false);
    final PsiClass selectedClass;
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        final Ref<PsiClass> selClassRef = Ref.create();
        ClassInheritorsSearch.search(activityBaseClass, scope, true, true, false).forEach(new Processor<PsiClass>() {

            @Override
            public boolean process(PsiClass psiClass) {
                if (!psiClass.isInterface() && !psiClass.hasModifierProperty(PsiModifier.ABSTRACT)) {
                    selClassRef.set(psiClass);
                    return false;
                }
                return true;
            }
        });
        selectedClass = selClassRef.get();
    } else {
        final TreeClassChooser chooser = TreeClassChooserFactory.getInstance(project).createInheritanceClassChooser("Choose Activity to Create the Method", scope, activityBaseClass, null, new ClassFilter() {

            @Override
            public boolean isAccepted(PsiClass aClass) {
                return !converter.findHandlerMethod(aClass, methodName);
            }
        });
        chooser.showDialog();
        selectedClass = chooser.getSelected();
    }
    if (selectedClass != null) {
        addHandlerMethodAndNavigate(project, selectedClass, methodName, converter.getDefaultMethodParameterType(selectedClass));
    }
}
Also used : OnClickConverter(org.jetbrains.android.dom.converters.OnClickConverter) TreeClassChooser(com.intellij.ide.util.TreeClassChooser) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) ClassFilter(com.intellij.ide.util.ClassFilter) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) GenericAttributeValue(com.intellij.util.xml.GenericAttributeValue)

Example 9 with ClassFilter

use of com.intellij.ide.util.ClassFilter in project android by JetBrains.

the class AndroidClassBrowserBase method showDialog.

@Override
protected String showDialog() {
    Project project = getProject();
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
    Module module = myModuleSelector.getModule();
    if (module == null) {
        Messages.showErrorDialog(project, ExecutionBundle.message("module.not.specified.error.text"), CommonBundle.getErrorTitle());
        return null;
    }
    GlobalSearchScope scope = myIncludeLibraryClasses ? module.getModuleWithDependenciesAndLibrariesScope(true) : module.getModuleWithDependenciesScope();
    PsiClass initialSelection = facade.findClass(getText(), scope);
    TreeClassChooser chooser = createTreeClassChooser(project, scope, initialSelection, new ClassFilter() {

        @Override
        public boolean isAccepted(PsiClass aClass) {
            if (aClass.isInterface())
                return false;
            final PsiModifierList modifierList = aClass.getModifierList();
            return modifierList == null || !modifierList.hasModifierProperty(PsiModifier.ABSTRACT);
        }
    });
    if (chooser == null)
        return null;
    chooser.showDialog();
    PsiClass selClass = chooser.getSelected();
    return selClass != null ? selClass.getQualifiedName() : null;
}
Also used : JavaPsiFacade(com.intellij.psi.JavaPsiFacade) Project(com.intellij.openapi.project.Project) TreeClassChooser(com.intellij.ide.util.TreeClassChooser) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) ClassFilter(com.intellij.ide.util.ClassFilter) PsiClass(com.intellij.psi.PsiClass) Module(com.intellij.openapi.module.Module) PsiModifierList(com.intellij.psi.PsiModifierList)

Aggregations

ClassFilter (com.intellij.ide.util.ClassFilter)9 TreeClassChooser (com.intellij.ide.util.TreeClassChooser)8 PsiClass (com.intellij.psi.PsiClass)6 Project (com.intellij.openapi.project.Project)4 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)4 TreeJavaClassChooserDialog (com.intellij.ide.util.TreeJavaClassChooserDialog)2 DataContext (com.intellij.openapi.actionSystem.DataContext)2 ActionEvent (java.awt.event.ActionEvent)2 ActionListener (java.awt.event.ActionListener)2 ListWrappingTableModel (com.intellij.codeInspection.ui.ListWrappingTableModel)1 TreeClassChooserFactory (com.intellij.ide.util.TreeClassChooserFactory)1 DocumentAdapter (com.intellij.openapi.editor.event.DocumentAdapter)1 DocumentEvent (com.intellij.openapi.editor.event.DocumentEvent)1 Module (com.intellij.openapi.module.Module)1 JavaPsiFacade (com.intellij.psi.JavaPsiFacade)1 PsiModifierList (com.intellij.psi.PsiModifierList)1 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)1 ReferenceEditorComboWithBrowseButton (com.intellij.ui.ReferenceEditorComboWithBrowseButton)1 JBList (com.intellij.ui.components.JBList)1 DomElement (com.intellij.util.xml.DomElement)1