Search in sources :

Example 81 with Result

use of com.intellij.openapi.application.Result in project android by JetBrains.

the class ConstraintUtilities method saveModelToXML.

/**
   * Utility function to commit to the NlModel the current state of all widgets
   *
   * @param nlModel
   * @param commit  if false, the changes are only reflected in memory not saved to the XML file.
   */
static void saveModelToXML(@NotNull NlModel nlModel, boolean commit) {
    ConstraintModel model = ConstraintModel.getConstraintModel(nlModel);
    if (commit) {
        Project project = nlModel.getProject();
        XmlFile file = nlModel.getFile();
        String label = "Constraint";
        WriteCommandAction action = new WriteCommandAction(project, label, file) {

            @Override
            protected void run(@NotNull Result result) throws Throwable {
                saveXmlWidgets(model, commit);
            }
        };
        action.execute();
    } else {
        saveXmlWidgets(model, commit);
    }
}
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 82 with Result

use of com.intellij.openapi.application.Result in project android by JetBrains.

the class AnchorTarget method mouseRelease.

/**
   * On mouseRelease, we can either disconnect the current anchor (if the mouse release is on ourselve)
   * or connect the anchor to a given target. Modifications are applied first in memory then commited
   * to the XML model.
   *
   * @param x
   * @param y
   * @param closestTarget
   */
@Override
public void mouseRelease(int x, int y, @Nullable Target closestTarget) {
    myLastX = -1;
    myLastY = -1;
    if (myComponent.getParent() != null) {
        myComponent.getParent().setExpandTargetArea(false);
    }
    if (closestTarget != null && closestTarget instanceof AnchorTarget && !(((AnchorTarget) closestTarget).isConnected(this))) {
        NlComponent component = myComponent.getNlComponent();
        if (closestTarget == this) {
            disconnectMe(component);
        } else {
            String attribute = getAttribute(closestTarget);
            if (attribute != null) {
                AnchorTarget targetAnchor = (AnchorTarget) closestTarget;
                NlComponent targetComponent = targetAnchor.myComponent.getNlComponent();
                AttributesTransaction attributes = connectMe(component, attribute, targetComponent);
                NlModel nlModel = component.getModel();
                Project project = nlModel.getProject();
                XmlFile file = nlModel.getFile();
                String label = "Constraint Connected";
                WriteCommandAction action = new WriteCommandAction(project, label, file) {

                    @Override
                    protected void run(@NotNull Result result) throws Throwable {
                        attributes.commit();
                    }
                };
                action.execute();
                myComponent.getScene().needsLayout(Scene.ANIMATED_LAYOUT);
            }
        }
    }
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Project(com.intellij.openapi.project.Project) NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) XmlFile(com.intellij.psi.xml.XmlFile) NlModel(com.android.tools.idea.uibuilder.model.NlModel) NotNull(org.jetbrains.annotations.NotNull) AttributesTransaction(com.android.tools.idea.uibuilder.model.AttributesTransaction) Result(com.intellij.openapi.application.Result)

Example 83 with Result

use of com.intellij.openapi.application.Result in project android by JetBrains.

the class NlDropListener method morphReceiverIntoViewGroup.

/**
   * Morph the receiver into a constraint layout and add the dragged component to it.
   *
   * @param model      {@link NlComponentTree#getDesignerModel()}
   */
private void morphReceiverIntoViewGroup(@NotNull NlModel model) {
    final AttributesTransaction transaction = myDragReceiver.startAttributeTransaction();
    for (AttributeSnapshot attribute : myDragReceiver.getAttributes()) {
        if (!TOOLS_PREFIX.equals(attribute.prefix) && !ourCopyableAttributes.contains(attribute.name) && attribute.namespace != null) {
            transaction.removeAttribute(attribute.namespace, attribute.name);
        }
    }
    new WriteCommandAction(model.getProject(), model.getFile()) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            final XmlTag tag = myDragReceiver.getTag();
            tag.setName(CONSTRAINT_LAYOUT);
            myDragReceiver.setTag(tag);
            transaction.commit();
        }
    }.execute();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) AttributeSnapshot(com.android.tools.idea.rendering.AttributeSnapshot) Result(com.intellij.openapi.application.Result) XmlTag(com.intellij.psi.xml.XmlTag)

Example 84 with Result

use of com.intellij.openapi.application.Result in project android by JetBrains.

the class GuidelineCycleTarget method mouseRelease.

