Search in sources :

Example 1 with PackageChooserDialog

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

the class GroovyDocAddPackageAction method actionPerformed.

@Override
public void actionPerformed(final AnActionEvent e) {
    final Project project = e.getProject();
    PackageChooserDialog chooser = new PackageChooserDialog("Choose packages", project);
    chooser.show();
    final List<PsiPackage> packages = chooser.getSelectedPackages();
    for (PsiPackage aPackage : packages) {
        final String qualifiedName = aPackage.getQualifiedName();
        if (qualifiedName.isEmpty()) {
            myDataModel.addElement(GroovyDocConfiguration.ALL_PACKAGES);
        }
        myDataModel.addElement(qualifiedName);
    }
}
Also used : Project(com.intellij.openapi.project.Project) PackageChooserDialog(com.intellij.ide.util.PackageChooserDialog) PsiPackage(com.intellij.psi.PsiPackage)

Example 2 with PackageChooserDialog

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

the class MoveInnerImpl method getTargetContainer.

/**
   * must be called in atomic action
   */
@Nullable
public static PsiElement getTargetContainer(PsiClass innerClass, final boolean chooseIfNotUnderSource) {
    final PsiClass outerClass = innerClass.getContainingClass();
    // Only inner classes allowed.
    assert outerClass != null;
    PsiElement outerClassParent = outerClass.getParent();
    while (outerClassParent != null) {
        if (outerClassParent instanceof PsiClass && !(outerClassParent instanceof PsiAnonymousClass)) {
            return outerClassParent;
        } else if (outerClassParent instanceof PsiFile) {
            final PsiDirectory directory = innerClass.getContainingFile().getContainingDirectory();
            final PsiPackage aPackage = JavaDirectoryService.getInstance().getPackage(directory);
            if (aPackage == null) {
                if (chooseIfNotUnderSource) {
                    PackageChooserDialog chooser = new PackageChooserDialog("Select Target Package", innerClass.getProject());
                    if (!chooser.showAndGet()) {
                        return null;
                    }
                    final PsiPackage chosenPackage = chooser.getSelectedPackage();
                    if (chosenPackage == null)
                        return null;
                    return chosenPackage.getDirectories()[0];
                }
                return null;
            }
            return directory;
        }
        outerClassParent = outerClassParent.getParent();
    }
    // should not happen
    LOG.assertTrue(false);
    return null;
}
Also used : PackageChooserDialog(com.intellij.ide.util.PackageChooserDialog) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with PackageChooserDialog

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

the class GenerateVisitorByHierarchyAction method actionPerformed.

