Search in sources :

Example 86 with WriteCommandAction

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

the class ResizeInteraction method moveTo.

private void moveTo(@SwingCoordinate int x, @SwingCoordinate int y, @InputEventMask final int modifiers, boolean commit) {
    if (myResizeHandler == null) {
        return;
    }
    final int ax = Coordinates.getAndroidX(myScreenView, x);
    final int ay = Coordinates.getAndroidY(myScreenView, y);
    final int deltaX = Coordinates.getAndroidDimension(myScreenView, x - myStartX);
    final int deltaY = Coordinates.getAndroidDimension(myScreenView, y - myStartY);
    final Rectangle newBounds = getNewBounds(new Rectangle(myComponent.x, myComponent.y, myComponent.w, myComponent.h), deltaX, deltaY);
    myResizeHandler.update(ax, ay, modifiers, newBounds);
    if (commit) {
        NlModel model = myScreenView.getModel();
        Project project = model.getFacet().getModule().getProject();
        XmlFile file = model.getFile();
        String label = "Resize";
        WriteCommandAction action = new WriteCommandAction(project, label, file) {

            @Override
            protected void run(@NotNull Result result) throws Throwable {
                myResizeHandler.commit(ax, ay, modifiers, newBounds);
            }
        };
        action.execute();
        model.notifyModified(NlModel.ChangeType.RESIZE_COMMIT);
    }
    myScreenView.getSurface().repaint();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Project(com.intellij.openapi.project.Project) XmlFile(com.intellij.psi.xml.XmlFile) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result)

Example 87 with WriteCommandAction

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

the class LayoutMetadata method setProperty.

/**
   * Sets the given property of the given DOM node to a given value, or if null clears
   * the property.
   */
public static void setProperty(@NotNull final Project project, @Nullable String title, @NotNull final XmlFile file, @NotNull final XmlTag element, @NotNull final String name, @Nullable final String namespace, @Nullable final String value) {
    String capitalizedName = StringUtil.capitalize(name);
    if (title == null) {
        title = String.format(value != null ? "Set %1$s" : "Clear %1$s", capitalizedName);
    }
    WriteCommandAction<Void> action = new WriteCommandAction<Void>(project, title, file) {

        @Override
        protected void run(@NotNull Result<Void> result) throws Throwable {
            if (value == null) {
                // Clear attribute
                XmlAttribute attribute;
                if (namespace != null) {
                    attribute = element.getAttribute(name, namespace);
                } else {
                    attribute = element.getAttribute(name);
                }
                if (attribute != null) {
                    attribute.delete();
                }
            } else {
                if (namespace != null) {
                    AndroidResourceUtil.ensureNamespaceImported(file, namespace, null);
                    element.setAttribute(name, namespace, value);
                } else {
                    element.setAttribute(name, value);
                }
            }
        }
    };
    action.execute();
    // Also set the values on the same elements in any resource variations
    // of the same layout
    // TODO: This should be done after a brief delay, say 50ms
    final List<XmlTag> list = ApplicationManager.getApplication().runReadAction(new Computable<List<XmlTag>>() {

        @Override
        @Nullable
        public List<XmlTag> compute() {
            // Look up the id of the element, if any
            String id = stripIdPrefix(element.getAttributeValue(ATTR_ID, ANDROID_URI));
            if (id.isEmpty()) {
                return null;
            }
            VirtualFile layoutFile = file.getVirtualFile();
            if (layoutFile != null) {
                final List<VirtualFile> variations = ResourceHelper.getResourceVariations(layoutFile, false);
                if (variations.isEmpty()) {
                    return null;
                }
                PsiManager manager = PsiManager.getInstance(project);
                List<XmlTag> list = Lists.newArrayList();
                for (VirtualFile file : variations) {
                    PsiFile psiFile = manager.findFile(file);
                    if (psiFile == null) {
                        continue;
                    }
                    for (XmlTag tag : PsiTreeUtil.findChildrenOfType(psiFile, XmlTag.class)) {
                        XmlAttribute attribute = tag.getAttribute(ATTR_ID, ANDROID_URI);
                        if (attribute == null || attribute.getValue() == null) {
                            continue;
                        }
                        if (attribute.getValue().endsWith(id) && id.equals(stripIdPrefix(attribute.getValue()))) {
                            list.add(tag);
                            break;
                        }
                    }
                }
                return list;
            }
            return null;
        }
    });
    if (list != null && !list.isEmpty()) {
        List<PsiFile> affectedFiles = Lists.newArrayList();
        for (XmlTag tag : list) {
            PsiFile psiFile = tag.getContainingFile();
            if (psiFile != null) {
                affectedFiles.add(psiFile);
            }
        }
        action = new WriteCommandAction<Void>(project, title, affectedFiles.toArray(new PsiFile[affectedFiles.size()])) {

            @Override
            protected void run(@NotNull Result<Void> result) throws Throwable {
                for (XmlTag tag : list) {
                    if (value == null) {
                        // Clear attribute
                        XmlAttribute attribute;
                        if (namespace != null) {
                            attribute = tag.getAttribute(name, namespace);
                        } else {
                            attribute = tag.getAttribute(name);
                        }
                        if (attribute != null) {
                            attribute.delete();
                        }
                    } else {
                        if (namespace != null) {
                            AndroidResourceUtil.ensureNamespaceImported(file, namespace, null);
                            tag.setAttribute(name, namespace, value);
                        } else {
                            tag.setAttribute(name, value);
                        }
                    }
                }
            }
        };
        action.execute();
    }
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlAttribute(com.intellij.psi.xml.XmlAttribute) PsiManager(com.intellij.psi.PsiManager) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result) List(java.util.List) PsiFile(com.intellij.psi.PsiFile) Nullable(com.android.annotations.Nullable) XmlTag(com.intellij.psi.xml.XmlTag)

