Search in sources :

Example 61 with IncorrectOperationException

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

the class BindingEditor method getFieldNames.

private static String[] getFieldNames(final RadComponent component, final String currentName) {
    final ArrayList<String> result = new ArrayList<>();
    if (currentName != null) {
        result.add(currentName);
    }
    final IRootContainer root = FormEditingUtil.getRoot(component);
    final String className = root.getClassToBind();
    if (className == null) {
        return ArrayUtil.toStringArray(result);
    }
    final PsiClass aClass = FormEditingUtil.findClassToBind(component.getModule(), className);
    if (aClass == null) {
        return ArrayUtil.toStringArray(result);
    }
    final PsiField[] fields = aClass.getFields();
    for (final PsiField field : fields) {
        if (field.hasModifierProperty(PsiModifier.STATIC)) {
            continue;
        }
        final String fieldName = field.getName();
        if (Comparing.equal(currentName, fieldName)) {
            continue;
        }
        if (!FormEditingUtil.isBindingUnique(component, fieldName, root)) {
            continue;
        }
        final String componentClassName;
        if (component instanceof RadErrorComponent) {
            componentClassName = component.getComponentClassName();
        } else if (component instanceof RadHSpacer || component instanceof RadVSpacer) {
            componentClassName = Spacer.class.getName();
        } else {
            componentClassName = component.getComponentClass().getName();
        }
        final PsiType componentType;
        try {
            componentType = JavaPsiFacade.getInstance(component.getProject()).getElementFactory().createTypeFromText(componentClassName, null);
        } catch (IncorrectOperationException e) {
            continue;
        }
        final PsiType fieldType = field.getType();
        if (!fieldType.isAssignableFrom(componentType)) {
            continue;
        }
        result.add(fieldName);
    }
    String text = FormInspectionUtil.getText(component.getModule(), component);
    if (text != null) {
        String binding = BindingProperty.suggestBindingFromText(component, text);
        if (binding != null && !result.contains(binding)) {
            result.add(binding);
        }
    }
    final String[] names = ArrayUtil.toStringArray(result);
    Arrays.sort(names);
    return names;
}
Also used : ArrayList(java.util.ArrayList) RadErrorComponent(com.intellij.uiDesigner.radComponents.RadErrorComponent) RadVSpacer(com.intellij.uiDesigner.radComponents.RadVSpacer) IRootContainer(com.intellij.uiDesigner.lw.IRootContainer) IncorrectOperationException(com.intellij.util.IncorrectOperationException) RadHSpacer(com.intellij.uiDesigner.radComponents.RadHSpacer)

Example 62 with IncorrectOperationException

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

the class CreateClassToBindFix method createBoundFields.

private void createBoundFields(final PsiClass formClass) throws IncorrectOperationException {
    final Module module = myEditor.getRootContainer().getModule();
    final GlobalSearchScope scope = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module);
    final PsiManager psiManager = PsiManager.getInstance(myEditor.getProject());
    final Ref<IncorrectOperationException> exception = new Ref<>();
    FormEditingUtil.iterate(myEditor.getRootContainer(), new FormEditingUtil.ComponentVisitor() {

        public boolean visit(final IComponent component) {
            if (component.getBinding() != null) {
                final PsiClass fieldClass = JavaPsiFacade.getInstance(psiManager.getProject()).findClass(component.getComponentClassName(), scope);
                if (fieldClass != null) {
                    PsiType fieldType = JavaPsiFacade.getInstance(psiManager.getProject()).getElementFactory().createType(fieldClass);
                    try {
                        PsiField field = JavaPsiFacade.getInstance(psiManager.getProject()).getElementFactory().createField(component.getBinding(), fieldType);
                        formClass.add(field);
                    } catch (IncorrectOperationException e) {
                        exception.set(e);
                        return false;
                    }
                }
            }
            return true;
        }
    });
    if (!exception.isNull()) {
        throw exception.get();
    }
}
Also used : Ref(com.intellij.openapi.util.Ref) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) IComponent(com.intellij.uiDesigner.lw.IComponent) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Module(com.intellij.openapi.module.Module) FormEditingUtil(com.intellij.uiDesigner.FormEditingUtil)

