Search in sources :

Example 11 with WriteCommandAction

use of com.intellij.openapi.command.WriteCommandAction in project intellij-community by JetBrains.

the class DomFileDescriptionTest method testChangeCustomDomness.

public void testChangeCustomDomness() throws Throwable {
    getDomManager().registerFileDescription(new DomFileDescription<MyElement>(MyElement.class, "xxx") {

        @Override
        public boolean isMyFile(@NotNull final XmlFile file, @Nullable final Module module) {
            return file.getText().contains("foo");
        }
    }, myDisposable);
    final XmlFile file = (XmlFile) createFile("xxx.xml", "<xxx zzz=\"foo\"><boy/><boy/><xxx/>");
    final MyElement boy = getDomManager().getFileElement(file, MyElement.class).getRootElement().getBoys().get(0);
    new WriteCommandAction(getProject()) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            file.getDocument().getRootTag().setAttribute("zzz", "bar");
        }
    }.execute();
    assertFalse(getDomManager().isDomFile(file));
    assertFalse(boy.isValid());
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) XmlFile(com.intellij.psi.xml.XmlFile) Module(com.intellij.openapi.module.Module) Result(com.intellij.openapi.application.Result)

Example 12 with WriteCommandAction

use of com.intellij.openapi.command.WriteCommandAction in project intellij-community by JetBrains.

the class DomBasicsTest method testCopyingFromNonEmptyToEmpty.

public void testCopyingFromNonEmptyToEmpty() throws Throwable {
    Module module = new MockModule(getTestRootDisposable());
    final MyElement element1 = getDomManager().createMockElement(MyElement.class, module, false);
    final MyElement element2 = getDomManager().createMockElement(MyElement.class, module, false);
    element2.ensureTagExists();
    assertNull(element2.getChild().getChild().getGenericValue().getStringValue());
    element1.getChild().getChild().getGenericValue().setStringValue("abc");
    new WriteCommandAction(getProject()) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            element2.copyFrom(element1);
        }
    }.execute();
    assertEquals("abc", element2.getChild().getChild().getGenericValue().getStringValue());
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) MockModule(com.intellij.mock.MockModule) MockModule(com.intellij.mock.MockModule) Module(com.intellij.openapi.module.Module) Result(com.intellij.openapi.application.Result)

Example 13 with WriteCommandAction

use of com.intellij.openapi.command.WriteCommandAction in project intellij-community by JetBrains.

the class XmlChooseColorIntentionAction method chooseColor.

public static void chooseColor(JComponent editorComponent, PsiElement element) {
    String caption = CodeInsightBundle.message("intention.color.chooser.dialog");
    final XmlAttributeValue literal = PsiTreeUtil.getParentOfType(element, XmlAttributeValue.class, false);
    if (literal == null)
        return;
    final String text = StringUtil.unquoteString(literal.getValue());
    Color oldColor;
    try {
        oldColor = Color.decode(text);
    } catch (NumberFormatException e) {
        oldColor = JBColor.GRAY;
    }
    Color color = ColorChooser.chooseColor(editorComponent, caption, oldColor, true);
    if (color == null)
        return;
    if (!Comparing.equal(color, oldColor)) {
        if (!FileModificationService.getInstance().preparePsiElementForWrite(element))
            return;
        final String newText = "#" + ColorUtil.toHex(color);
        final PsiManager manager = literal.getManager();
        final XmlAttribute newAttribute = XmlElementFactory.getInstance(manager.getProject()).createAttribute("name", newText, element);
        final Runnable replaceRunnable = () -> {
            final XmlAttributeValue valueElement = newAttribute.getValueElement();
            assert valueElement != null;
            literal.replace(valueElement);
        };
        new WriteCommandAction(element.getProject(), caption) {

            @Override
            protected void run(@NotNull Result result) throws Throwable {
                replaceRunnable.run();
            }
        }.execute();
    }
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) XmlAttribute(com.intellij.psi.xml.XmlAttribute) JBColor(com.intellij.ui.JBColor) PsiManager(com.intellij.psi.PsiManager) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) Result(com.intellij.openapi.application.Result)

Example 14 with WriteCommandAction

use of com.intellij.openapi.command.WriteCommandAction in project intellij-community by JetBrains.

the class MavenOpenOrCreateFilesAction method actionPerformed.

