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