Search in sources :

Example 31 with IncorrectOperationException

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

the class CreateSnapShotAction method runSnapShooterSession.

private static void runSnapShooterSession(final SnapShotClient client, final Project project, final PsiDirectory dir, final IdeView view) {
    try {
        client.suspendSwing();
    } catch (IOException e1) {
        Messages.showMessageDialog(project, UIDesignerBundle.message("snapshot.connection.error"), UIDesignerBundle.message("snapshot.title"), Messages.getInformationIcon());
        return;
    }
    final MyDialog dlg = new MyDialog(project, client, dir);
    dlg.show();
    if (dlg.getExitCode() == DialogWrapper.OK_EXIT_CODE) {
        final int id = dlg.getSelectedComponentId();
        final Ref<Object> result = new Ref<>();
        ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> {
            try {
                result.set(client.createSnapshot(id));
            } catch (Exception ex) {
                result.set(ex);
            }
        }, UIDesignerBundle.message("progress.creating.snapshot"), false, project);
        String snapshot = null;
        if (result.get() instanceof String) {
            snapshot = (String) result.get();
        } else {
            Exception ex = (Exception) result.get();
            Messages.showMessageDialog(project, UIDesignerBundle.message("snapshot.create.error", ex.getMessage()), UIDesignerBundle.message("snapshot.title"), Messages.getErrorIcon());
        }
        if (snapshot != null) {
            final String snapshot1 = snapshot;
            ApplicationManager.getApplication().runWriteAction(() -> CommandProcessor.getInstance().executeCommand(project, () -> {
                try {
                    PsiFile formFile = PsiFileFactory.getInstance(dir.getProject()).createFileFromText(dlg.getFormName() + GuiFormFileType.DOT_DEFAULT_EXTENSION, snapshot1);
                    formFile = (PsiFile) dir.add(formFile);
                    formFile.getVirtualFile().setCharset(CharsetToolkit.UTF8_CHARSET);
                    formFile.getViewProvider().getDocument().setText(snapshot1);
                    view.selectElement(formFile);
                } catch (IncorrectOperationException ex) {
                    Messages.showMessageDialog(project, UIDesignerBundle.message("snapshot.save.error", ex.getMessage()), UIDesignerBundle.message("snapshot.title"), Messages.getErrorIcon());
                }
            }, "", null));
        }
    }
    try {
        client.resumeSwing();
    } catch (IOException ex) {
        Messages.showErrorDialog(project, UIDesignerBundle.message("snapshot.connection.broken"), UIDesignerBundle.message("snapshot.title"));
    }
    client.dispose();
}
Also used : Ref(com.intellij.openapi.util.Ref) IncorrectOperationException(com.intellij.util.IncorrectOperationException) IOException(java.io.IOException) IncorrectOperationException(com.intellij.util.IncorrectOperationException) ExecutionException(com.intellij.execution.ExecutionException) IOException(java.io.IOException)

Example 32 with IncorrectOperationException

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

the class Generator method createBeanClass.

@NotNull
private static PsiClass createBeanClass(final WizardData wizardData) throws MyException {
    final PsiManager psiManager = PsiManager.getInstance(wizardData.myProject);
    final ProjectRootManager projectRootManager = ProjectRootManager.getInstance(wizardData.myProject);
    final ProjectFileIndex fileIndex = projectRootManager.getFileIndex();
    final VirtualFile sourceRoot = fileIndex.getSourceRootForFile(wizardData.myFormFile);
    if (sourceRoot == null) {
        throw new MyException(UIDesignerBundle.message("error.form.file.is.not.in.source.root"));
    }
    final PsiDirectory rootDirectory = psiManager.findDirectory(sourceRoot);
    LOG.assertTrue(rootDirectory != null);
    final PsiPackage aPackage = JavaPsiFacade.getInstance(psiManager.getProject()).findPackage(wizardData.myPackageName);
    if (aPackage == null) {
        throw new MyException(UIDesignerBundle.message("error.package.does.not.exist", wizardData.myPackageName));
    }
    PsiDirectory targetDir = null;
    final PsiDirectory[] directories = aPackage.getDirectories();
    for (final PsiDirectory psiDirectory : directories) {
        if (PsiTreeUtil.isAncestor(rootDirectory, psiDirectory, false)) {
            targetDir = psiDirectory;
            break;
        }
    }
    if (targetDir == null) {
        // todo
        throw new MyException(UIDesignerBundle.message("error.cannot.find.package", wizardData.myPackageName));
    }
    //noinspection HardCodedStringLiteral
    final String body = "public class " + wizardData.myShortClassName + "{\n" + "public " + wizardData.myShortClassName + "(){}\n" + "}";
    try {
        PsiFile sourceFile = PsiFileFactory.getInstance(psiManager.getProject()).createFileFromText(wizardData.myShortClassName + ".java", body);
        sourceFile = (PsiFile) targetDir.add(sourceFile);
        final PsiClass beanClass = ((PsiJavaFile) sourceFile).getClasses()[0];
        final ArrayList<String> properties = new ArrayList<>();
        final HashMap<String, String> property2fqClassName = new HashMap<>();
        final FormProperty2BeanProperty[] bindings = wizardData.myBindings;
        for (final FormProperty2BeanProperty binding : bindings) {
            if (binding == null || binding.myBeanProperty == null) {
                continue;
            }
            properties.add(binding.myBeanProperty.myName);
            // todo: handle "casts" ?
            final String propertyClassName = binding.myFormProperty.getComponentPropertyClassName();
            property2fqClassName.put(binding.myBeanProperty.myName, propertyClassName);
        }
        generateBean(beanClass, ArrayUtil.toStringArray(properties), property2fqClassName);
        return beanClass;
    } catch (IncorrectOperationException e) {
        throw new MyException(e.getMessage());
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) IncorrectOperationException(com.intellij.util.IncorrectOperationException) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager) NotNull(org.jetbrains.annotations.NotNull)

