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