Search in sources :

Example 21 with TreeClassChooser

use of com.intellij.ide.util.TreeClassChooser 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 22 with TreeClassChooser

use of com.intellij.ide.util.TreeClassChooser in project azure-tools-for-java by Microsoft.

the class ManifestFileUtilsEx method selectMainClass.

@Nullable
public PsiClass selectMainClass(@Nullable VirtualFile jarFile) {
    if (jarFile == null) {
        return null;
    }
    final TreeClassChooserFactory chooserFactory = TreeClassChooserFactory.getInstance(myProject);
    final GlobalSearchScope searchScope = GlobalSearchScope.everythingScope(myProject);
    // TODO: the following code is used to find initialClassName in the jar. When user specified initialClassName
    // in the main-class-selection textfield and clicked the main-class-selection button, the filter result in the dialog
    // should be the initialClass. Currently we don't enable this method since exception happens with the following code.
    // final PsiClass aClass = initialClassName != null ? JavaPsiFacade.getInstance(project).findClass(initialClassName, searchScope) : null;
    final TreeClassChooser chooser = chooserFactory.createWithInnerClassesScopeChooser("Select Main Class", searchScope, new MainClassFilter(jarFile.getPath()), null);
    ((TreeJavaClassChooserDialog) chooser).getWindow().addWindowListener(new WindowAdapter() {

        // These fields are recorded to help remove the artifact and the module.
        @Nullable
        private String localArtifactLibraryName;

        @Nullable
        private String localArtifactModuleName;

        @Override
        public void windowOpened(WindowEvent e) {
            // remove old jar and add new jar to project dependency
            WriteAction.run(() -> addJarToModuleDependency(jarFile));
            super.windowOpened(e);
        }

        @Override
        public void windowClosed(WindowEvent e) {
            WriteAction.run(() -> removeModuleAndJar());
            super.windowClosed(e);
        }

        private void addJarToModuleDependency(@NotNull VirtualFile jarFile) {
            try {
                final Module module = createJarPackingModule();
                final List<OrderRoot> myRoots = RootDetectionUtil.detectRoots(Arrays.asList(jarFile), null, myProject, new DefaultLibraryRootsComponentDescriptor());
                final ModifiableRootModel modifiableModel = ModuleRootManager.getInstance(module).getModifiableModel();
                // find a unique library name in the module
                final String libraryName = LibraryEditingUtil.suggestNewLibraryName(modifiableModel.getModuleLibraryTable().getModifiableModel(), jarFile.getName());
                // add library to the model of the new-created module
                LibrariesContainerFactory.createContainer(modifiableModel).createLibrary(libraryName, LibrariesContainer.LibraryLevel.MODULE, myRoots);
                modifiableModel.commit();
                localArtifactModuleName = module.getName();
                localArtifactLibraryName = libraryName;
            } catch (Exception ex) {
                log().warn(String.format("Failed to add the user selected jar(%s) into module dependency: %s", jarFile.getPath(), ex.toString()));
            }
        }

        private void removeModuleAndJar() {
            assert (localArtifactLibraryName != null && localArtifactModuleName != null) : String.format("Can't get module name or library name. module:%s, library:%s", localArtifactModuleName, localArtifactLibraryName);
            try {
                final Module module = ModuleManager.getInstance(myProject).findModuleByName(localArtifactModuleName);
                // remove library from the model of the module
                final ModifiableRootModel modifiableModel = ModuleRootManager.getInstance(module).getModifiableModel();
                modifiableModel.getModuleLibraryTable().removeLibrary(modifiableModel.getModuleLibraryTable().getLibraryByName(localArtifactLibraryName));
                modifiableModel.commit();
                // remove module from project
                ModuleManager.getInstance(myProject).disposeModule(module);
                localArtifactModuleName = null;
                localArtifactLibraryName = null;
            } catch (Exception ex) {
                log().warn(String.format("Failed to remove jar(%s) from module(%s): %s", localArtifactLibraryName, localArtifactModuleName, ex.toString()));
            }
        }
    });
    chooser.showDialog();
    return chooser.getSelected();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) TreeClassChooser(com.intellij.ide.util.TreeClassChooser) DefaultLibraryRootsComponentDescriptor(com.intellij.openapi.roots.ui.configuration.libraryEditor.DefaultLibraryRootsComponentDescriptor) WindowAdapter(java.awt.event.WindowAdapter) ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) TreeClassChooserFactory(com.intellij.ide.util.TreeClassChooserFactory) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) WindowEvent(java.awt.event.WindowEvent) List(java.util.List) Module(com.intellij.openapi.module.Module) Nullable(com.microsoft.azuretools.azurecommons.helpers.Nullable) Nullable(com.microsoft.azuretools.azurecommons.helpers.Nullable)

Aggregations

TreeClassChooser (com.intellij.ide.util.TreeClassChooser)22 PsiClass (com.intellij.psi.PsiClass)12 ClassFilter (com.intellij.ide.util.ClassFilter)10 ActionEvent (java.awt.event.ActionEvent)7 ActionListener (java.awt.event.ActionListener)7 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)6 Project (com.intellij.openapi.project.Project)4 DocumentEvent (javax.swing.event.DocumentEvent)4 Nullable (org.jetbrains.annotations.Nullable)4 TreeClassChooserFactory (com.intellij.ide.util.TreeClassChooserFactory)3 DocumentAdapter (com.intellij.ui.DocumentAdapter)3 List (java.util.List)3 MemberChooser (com.intellij.ide.util.MemberChooser)2 DataContext (com.intellij.openapi.actionSystem.DataContext)2 Module (com.intellij.openapi.module.Module)2 JavaPsiFacade (com.intellij.psi.JavaPsiFacade)2 PsiModifierList (com.intellij.psi.PsiModifierList)2 DestinationFolderComboBox (com.intellij.refactoring.move.moveClassesOrPackages.DestinationFolderComboBox)2 PackageNameReferenceEditorCombo (com.intellij.refactoring.ui.PackageNameReferenceEditorCombo)2 ReferenceEditorComboWithBrowseButton (com.intellij.ui.ReferenceEditorComboWithBrowseButton)2