public void actionPerformed(AnActionEvent e) {
    final Ref<String> visitorNameRef = Ref.create("MyVisitor");
    final Ref<PsiClass> parentClassRef = Ref.create(null);
    final Project project = e.getData(CommonDataKeys.PROJECT);
    assert project != null;
    final PsiNameHelper helper = PsiNameHelper.getInstance(project);
    final PackageChooserDialog dialog = new PackageChooserDialog("Choose Target Package and Hierarchy Root Class", project) {

        @Override
        protected ValidationInfo doValidate() {
            PsiDocumentManager.getInstance(project).commitAllDocuments();
            if (!helper.isQualifiedName(visitorNameRef.get())) {
                return new ValidationInfo("Visitor class name is not valid");
            } else if (parentClassRef.isNull()) {
                return new ValidationInfo("Hierarchy root class should be specified");
            } else if (parentClassRef.get().isAnnotationType() || parentClassRef.get().isEnum()) {
                return new ValidationInfo("Hierarchy root class should be an interface or a class");
            }
            return super.doValidate();
        }

        protected JComponent createCenterPanel() {
            final JPanel panel = new JPanel(new BorderLayout());
            panel.add(super.createCenterPanel(), BorderLayout.CENTER);
            panel.add(createNamePanel(), BorderLayout.NORTH);
            panel.add(createBaseClassPanel(), BorderLayout.SOUTH);
            return panel;
        }

        private JComponent createNamePanel() {
            LabeledComponent<JTextField> labeledComponent = new LabeledComponent<>();
            labeledComponent.setText("Visitor class");
            final JTextField nameField = new JTextField(visitorNameRef.get());
            labeledComponent.setComponent(nameField);
            nameField.getDocument().addDocumentListener(new DocumentAdapter() {

                protected void textChanged(final DocumentEvent e) {
                    visitorNameRef.set(nameField.getText());
                }
            });
            return labeledComponent;
        }

        private JComponent createBaseClassPanel() {
            LabeledComponent<EditorTextField> labeledComponent = new LabeledComponent<>();
            labeledComponent.setText("Hierarchy root class");
            final JavaCodeFragmentFactory factory = JavaCodeFragmentFactory.getInstance(project);
            final PsiTypeCodeFragment codeFragment = factory.createTypeCodeFragment("", null, true, JavaCodeFragmentFactory.ALLOW_VOID);
            final Document document = PsiDocumentManager.getInstance(project).getDocument(codeFragment);
            final EditorTextField editorTextField = new EditorTextField(document, project, StdFileTypes.JAVA);
            labeledComponent.setComponent(editorTextField);
            editorTextField.addDocumentListener(new com.intellij.openapi.editor.event.DocumentAdapter() {

                public void documentChanged(final com.intellij.openapi.editor.event.DocumentEvent e) {
                    parentClassRef.set(null);
                    try {
                        final PsiType psiType = codeFragment.getType();
                        final PsiClass psiClass = psiType instanceof PsiClassType ? ((PsiClassType) psiType).resolve() : null;
                        parentClassRef.set(psiClass);
                    } catch (PsiTypeCodeFragment.IncorrectTypeException e1) {
                    // ok
                    }
                }
            });
            return labeledComponent;
        }
    };
    final PsiElement element = CommonDataKeys.PSI_ELEMENT.getData(e.getDataContext());
    if (element instanceof PsiPackage) {
        dialog.selectPackage(((PsiPackage) element).getQualifiedName());
    } else if (element instanceof PsiDirectory) {
        final PsiPackage aPackage = JavaDirectoryService.getInstance().getPackage((PsiDirectory) element);
        if (aPackage != null) {
            dialog.selectPackage(aPackage.getQualifiedName());
        }
    }
    dialog.show();
    if (dialog.getExitCode() != DialogWrapper.OK_EXIT_CODE || dialog.getSelectedPackage() == null || dialog.getSelectedPackage().getQualifiedName().length() == 0 || parentClassRef.isNull()) {
        return;
    }
    final String visitorQName = generateEverything(dialog.getSelectedPackage(), parentClassRef.get(), visitorNameRef.get());
    final IdeView ideView = LangDataKeys.IDE_VIEW.getData(e.getDataContext());
    final PsiClass visitorClass = JavaPsiFacade.getInstance(project).findClass(visitorQName, GlobalSearchScope.projectScope(project));
    if (ideView != null && visitorClass != null) {
        ideView.selectElement(visitorClass);
    }
}
Also used : Document(com.intellij.openapi.editor.Document) IdeView(com.intellij.ide.IdeView) EditorTextField(com.intellij.ui.EditorTextField) DocumentAdapter(com.intellij.ui.DocumentAdapter) DocumentEvent(javax.swing.event.DocumentEvent) LabeledComponent(com.intellij.openapi.ui.LabeledComponent) Project(com.intellij.openapi.project.Project) ValidationInfo(com.intellij.openapi.ui.ValidationInfo) PackageChooserDialog(com.intellij.ide.util.PackageChooserDialog)

Example 4 with PackageChooserDialog

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

the class PackageBrowser method showDialog.

@Override
protected String showDialog() {
    PackageChooserDialog chooser = new PackageChooserDialog("Choose Package", getProject());
    chooser.show();
    PsiPackage psiPackage = chooser.getSelectedPackage();
    String packageName = psiPackage == null ? null : psiPackage.getQualifiedName();
    return packageName;
}
Also used : PackageChooserDialog(com.intellij.ide.util.PackageChooserDialog) PsiPackage(com.intellij.psi.PsiPackage)

Aggregations

PackageChooserDialog (com.intellij.ide.util.PackageChooserDialog)4 Project (com.intellij.openapi.project.Project)2 PsiPackage (com.intellij.psi.PsiPackage)2 IdeView (com.intellij.ide.IdeView)1 Document (com.intellij.openapi.editor.Document)1 LabeledComponent (com.intellij.openapi.ui.LabeledComponent)1 ValidationInfo (com.intellij.openapi.ui.ValidationInfo)1 DocumentAdapter (com.intellij.ui.DocumentAdapter)1 EditorTextField (com.intellij.ui.EditorTextField)1 DocumentEvent (javax.swing.event.DocumentEvent)1 Nullable (org.jetbrains.annotations.Nullable)1