@Override
public void mouseRelease(int x, int y, @Nullable Target closestTarget) {
    AttributesTransaction attributes = myComponent.getNlComponent().startAttributeTransaction();
    String begin = attributes.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.LAYOUT_CONSTRAINT_GUIDE_BEGIN);
    String end = attributes.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.LAYOUT_CONSTRAINT_GUIDE_END);
    String percent = attributes.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.LAYOUT_CONSTRAINT_GUIDE_PERCENT);
    SceneComponent parent = myComponent.getParent();
    int value = myComponent.getDrawY() - parent.getDrawY();
    int dimension = parent.getDrawHeight();
    if (!myIsHorizontal) {
        value = myComponent.getDrawX() - parent.getDrawX();
        dimension = parent.getDrawWidth();
    }
    if (begin != null) {
        setEnd(attributes, dimension - value);
    } else if (end != null) {
        setPercent(attributes, value / (float) dimension);
    } else if (percent != null) {
        setBegin(attributes, value);
    }
    attributes.apply();
    NlModel nlModel = myComponent.getNlComponent().getModel();
    Project project = nlModel.getProject();
    XmlFile file = nlModel.getFile();
    String label = "Cycle Guideline";
    WriteCommandAction action = new WriteCommandAction(project, label, file) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            attributes.commit();
        }
    };
    action.execute();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Project(com.intellij.openapi.project.Project) XmlFile(com.intellij.psi.xml.XmlFile) SceneComponent(com.android.tools.idea.uibuilder.scene.SceneComponent) NlModel(com.android.tools.idea.uibuilder.model.NlModel) NotNull(org.jetbrains.annotations.NotNull) AttributesTransaction(com.android.tools.idea.uibuilder.model.AttributesTransaction) Result(com.intellij.openapi.application.Result)

Example 85 with Result

use of com.intellij.openapi.application.Result in project intellij-plugins by JetBrains.

the class DartIntroduceHandler method performReplace.

@Nullable
private SmartPsiElementPointer<PsiElement> performReplace(@NotNull final PsiElement declaration, final DartIntroduceOperation operation) {
    final DartExpression expression = operation.getInitializer();
    final Project project = operation.getProject();
    return new WriteCommandAction<SmartPsiElementPointer<PsiElement>>(project, expression.getContainingFile()) {

        protected void run(@NotNull final Result<SmartPsiElementPointer<PsiElement>> result) throws Throwable {
            final PsiElement createdDeclaration = addDeclaration(operation, declaration);
            if (createdDeclaration != null) {
                result.setResult(SmartPointerManager.getInstance(project).createSmartPsiElementPointer(createdDeclaration));
                modifyDeclaration(createdDeclaration);
            }
            PsiElement newExpression = createExpression(project, operation.getName());
            if (operation.isReplaceAll()) {
                List<PsiElement> newOccurrences = new ArrayList<>();
                for (PsiElement occurrence : operation.getOccurrences()) {
                    final PsiElement replaced = replaceExpression(occurrence, newExpression, operation);
                    if (replaced != null) {
                        newOccurrences.add(replaced);
                    }
                }
                operation.setOccurrences(newOccurrences);
            } else {
                final PsiElement replaced = replaceExpression(expression, newExpression, operation);
                operation.setOccurrences(Collections.singletonList(replaced));
            }
            postRefactoring(operation.getElement());
        }
    }.execute().getResultObject();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Project(com.intellij.openapi.project.Project) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

Result (com.intellij.openapi.application.Result)298 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)186 NotNull (org.jetbrains.annotations.NotNull)116 WriteAction (com.intellij.openapi.application.WriteAction)93 VirtualFile (com.intellij.openapi.vfs.VirtualFile)83 Project (com.intellij.openapi.project.Project)58 Module (com.intellij.openapi.module.Module)32 File (java.io.File)32 PsiFile (com.intellij.psi.PsiFile)31 Document (com.intellij.openapi.editor.Document)28 XmlFile (com.intellij.psi.xml.XmlFile)28 IOException (java.io.IOException)28 XmlTag (com.intellij.psi.xml.XmlTag)24 Nullable (org.jetbrains.annotations.Nullable)17 ReadAction (com.intellij.openapi.application.ReadAction)16 ArrayList (java.util.ArrayList)15 Editor (com.intellij.openapi.editor.Editor)13 PsiElement (com.intellij.psi.PsiElement)13 NlModel (com.android.tools.idea.uibuilder.model.NlModel)11 AttributesTransaction (com.android.tools.idea.uibuilder.model.AttributesTransaction)10