Search in sources :

Example 16 with IncorrectOperationException

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

the class JavaI18nizeQuickFixDialog method isAvailable.

public static boolean isAvailable(PsiFile file) {
    final Project project = file.getProject();
    final String title = CodeInsightBundle.message("i18nize.dialog.error.jdk.title");
    try {
        return ResourceBundleManager.getManager(file) != null;
    } catch (ResourceBundleManager.ResourceBundleNotFoundException e) {
        final IntentionAction fix = e.getFix();
        if (fix != null) {
            if (Messages.showOkCancelDialog(project, e.getMessage(), title, Messages.getErrorIcon()) == Messages.OK) {
                try {
                    fix.invoke(project, null, file);
                    return false;
                } catch (IncorrectOperationException e1) {
                    LOG.error(e1);
                }
            }
        }
        Messages.showErrorDialog(project, e.getMessage(), title);
        return false;
    }
}
Also used : Project(com.intellij.openapi.project.Project) ResourceBundleManager(com.intellij.lang.properties.psi.ResourceBundleManager) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 17 with IncorrectOperationException

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

the class AnnotateCapitalizationIntention method invoke.

@Override
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException {
    final PsiModifierListOwner modifierListOwner = getElement(editor, file);
    if (modifierListOwner == null)
        throw new IncorrectOperationException();
    BaseListPopupStep<Nls.Capitalization> step = new BaseListPopupStep<Nls.Capitalization>(null, Nls.Capitalization.Title, Nls.Capitalization.Sentence) {

        @Override
        public PopupStep onChosen(final Nls.Capitalization selectedValue, boolean finalChoice) {
            new WriteCommandAction.Simple(project) {

                @Override
                protected void run() throws Throwable {
                    String nls = Nls.class.getName();
                    PsiAnnotation annotation = JavaPsiFacade.getInstance(project).getElementFactory().createAnnotationFromText("@" + nls + "(capitalization = " + nls + ".Capitalization." + selectedValue.toString() + ")", modifierListOwner);
                    new AddAnnotationFix(Nls.class.getName(), modifierListOwner, annotation.getParameterList().getAttributes()).applyFix();
                }
            }.execute();
            return FINAL_CHOICE;
        }
    };
    JBPopupFactory.getInstance().createListPopup(step).showInBestPositionFor(editor);
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Nls(org.jetbrains.annotations.Nls) BaseListPopupStep(com.intellij.openapi.ui.popup.util.BaseListPopupStep) IncorrectOperationException(com.intellij.util.IncorrectOperationException) AddAnnotationFix(com.intellij.codeInsight.intention.AddAnnotationFix)

Example 18 with IncorrectOperationException

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

the class I18nizeAction method doI18nSelectedString.

public static void doI18nSelectedString(@NotNull final Project project, @NotNull final Editor editor, @NotNull final PsiFile psiFile, @NotNull final I18nQuickFixHandler handler) {
    try {
        handler.checkApplicability(psiFile, editor);
    } catch (IncorrectOperationException ex) {
        CommonRefactoringUtil.showErrorHint(project, editor, ex.getMessage(), CodeInsightBundle.message("i18nize.error.title"), null);
        return;
    }
    if (!ApplicationManager.getApplication().isUnitTestMode()) {
        JavaI18nizeQuickFixDialog.isAvailable(psiFile);
    }
    try {
        ResourceBundleManager.getManager(psiFile);
    } catch (ResourceBundleManager.ResourceBundleNotFoundException e) {
        return;
    }
    final JavaI18nizeQuickFixDialog dialog = handler.createDialog(project, editor, psiFile);
    if (dialog == null)
        return;
    if (!dialog.showAndGet()) {
        return;
    }
    if (!FileModificationService.getInstance().prepareFileForWrite(psiFile))
        return;
    final Collection<PropertiesFile> propertiesFiles = dialog.getAllPropertiesFiles();
    for (PropertiesFile file : propertiesFiles) {
        if (!FileModificationService.getInstance().prepareFileForWrite(file.getContainingFile()))
            return;
    }
    ApplicationManager.getApplication().runWriteAction(() -> CommandProcessor.getInstance().executeCommand(project, () -> {
        try {
            handler.performI18nization(psiFile, editor, dialog.getLiteralExpression(), propertiesFiles, dialog.getKey(), StringUtil.unescapeStringCharacters(dialog.getValue()), dialog.getI18nizedText(), dialog.getParameters(), dialog.getPropertyCreationHandler());
        } catch (IncorrectOperationException e) {
            LOG.error(e);
        }
    }, CodeInsightBundle.message("quickfix.i18n.command.name"), project));
}
Also used : ResourceBundleManager(com.intellij.lang.properties.psi.ResourceBundleManager) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile)

