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