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;
}
}
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);
}
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));
}
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"));
}
}
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()));
}
}
Aggregations