Example 33 with IncorrectOperationException

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

the class Generator method generateBean.

// todo: inline
private static void generateBean(final PsiClass aClass, final String[] properties, final HashMap<String, String> property2fqClassName) throws MyException {
    final StringBuffer membersBuffer = new StringBuffer();
    final StringBuffer methodsBuffer = new StringBuffer();
    final CodeStyleManager formatter = CodeStyleManager.getInstance(aClass.getProject());
    final JavaCodeStyleManager styler = JavaCodeStyleManager.getInstance(aClass.getProject());
    for (final String property : properties) {
        LOG.assertTrue(property != null);
        final String type = property2fqClassName.get(property);
        LOG.assertTrue(type != null);
        generateProperty(styler, property, type, membersBuffer, methodsBuffer);
    }
    final PsiClass fakeClass;
    try {
        fakeClass = JavaPsiFacade.getInstance(aClass.getProject()).getElementFactory().createClassFromText(membersBuffer.toString() + methodsBuffer.toString(), null);
        final PsiField[] fields = fakeClass.getFields();
        for (final PsiField field : fields) {
            aClass.add(field);
        }
        final PsiMethod[] methods = fakeClass.getMethods();
        for (final PsiMethod method : methods) {
            aClass.add(method);
        }
        styler.shortenClassReferences(aClass);
        formatter.reformat(aClass);
    } catch (IncorrectOperationException e) {
        throw new MyException(e.getMessage());
    }
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 34 with IncorrectOperationException

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

the class FormEnumUsageTest method testEnumUsage.

public void testEnumUsage() throws IncorrectOperationException {
    LanguageLevelProjectExtension.getInstance(myJavaFacade.getProject()).setLanguageLevel(LanguageLevel.JDK_1_5);
    CommandProcessor.getInstance().executeCommand(myProject, () -> {
        try {
            createFile(myModule, myTestProjectRoot, "PropEnum.java", "public enum PropEnum { valueA, valueB }");
            createFile(myModule, myTestProjectRoot, "CustomComponent.java", "public class CustomComponent extends JLabel { private PropEnum e; public PropEnum getE() { return e; } public void setE(E newE) { e = newE; } }");
        } catch (Exception e) {
            fail(e.getMessage());
        }
    }, "", null);
    PsiClass enumClass = myJavaFacade.findClass("PropEnum", ProjectScope.getAllScope(myProject));
    PsiField valueBField = enumClass.findFieldByName("valueB", false);
    assertNotNull(valueBField);
    assertTrue(valueBField instanceof PsiEnumConstant);
    final PsiClass componentClass = myJavaFacade.findClass("CustomComponent", ProjectScope.getAllScope(myProject));
    assertNotNull(componentClass);
    assertEquals(1, ReferencesSearch.search(componentClass).findAll().size());
    assertEquals(1, ReferencesSearch.search(valueBField).findAll().size());
}
Also used : PsiEnumConstant(com.intellij.psi.PsiEnumConstant) PsiField(com.intellij.psi.PsiField) PsiClass(com.intellij.psi.PsiClass) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 35 with IncorrectOperationException

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

the class PyDecoratorImpl method setName.

public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException {
    final ASTNode node = getNode();
    final ASTNode nameNode = node.findChildByType(PyTokenTypes.IDENTIFIER);
    if (nameNode != null) {
        final ASTNode nameElement = PyUtil.createNewName(this, name);
        node.replaceChild(nameNode, nameElement);
        return this;
    } else {
        throw new IncorrectOperationException("No name node");
    }
}
Also used : ASTNode(com.intellij.lang.ASTNode) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Aggregations

IncorrectOperationException (com.intellij.util.IncorrectOperationException)494 Project (com.intellij.openapi.project.Project)91 NotNull (org.jetbrains.annotations.NotNull)91 PsiElement (com.intellij.psi.PsiElement)55 Nullable (org.jetbrains.annotations.Nullable)55 TextRange (com.intellij.openapi.util.TextRange)43 VirtualFile (com.intellij.openapi.vfs.VirtualFile)39 Document (com.intellij.openapi.editor.Document)38 PsiFile (com.intellij.psi.PsiFile)38 ASTNode (com.intellij.lang.ASTNode)35 Editor (com.intellij.openapi.editor.Editor)33 ArrayList (java.util.ArrayList)32 NonNls (org.jetbrains.annotations.NonNls)32 UsageInfo (com.intellij.usageView.UsageInfo)31 IOException (java.io.IOException)27 JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)24 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)21 XmlTag (com.intellij.psi.xml.XmlTag)20 CodeStyleManager (com.intellij.psi.codeStyle.CodeStyleManager)19 Module (com.intellij.openapi.module.Module)18