Search in sources :

Example 1 with AttributesTransaction

use of com.android.tools.idea.uibuilder.model.AttributesTransaction in project android by JetBrains.

the class ConstraintDragHandler method commit.

@Override
public void commit(@AndroidCoordinate int x, @AndroidCoordinate int y, int modifiers, @NotNull InsertType insertType) {
    if (myComponent != null) {
        myComponent = components.get(0);
        myComponent.x = x;
        myComponent.y = y;
        NlComponent root = myComponent.getRoot();
        root.ensureNamespace(SdkConstants.SHERPA_PREFIX, SdkConstants.AUTO_URI);
        ConstraintModel model = ConstraintModel.getConstraintModel(editor.getModel());
        if (model != null) {
            int ax = model.pxToDp(x - this.layout.x - this.layout.getPadding().left - myComponent.w / 2);
            int ay = model.pxToDp(y - this.layout.y - this.layout.getPadding().top - myComponent.h / 2);
            AttributesTransaction attributes = myComponent.startAttributeTransaction();
            ConstraintUtilities.setEditorPosition(null, attributes, ax, ay);
            attributes.commit();
        }
        if (myDragWidget != null) {
            model.commitDragComponent(myComponent);
        }
    }
    insertComponents(-1, insertType);
}
Also used : NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) AttributesTransaction(com.android.tools.idea.uibuilder.model.AttributesTransaction)

Example 2 with AttributesTransaction

use of com.android.tools.idea.uibuilder.model.AttributesTransaction in project android by JetBrains.

the class AnchorTarget method connectMe.

/**
   * Connect the anchor to the given target. Applied immediately in memory.
   *
   * @param component
   * @param attribute
   * @param targetComponent
   * @return
   */
private AttributesTransaction connectMe(NlComponent component, String attribute, NlComponent targetComponent) {
    AttributesTransaction attributes = component.startAttributeTransaction();
    String targetId = null;
    if (targetComponent == component.getParent()) {
        targetId = SdkConstants.ATTR_PARENT;
    } else {
        targetId = SdkConstants.NEW_ID_PREFIX + targetComponent.ensureLiveId();
    }
    attributes.setAttribute(SdkConstants.SHERPA_URI, attribute, targetId);
    if (myType == Type.BASELINE) {
        clearAttributes(SdkConstants.SHERPA_URI, ourTopAttributes, attributes);
        clearAttributes(SdkConstants.SHERPA_URI, ourBottomAttributes, attributes);
        attributes.setAttribute(SdkConstants.SHERPA_URI, SdkConstants.ATTR_LAYOUT_VERTICAL_BIAS, null);
        attributes.setAttribute(SdkConstants.ANDROID_URI, SdkConstants.ATTR_LAYOUT_MARGIN_TOP, null);
        attributes.setAttribute(SdkConstants.ANDROID_URI, SdkConstants.ATTR_LAYOUT_MARGIN_BOTTOM, null);
    } else if (ourReciprocalAttributes.get(attribute) != null) {
        attributes.setAttribute(SdkConstants.SHERPA_URI, ourReciprocalAttributes.get(attribute), null);
    }
    if (ourMapMarginAttributes.get(attribute) != null) {
        Scene scene = myComponent.getScene();
        int marginValue = getDistance(attribute, targetComponent, scene);
        if (!scene.isControlDown()) {
            if (marginValue < 0) {
                marginValue = 0;
            } else {
                marginValue = Scout.getMargin();
            }
        } else {
            marginValue = Math.max(marginValue, 0);
        }
        String margin = String.format(SdkConstants.VALUE_N_DP, marginValue);
        attributes.setAttribute(SdkConstants.ANDROID_URI, ourMapMarginAttributes.get(attribute), margin);
        scene.needsRebuildList();
        myConnectedX = myLastX;
        myConnectedY = myLastY;
    }
    cleanup(attributes);
    attributes.apply();
    myComponent.getScene().needsLayout(Scene.ANIMATED_LAYOUT);
    return attributes;
}
Also used : Scene(com.android.tools.idea.uibuilder.scene.Scene) AttributesTransaction(com.android.tools.idea.uibuilder.model.AttributesTransaction)

Example 3 with AttributesTransaction

use of com.android.tools.idea.uibuilder.model.AttributesTransaction 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 4 with AttributesTransaction

use of com.android.tools.idea.uibuilder.model.AttributesTransaction 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 5 with AttributesTransaction

use of com.android.tools.idea.uibuilder.model.AttributesTransaction in project android by JetBrains.

the class WidgetConstraintPanel method setAttribute.

private void setAttribute(String nameSpace, String attribute, String value) {
    if (mConfiguringUI) {
        return;
    }
    NlModel model = mComponent.getModel();
    AttributesTransaction transaction = mComponent.startAttributeTransaction();
    transaction.setAttribute(nameSpace, attribute, value);
    transaction.apply();
    model.requestLayout(false);
    myTimer.setRepeats(false);
    Project project = model.getProject();
    XmlFile file = model.getFile();
    String label = "Change Widget";
    myWriteAction = new WriteCommandAction(project, label, file) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            AttributesTransaction transaction = mComponent.startAttributeTransaction();
            transaction.setAttribute(nameSpace, attribute, value);
            transaction.commit();
        }
    };
    myTimer.restart();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Project(com.intellij.openapi.project.Project) XmlFile(com.intellij.psi.xml.XmlFile) NlModel(com.android.tools.idea.uibuilder.model.NlModel) AttributesTransaction(com.android.tools.idea.uibuilder.model.AttributesTransaction) Result(com.intellij.openapi.application.Result)

Aggregations

AttributesTransaction (com.android.tools.idea.uibuilder.model.AttributesTransaction)17 NlModel (com.android.tools.idea.uibuilder.model.NlModel)11 XmlFile (com.intellij.psi.xml.XmlFile)11 Result (com.intellij.openapi.application.Result)10 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)10 Project (com.intellij.openapi.project.Project)10 NotNull (org.jetbrains.annotations.NotNull)10 NlComponent (com.android.tools.idea.uibuilder.model.NlComponent)9 ResourceFolderType (com.android.resources.ResourceFolderType)1 ModelListener (com.android.tools.idea.uibuilder.model.ModelListener)1 Scene (com.android.tools.idea.uibuilder.scene.Scene)1 SceneComponent (com.android.tools.idea.uibuilder.scene.SceneComponent)1 XmlTag (com.intellij.psi.xml.XmlTag)1 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)1