Search in sources :

Example 11 with IdeView

use of com.intellij.ide.IdeView 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 12 with IdeView

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

the class GeneratePluginClassAction method update.

public void update(final AnActionEvent e) {
    super.update(e);
    final Presentation presentation = e.getPresentation();
    if (presentation.isEnabled()) {
        final Project project = e.getProject();
        final Module module = e.getData(LangDataKeys.MODULE);
        if (project != null && module != null && PsiUtil.isPluginModule(module)) {
            final IdeView view = e.getData(LangDataKeys.IDE_VIEW);
            if (view != null) {
                // from com.intellij.ide.actions.CreateClassAction.update()
                ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex();
                PsiDirectory[] dirs = view.getDirectories();
                for (PsiDirectory dir : dirs) {
                    if (projectFileIndex.isUnderSourceRootOfType(dir.getVirtualFile(), JavaModuleSourceRootTypes.SOURCES) && JavaDirectoryService.getInstance().getPackage(dir) != null) {
                        return;
                    }
                }
            }
        }
        presentation.setEnabledAndVisible(false);
    }
}
Also used : Project(com.intellij.openapi.project.Project) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) PsiDirectory(com.intellij.psi.PsiDirectory) Presentation(com.intellij.openapi.actionSystem.Presentation) Module(com.intellij.openapi.module.Module) IdeView(com.intellij.ide.IdeView)

Example 13 with IdeView

use of com.intellij.ide.IdeView in project intellij-plugins by JetBrains.

the class NewActionScriptClassAction method isAvailable.

private boolean isAvailable(DataContext dataContext) {
    final Project project = CommonDataKeys.PROJECT.getData(dataContext);
    final IdeView view = LangDataKeys.IDE_VIEW.getData(dataContext);
    if (project == null || project.isDisposed() || view == null)
        return false;
    ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex();
    for (PsiDirectory dir : view.getDirectories()) {
        if (projectFileIndex.isInSourceContent(dir.getVirtualFile()) && DirectoryIndex.getInstance(dir.getProject()).getPackageName(dir.getVirtualFile()) != null) {
            Module module = ModuleUtilCore.findModuleForPsiElement(dir);
            if (module != null && isAvailableIn(module)) {
                return true;
            }
        }
    }
    return false;
}
Also used : Project(com.intellij.openapi.project.Project) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) PsiDirectory(com.intellij.psi.PsiDirectory) IdeView(com.intellij.ide.IdeView) Module(com.intellij.openapi.module.Module)

Example 14 with IdeView

use of com.intellij.ide.IdeView in project intellij-plugins by JetBrains.

the class NewActionScriptClassAction method actionPerformed.

public void actionPerformed(final AnActionEvent e) {
    final DataContext dataContext = e.getDataContext();
    final IdeView view = LangDataKeys.IDE_VIEW.getData(dataContext);
    if (view == null) {
        return;
    }
    final Project project = CommonDataKeys.PROJECT.getData(dataContext);
    final PsiDirectory dir = view.getOrChooseDirectory();
    if (dir == null || project == null)
        return;
    CommandProcessor.getInstance().executeCommand(project, () -> createAction(dir).execute(), getCommandName(), null);
}
Also used : Project(com.intellij.openapi.project.Project) PsiDirectory(com.intellij.psi.PsiDirectory) IdeView(com.intellij.ide.IdeView)

Example 15 with IdeView

use of com.intellij.ide.IdeView in project android by JetBrains.

the class CreateClassAction method isAvailable.

private boolean isAvailable(@NotNull DataContext dataContext) {
    Project project = CommonDataKeys.PROJECT.getData(dataContext);
    IdeView view = LangDataKeys.IDE_VIEW.getData(dataContext);
    if (project == null || view == null || view.getDirectories().length == 0) {
        return false;
    }
    ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex();
    for (PsiDirectory dir : view.getDirectories()) {
        if (projectFileIndex.isUnderSourceRootOfType(dir.getVirtualFile(), JavaModuleSourceRootTypes.SOURCES) && checkPackageExists(dir)) {
            return true;
        }
    }
    return false;
}
Also used : Project(com.intellij.openapi.project.Project) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) IdeView(com.intellij.ide.IdeView)

Aggregations

IdeView (com.intellij.ide.IdeView)47 Project (com.intellij.openapi.project.Project)35 PsiDirectory (com.intellij.psi.PsiDirectory)30 Module (com.intellij.openapi.module.Module)10 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)10 Course (com.jetbrains.edu.learning.courseFormat.Course)7 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)6 Presentation (com.intellij.openapi.actionSystem.Presentation)5 PsiElement (com.intellij.psi.PsiElement)5 Lesson (com.jetbrains.edu.learning.courseFormat.Lesson)5 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)4 NotNull (org.jetbrains.annotations.NotNull)4 DataContext (com.intellij.openapi.actionSystem.DataContext)3 Task (com.intellij.openapi.progress.Task)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 JavaDirectoryService (com.intellij.psi.JavaDirectoryService)3 AndroidProjectViewPane (com.android.tools.idea.navigator.AndroidProjectViewPane)2 StudioWizardDialogBuilder (com.android.tools.idea.ui.wizard.StudioWizardDialogBuilder)2 ModelWizard (com.android.tools.idea.wizard.model.ModelWizard)2 AbstractProjectViewPane (com.intellij.ide.projectView.impl.AbstractProjectViewPane)2