Example 19 with IncorrectOperationException

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

the class I18nizeQuickFix method performI18nization.

@Override
public void performI18nization(final PsiFile psiFile, final Editor editor, PsiLiteralExpression literalExpression, Collection<PropertiesFile> propertiesFiles, String key, String value, String i18nizedText, PsiExpression[] parameters, final PropertyCreationHandler propertyCreationHandler) throws IncorrectOperationException {
    Project project = psiFile.getProject();
    propertyCreationHandler.createProperty(project, propertiesFiles, key, value, parameters);
    try {
        final PsiElement newExpression = doReplacementInJava(psiFile, editor, literalExpression, i18nizedText);
        reformatAndCorrectReferences(newExpression);
    } catch (IncorrectOperationException e) {
        Messages.showErrorDialog(project, CodeInsightBundle.message("inspection.i18n.expression.is.invalid.error.message"), CodeInsightBundle.message("inspection.error.dialog.title"));
    }
}
Also used : Project(com.intellij.openapi.project.Project) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 20 with IncorrectOperationException

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

the class FormSourceCodeGenerator method generate.

public void generate(final VirtualFile formFile) {
    myNeedLoadLabelText = false;
    myNeedLoadButtonText = false;
    final Module module = ModuleUtil.findModuleForFile(formFile, myProject);
    if (module == null) {
        return;
    }
    // ensure that new instances of generators are used for every run
    ourContainerLayoutCodeGenerators.clear();
    ourContainerLayoutCodeGenerators.put(UIFormXmlConstants.LAYOUT_INTELLIJ, new GridLayoutSourceGenerator());
    ourContainerLayoutCodeGenerators.put(UIFormXmlConstants.LAYOUT_GRIDBAG, new GridBagLayoutSourceGenerator());
    ourContainerLayoutCodeGenerators.put(UIFormXmlConstants.LAYOUT_BORDER, new BorderLayoutSourceGenerator());
    ourContainerLayoutCodeGenerators.put(UIFormXmlConstants.LAYOUT_FLOW, new FlowLayoutSourceGenerator());
    ourContainerLayoutCodeGenerators.put(UIFormXmlConstants.LAYOUT_CARD, new CardLayoutSourceGenerator());
    ourContainerLayoutCodeGenerators.put(UIFormXmlConstants.LAYOUT_FORM, new FormLayoutSourceGenerator());
    myErrors.clear();
    final PsiPropertiesProvider propertiesProvider = new PsiPropertiesProvider(module);
    final Document doc = FileDocumentManager.getInstance().getDocument(formFile);
    final LwRootContainer rootContainer;
    try {
        rootContainer = Utils.getRootContainer(doc.getText(), propertiesProvider);
    } catch (AlienFormFileException ignored) {
        // ignoring this file
        return;
    } catch (Exception e) {
        myErrors.add(new FormErrorInfo(null, UIDesignerBundle.message("error.cannot.process.form.file", e)));
        return;
    }
    if (rootContainer.getClassToBind() == null) {
        // form skipped - no class to bind
        return;
    }
    ErrorAnalyzer.analyzeErrors(module, formFile, null, rootContainer, null);
    FormEditingUtil.iterate(rootContainer, new FormEditingUtil.ComponentVisitor<LwComponent>() {

        public boolean visit(final LwComponent iComponent) {
            final ErrorInfo errorInfo = ErrorAnalyzer.getErrorForComponent(iComponent);
            if (errorInfo != null) {
                String message;
                if (iComponent.getBinding() != null) {
                    message = UIDesignerBundle.message("error.for.component", iComponent.getBinding(), errorInfo.myDescription);
                } else {
                    message = errorInfo.myDescription;
                }
                myErrors.add(new FormErrorInfo(iComponent.getId(), message));
            }
            return true;
        }
    });
    if (myErrors.size() != 0) {
        return;
    }
    try {
        _generate(rootContainer, module);
    } catch (ClassToBindNotFoundException e) {
    // ignore
    } catch (CodeGenerationException e) {
        myErrors.add(new FormErrorInfo(e.getComponentId(), e.getMessage()));
    } catch (IncorrectOperationException e) {
        myErrors.add(new FormErrorInfo(null, e.getMessage()));
    }
}
Also used : Document(com.intellij.openapi.editor.Document) IncorrectOperationException(com.intellij.util.IncorrectOperationException) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Module(com.intellij.openapi.module.Module)

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