Example 63 with IncorrectOperationException

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

the class CreateClassToBindFix method run.

public void run() {
    final Project project = myEditor.getProject();
    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
    final VirtualFile sourceRoot = fileIndex.getSourceRootForFile(myEditor.getFile());
    if (sourceRoot == null) {
        Messages.showErrorDialog(myEditor, UIDesignerBundle.message("error.cannot.create.class.not.in.source.root"), CommonBundle.getErrorTitle());
        return;
    }
    ApplicationManager.getApplication().runWriteAction(() -> CommandProcessor.getInstance().executeCommand(project, () -> {
        // 1. Create all necessary packages
        final int indexOfLastDot = myClassName.lastIndexOf('.');
        final String packageName = myClassName.substring(0, indexOfLastDot != -1 ? indexOfLastDot : 0);
        final PsiDirectory psiDirectory;
        if (packageName.length() > 0) {
            final PackageWrapper packageWrapper = new PackageWrapper(PsiManager.getInstance(project), packageName);
            try {
                psiDirectory = RefactoringUtil.createPackageDirectoryInSourceRoot(packageWrapper, sourceRoot);
                LOG.assertTrue(psiDirectory != null);
            } catch (final IncorrectOperationException e) {
                ApplicationManager.getApplication().invokeLater(() -> Messages.showErrorDialog(myEditor, UIDesignerBundle.message("error.cannot.create.package", packageName, e.getMessage()), CommonBundle.getErrorTitle()));
                return;
            }
        } else {
            psiDirectory = PsiManager.getInstance(project).findDirectory(sourceRoot);
            LOG.assertTrue(psiDirectory != null);
        }
        // 2. Create class in the package
        try {
            final String name = myClassName.substring(indexOfLastDot != -1 ? indexOfLastDot + 1 : 0);
            final PsiClass aClass = JavaDirectoryService.getInstance().createClass(psiDirectory, name);
            createBoundFields(aClass);
        } catch (final IncorrectOperationException e) {
            ApplicationManager.getApplication().invokeLater(() -> Messages.showErrorDialog(myEditor, UIDesignerBundle.message("error.cannot.create.class", packageName, e.getMessage()), CommonBundle.getErrorTitle()));
        }
    }, getName(), null));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PackageWrapper(com.intellij.refactoring.PackageWrapper)

Example 64 with IncorrectOperationException

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

the class ExtendedTagInsertHandler method handleInsert.

@Override
public void handleInsert(final InsertionContext context, final LookupElement item) {
    final XmlFile contextFile = (XmlFile) context.getFile();
    final XmlExtension extension = XmlExtension.getExtension(contextFile);
    final XmlFile file = extension.getContainingFile(contextFile);
    final Project project = context.getProject();
    assert file != null;
    final PsiElement psiElement = file.findElementAt(context.getStartOffset());
    assert psiElement != null;
    if (isNamespaceBound(psiElement)) {
        doDefault(context, item);
        return;
    }
    final Editor editor = context.getEditor();
    final Document document = editor.getDocument();
    PsiDocumentManager.getInstance(project).commitDocument(document);
    final int caretOffset = editor.getCaretModel().getOffset();
    final RangeMarker caretMarker = document.createRangeMarker(caretOffset, caretOffset);
    caretMarker.setGreedyToRight(true);
    final XmlNamespaceHelper.Runner<String, IncorrectOperationException> runAfter = new XmlNamespaceHelper.Runner<String, IncorrectOperationException>() {

        @Override
        public void run(final String namespacePrefix) {
            PsiDocumentManager.getInstance(project).commitDocument(document);
            final PsiElement element = file.findElementAt(context.getStartOffset());
            if (element != null) {
                qualifyWithPrefix(namespacePrefix, element, document);
                PsiDocumentManager.getInstance(project).commitDocument(document);
            }
            editor.getCaretModel().moveToOffset(caretMarker.getEndOffset());
            PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(document);
            doDefault(context, item);
        }
    };
    try {
        final String prefixByNamespace = getPrefixByNamespace(file, myNamespace);
        if (myNamespacePrefix != null || StringUtil.isEmpty(prefixByNamespace)) {
            final String nsPrefix = myNamespacePrefix == null ? suggestPrefix(file, myNamespace) : myNamespacePrefix;
            XmlNamespaceHelper.getHelper(file).insertNamespaceDeclaration(file, editor, Collections.singleton(myNamespace), nsPrefix, runAfter);
            FeatureUsageTracker.getInstance().triggerFeatureUsed(XmlCompletionContributor.TAG_NAME_COMPLETION_FEATURE);
        } else {
            // qualify && complete
            runAfter.run(prefixByNamespace);
        }
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) XmlNamespaceHelper(com.intellij.xml.XmlNamespaceHelper) RangeMarker(com.intellij.openapi.editor.RangeMarker) Document(com.intellij.openapi.editor.Document) Project(com.intellij.openapi.project.Project) XmlExtension(com.intellij.xml.XmlExtension) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement)

Example 65 with IncorrectOperationException

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

the class CreateNSDeclarationIntentionFix method invoke.

@Override
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException {
    final PsiElement element = myElement.retrieve();
    if (element == null)
        return;
    XmlFile xmlFile = getFile();
    final String[] namespaces = ArrayUtil.toStringArray(getNamespaces(element, xmlFile));
    runActionOverSeveralAttributeValuesAfterLettingUserSelectTheNeededOne(namespaces, project, new StringToAttributeProcessor() {

        @Override
        public void doSomethingWithGivenStringToProduceXmlAttributeNowPlease(@NotNull final String namespace) throws IncorrectOperationException {
            String prefix = myNamespacePrefix;
            if (StringUtil.isEmpty(prefix)) {
                final XmlFile xmlFile = XmlExtension.getExtension(file).getContainingFile(element);
                prefix = ExtendedTagInsertHandler.getPrefixByNamespace(xmlFile, namespace);
                if (StringUtil.isNotEmpty(prefix)) {
                    // namespace already declared
                    ExtendedTagInsertHandler.qualifyWithPrefix(prefix, element);
                    return;
                } else {
                    prefix = ExtendedTagInsertHandler.suggestPrefix(xmlFile, namespace);
                    if (!StringUtil.isEmpty(prefix)) {
                        ExtendedTagInsertHandler.qualifyWithPrefix(prefix, element);
                        PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
                    }
                }
            }
            final int offset = editor.getCaretModel().getOffset();
            final RangeMarker marker = editor.getDocument().createRangeMarker(offset, offset);
            final XmlNamespaceHelper helper = XmlNamespaceHelper.getHelper(file);
            helper.insertNamespaceDeclaration((XmlFile) file, editor, Collections.singleton(namespace), prefix, new XmlNamespaceHelper.Runner<String, IncorrectOperationException>() {

                @Override
                public void run(final String param) throws IncorrectOperationException {
                    if (!namespace.isEmpty()) {
                        editor.getCaretModel().moveToOffset(marker.getStartOffset());
                    }
                }
            });
        }
    }, getTitle(), this, editor);
}
Also used : XmlNamespaceHelper(com.intellij.xml.XmlNamespaceHelper) IncorrectOperationException(com.intellij.util.IncorrectOperationException) RangeMarker(com.intellij.openapi.editor.RangeMarker) PsiElement(com.intellij.psi.PsiElement)

Aggregations

IncorrectOperationException (com.intellij.util.IncorrectOperationException)485 Project (com.intellij.openapi.project.Project)91 NotNull (org.jetbrains.annotations.NotNull)91 Nullable (org.jetbrains.annotations.Nullable)54 PsiElement (com.intellij.psi.PsiElement)50 TextRange (com.intellij.openapi.util.TextRange)43 VirtualFile (com.intellij.openapi.vfs.VirtualFile)38 Document (com.intellij.openapi.editor.Document)37 ASTNode (com.intellij.lang.ASTNode)35 PsiFile (com.intellij.psi.PsiFile)35 Editor (com.intellij.openapi.editor.Editor)33 NonNls (org.jetbrains.annotations.NonNls)32 UsageInfo (com.intellij.usageView.UsageInfo)31 ArrayList (java.util.ArrayList)31 JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)24 IOException (java.io.IOException)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