Example 88 with WriteCommandAction

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

the class NewAndroidComponentDialog method addInflateStatement.

private static void addInflateStatement(final PsiCodeBlock body, final String layoutFieldRef) {
    final Project project = body.getProject();
    final PsiStatement[] statements = body.getStatements();
    if (statements.length == 1) {
        final PsiStatement statement = statements[0];
        new WriteCommandAction(project, body.getContainingFile()) {

            @Override
            protected void run(@NotNull Result result) throws Throwable {
                final PsiStatement newStatement = PsiElementFactory.SERVICE.getInstance(project).createStatementFromText("return inflater.inflate(" + layoutFieldRef + ", container, false);", body);
                statement.replace(newStatement);
                JavaCodeStyleManager.getInstance(project).shortenClassReferences(body);
                CodeStyleManager.getInstance(project).reformat(body);
            }
        }.execute();
    }
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Project(com.intellij.openapi.project.Project) Result(com.intellij.openapi.application.Result)

Example 89 with WriteCommandAction

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

the class BaseNamedConfigurable method apply.

@Override
public void apply() throws ConfigurationException {
    if (myModule.isModified()) {
        GradleBuildModel parsedModel = myModule.getParsedModel();
        if (parsedModel != null && parsedModel.isModified()) {
            String name = String.format("Applying changes to module '%1$s'", myModule.getName());
            new WriteCommandAction(myModule.getParent().getResolvedModel(), name) {

                @Override
                protected void run(@NotNull Result result) throws Throwable {
                    parsedModel.applyChanges();
                    myModule.setModified(false);
                }
            }.execute();
        }
    }
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) GradleBuildModel(com.android.tools.idea.gradle.dsl.model.GradleBuildModel) Result(com.intellij.openapi.application.Result)

Example 90 with WriteCommandAction

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

the class InferSupportAnnotationsAction method applyRunnable.

private static Runnable applyRunnable(Project project, Computable<UsageInfo[]> computable) {
    return () -> {
        LocalHistoryAction action = LocalHistory.getInstance().startAction(INFER_SUPPORT_ANNOTATIONS);
        try {
            new WriteCommandAction(project, INFER_SUPPORT_ANNOTATIONS) {

                @Override
                protected void run(@NotNull Result result) throws Throwable {
                    UsageInfo[] infos = computable.compute();
                    if (infos.length > 0) {
                        Set<PsiElement> elements = new LinkedHashSet<>();
                        for (UsageInfo info : infos) {
                            PsiElement element = info.getElement();
                            if (element != null) {
                                PsiFile containingFile = element.getContainingFile();
                                // Skip results in .class files; these are typically from extracted AAR files
                                VirtualFile virtualFile = containingFile.getVirtualFile();
                                if (virtualFile.getFileType().isBinary()) {
                                    continue;
                                }
                                ContainerUtil.addIfNotNull(elements, containingFile);
                            }
                        }
                        if (!FileModificationService.getInstance().preparePsiElementsForWrite(elements))
                            return;
                        SequentialModalProgressTask progressTask = new SequentialModalProgressTask(project, INFER_SUPPORT_ANNOTATIONS, false);
                        progressTask.setMinIterationTime(200);
                        progressTask.setTask(new AnnotateTask(project, progressTask, infos));
                        ProgressManager.getInstance().run(progressTask);
                    } else {
                        InferSupportAnnotations.nothingFoundMessage(project);
                    }
                }
            }.execute();
        } finally {
            action.finish();
        }
    };
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) VirtualFile(com.intellij.openapi.vfs.VirtualFile) NotNull(org.jetbrains.annotations.NotNull) SequentialModalProgressTask(com.intellij.util.SequentialModalProgressTask) Result(com.intellij.openapi.application.Result) LocalHistoryAction(com.intellij.history.LocalHistoryAction) UsageInfo(com.intellij.usageView.UsageInfo)

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