@Override
public void actionPerformed(@NotNull AnActionEvent e) {
    final Project project = MavenActionUtil.getProject(e.getDataContext());
    if (project == null)
        return;
    final List<File> files = getFiles(e);
    final List<VirtualFile> virtualFiles = collectVirtualFiles(files);
    if (files.size() == 1 && virtualFiles.isEmpty()) {
        new WriteCommandAction(project, e.getPresentation().getText()) {

            @Override
            protected void run(@NotNull Result result) throws Throwable {
                File file = files.get(0);
                try {
                    final VirtualFile virtualFile = VfsUtil.createDirectoryIfMissing(file.getParent());
                    if (virtualFile != null) {
                        VirtualFile newFile = virtualFile.createChildData(this, file.getName());
                        virtualFiles.add(newFile);
                        MavenUtil.runFileTemplate(project, newFile, getFileTemplate());
                    }
                } catch (IOException ex) {
                    MavenUtil.showError(project, "Cannot create " + file.getName(), ex);
                }
            }
        }.execute();
        return;
    }
    for (VirtualFile each : virtualFiles) {
        new OpenFileDescriptor(project, each).navigate(true);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Project(com.intellij.openapi.project.Project) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) IOException(java.io.IOException) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) Result(com.intellij.openapi.application.Result)

Example 15 with WriteCommandAction

use of com.intellij.openapi.command.WriteCommandAction in project intellij-community by JetBrains.

the class GrCreateSubclassAction method createSubclassGroovy.

@Nullable
public static PsiClass createSubclassGroovy(final GrTypeDefinition psiClass, final PsiDirectory targetDirectory, final String className) {
    final Project project = psiClass.getProject();
    final Ref<GrTypeDefinition> targetClass = new Ref<>();
    new WriteCommandAction(project, getTitle(psiClass), getTitle(psiClass)) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            IdeDocumentHistory.getInstance(project).includeCurrentPlaceAsChangePlace();
            final GrTypeParameterList oldTypeParameterList = psiClass.getTypeParameterList();
            try {
                targetClass.set(CreateClassActionBase.createClassByType(targetDirectory, className, PsiManager.getInstance(project), psiClass, GroovyTemplates.GROOVY_CLASS, true));
            } catch (final IncorrectOperationException e) {
                ApplicationManager.getApplication().invokeLater(() -> Messages.showErrorDialog(project, CodeInsightBundle.message("intention.error.cannot.create.class.message", className) + "\n" + e.getLocalizedMessage(), CodeInsightBundle.message("intention.error.cannot.create.class.title")));
                return;
            }
            startTemplate(oldTypeParameterList, project, psiClass, targetClass.get(), false);
        }
    }.execute();
    if (targetClass.get() == null)
        return null;
    if (!ApplicationManager.getApplication().isUnitTestMode() && !psiClass.hasTypeParameters()) {
        final Editor editor = CodeInsightUtil.positionCursorAtLBrace(project, targetClass.get().getContainingFile(), targetClass.get());
        if (editor == null)
            return targetClass.get();
        chooseAndImplement(psiClass, project, targetClass.get(), editor);
    }
    return targetClass.get();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Project(com.intellij.openapi.project.Project) GrTypeParameterList(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeParameterList) Ref(com.intellij.openapi.util.Ref) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Editor(com.intellij.openapi.editor.Editor) Result(com.intellij.openapi.application.Result) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)176 Result (com.intellij.openapi.application.Result)175 NotNull (org.jetbrains.annotations.NotNull)62 Project (com.intellij.openapi.project.Project)45 VirtualFile (com.intellij.openapi.vfs.VirtualFile)29 XmlFile (com.intellij.psi.xml.XmlFile)28 XmlTag (com.intellij.psi.xml.XmlTag)23 Document (com.intellij.openapi.editor.Document)22 PsiFile (com.intellij.psi.PsiFile)16 Module (com.intellij.openapi.module.Module)14 Nullable (org.jetbrains.annotations.Nullable)12 NlModel (com.android.tools.idea.uibuilder.model.NlModel)11 AttributesTransaction (com.android.tools.idea.uibuilder.model.AttributesTransaction)10 Editor (com.intellij.openapi.editor.Editor)10 TextRange (com.intellij.openapi.util.TextRange)8 XmlAttribute (com.intellij.psi.xml.XmlAttribute)8 File (java.io.File)8 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)8 GradleBuildModel (com.android.tools.idea.gradle.dsl.model.GradleBuildModel)7