use of com.android.tools.idea.uibuilder.model.NlComponent in project android by JetBrains.
the class ConstraintPainter method paintConstraint.
/**
* Paints a constraint.
* <p/>
* TODO: when there are multiple links originating in the same direction from
* center, maybe offset them slightly from each other?
* @param graphics the graphics context to draw into
* @param constraint The constraint to be drawn
*/
private static void paintConstraint(NlGraphics graphics, DependencyGraph.Constraint constraint, Set<DependencyGraph.Constraint> allConstraints, TextDirection textDirection) {
DependencyGraph.ViewData source = constraint.from;
DependencyGraph.ViewData target = constraint.to;
NlComponent sourceNode = source.node;
NlComponent targetNode = target.node;
if (sourceNode == targetNode) {
// Self reference - don't visualize
return;
}
Rectangle sourceBounds = getBounds(sourceNode);
Rectangle targetBounds = getBounds(targetNode);
paintConstraint(graphics, constraint.type, sourceNode, sourceBounds, targetNode, targetBounds, allConstraints, false, /* highlightTargetEdge */
textDirection);
}
use of com.android.tools.idea.uibuilder.model.NlComponent in project android by JetBrains.
the class DeletionHandler method transfer.
private void transfer(NlComponent deleted, NlComponent target, ConstraintType targetType, int depth) {
if (depth == 20) {
// the cycle detection code
return;
}
assert myDeleted.contains(deleted);
for (XmlAttribute attribute : deleted.getTag().getAttributes()) {
String name = attribute.getLocalName();
ConstraintType type = ConstraintType.fromAttribute(name);
if (type == null) {
continue;
}
ConstraintType transfer = getCompatibleConstraint(type, targetType);
if (transfer != null) {
String id = getId(attribute);
if (id != null) {
if (myDeletedIds.contains(id)) {
NlComponent nextDeleted = myNodeMap.get(id);
if (nextDeleted != null) {
// Points to another deleted node: recurse
transfer(nextDeleted, target, targetType, depth + 1);
}
} else {
// Found an undeleted node destination: point to it directly.
// Note that we're using the
target.setAttribute(ANDROID_URI, transfer.name, attribute.getValue());
}
} else {
// Pointing to parent or center etc (non-id ref): replicate this on the target
target.setAttribute(ANDROID_URI, name, attribute.getValue());
}
}
}
}
use of com.android.tools.idea.uibuilder.model.NlComponent in project android by JetBrains.
the class GuidelinePainter method paintCycle.
/**
* Paints a constraint cycle
*/
private static void paintCycle(GuidelineHandler myState, NlGraphics g, List<Constraint> cycle) {
assert cycle.size() > 0;
NlComponent from = cycle.get(0).from.node;
assert from != null;
Rectangle fromBounds = new Rectangle(from.x, from.y, from.w, from.h);
if (myState.myDraggedNodes.contains(from)) {
fromBounds = myState.myBounds;
}
Point fromCenter = center(fromBounds);
List<Point> points = new ArrayList<Point>();
points.add(fromCenter);
for (Constraint constraint : cycle) {
assert constraint.from.node == from;
NlComponent to = constraint.to.node;
assert to != null;
Point toCenter = new Point(to.x + to.w / 2, to.y + to.h / 2);
points.add(toCenter);
// Also go through the dragged node bounds
boolean isDragged = myState.myDraggedNodes.contains(to);
if (isDragged) {
toCenter = center(myState.myBounds);
points.add(toCenter);
}
from = to;
fromCenter = toCenter;
}
points.add(fromCenter);
points.add(points.get(0));
g.useStyle(CYCLE);
for (int i = 1, n = points.size(); i < n; i++) {
Point a = points.get(i - 1);
Point b = points.get(i);
g.drawLine(a.x, a.y, b.x, b.y);
}
}
use of com.android.tools.idea.uibuilder.model.NlComponent in project android by JetBrains.
the class RelativeLayoutHandler method createDragHandler.
@Override
@Nullable
public DragHandler createDragHandler(@NotNull ViewEditor editor, @NotNull NlComponent layout, @NotNull List<NlComponent> components, @NotNull DragType type) {
final RelativeDragHandler moveHandler = new RelativeDragHandler(editor, layout, components);
return new DragHandler(editor, this, layout, components, type) {
@Nullable
@Override
public String update(@AndroidCoordinate int x, @AndroidCoordinate int y, int modifiers) {
super.update(x, y, modifiers);
NlComponent primary = components.get(0);
int deltaX = lastX - startX;
int deltaY = lastY - startY;
moveHandler.updateMove(primary, deltaX, deltaY, modifiers);
return null;
}
@Override
public void paint(@NotNull NlGraphics graphics) {
GuidelinePainter.paint(graphics, moveHandler);
}
@Override
public void commit(@AndroidCoordinate int x, @AndroidCoordinate int y, int modifiers, @NotNull InsertType insertType) {
moveHandler.removeCycles();
NlComponent previous = null;
for (NlComponent component : components) {
if (previous == null) {
moveHandler.applyConstraints(component);
} else {
// Arrange the nodes next to each other, depending on which
// edge we are attaching to. For example, if attaching to the
// top edge, arrange the subsequent nodes in a column below it.
//
// TODO: Try to do something smarter here where we detect
// constraints between the dragged edges, and we preserve these.
// We have to do this carefully though because if the
// constraints go through some other nodes not part of the
// selection, this doesn't work right, and you might be
// dragging several connected components, which we'd then
// need to stitch together such that they are all visible.
moveHandler.attachPrevious(previous, component);
}
previous = component;
}
insertAddedComponents(insertType);
}
private void insertAddedComponents(@NotNull InsertType insertType) {
List<NlComponent> added = components.stream().filter(component -> component.getParent() != layout).collect(Collectors.toList());
editor.getModel().addComponents(added, layout, null, insertType);
}
};
}
use of com.android.tools.idea.uibuilder.model.NlComponent in project android by JetBrains.
the class DeletionHandler method updateConstraints.
/**
* Updates the constraints in the layout to handle deletion of a set of
* nodes. This ensures that any constraints pointing to one of the deleted
* nodes are changed properly to point to a non-deleted node with similar
* constraints.
*/
public void updateConstraints() {
if (myChildren.size() == myDeleted.size()) {
// Deleting everything: Nothing to be done
return;
}
for (NlComponent child : myChildren) {
if (myDeleted.contains(child)) {
continue;
}
for (XmlAttribute attribute : child.getTag().getAttributes()) {
String id = getId(attribute);
if (id != null) {
if (myDeletedIds.contains(id)) {
// Unset this reference to a deleted widget. It might be
// replaced if the pointed to node points to some other node
// on the same side, but it may use a different constraint name,
// or have none at all (e.g. parent).
String name = attribute.getLocalName();
attribute.delete();
NlComponent deleted = myNodeMap.get(id);
if (deleted != null) {
ConstraintType type = ConstraintType.fromAttribute(name);
if (type != null) {
transfer(deleted, child, type, 0);
}
}
}
}
}
}
}
Aggregations