Search in sources :

Example 56 with NlComponent

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);
}
Also used : NlComponent(com.android.tools.idea.uibuilder.model.NlComponent)

Example 57 with NlComponent

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());
            }
        }
    }
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) NlComponent(com.android.tools.idea.uibuilder.model.NlComponent)

Example 58 with NlComponent

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);
    }
}
Also used : NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) Constraint(com.android.tools.idea.uibuilder.handlers.relative.DependencyGraph.Constraint) ArrayList(java.util.ArrayList) Constraint(com.android.tools.idea.uibuilder.handlers.relative.DependencyGraph.Constraint)

Example 59 with NlComponent

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);
        }
    };
}
Also used : AndroidCoordinate(com.android.tools.idea.uibuilder.model.AndroidCoordinate) AndroidCoordinate(com.android.tools.idea.uibuilder.model.AndroidCoordinate) com.android.tools.idea.uibuilder.api(com.android.tools.idea.uibuilder.api) TextDirection(com.android.tools.idea.uibuilder.model.TextDirection) NlGraphics(com.android.tools.idea.uibuilder.graphics.NlGraphics) SegmentType(com.android.tools.idea.uibuilder.model.SegmentType) Collectors(java.util.stream.Collectors) java.awt(java.awt) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Lists(com.google.common.collect.Lists) NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) ScreenView(com.android.tools.idea.uibuilder.surface.ScreenView) NotNull(org.jetbrains.annotations.NotNull) NlGraphics(com.android.tools.idea.uibuilder.graphics.NlGraphics) NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) NotNull(org.jetbrains.annotations.NotNull) Nullable(org.jetbrains.annotations.Nullable)

Example 60 with NlComponent

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);
                        }
                    }
                }
            }
        }
    }
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) NlComponent(com.android.tools.idea.uibuilder.model.NlComponent)

Aggregations

NlComponent (com.android.tools.idea.uibuilder.model.NlComponent)184 NlModel (com.android.tools.idea.uibuilder.model.NlModel)34 NotNull (org.jetbrains.annotations.NotNull)34 NlProperty (com.android.tools.idea.uibuilder.property.NlProperty)18 ScreenView (com.android.tools.idea.uibuilder.surface.ScreenView)17 Nullable (org.jetbrains.annotations.Nullable)14 Test (org.junit.Test)12 Matchers.anyString (org.mockito.Matchers.anyString)11 ViewGroupHandler (com.android.tools.idea.uibuilder.api.ViewGroupHandler)9 AttributesTransaction (com.android.tools.idea.uibuilder.model.AttributesTransaction)9 AttributeDefinition (org.jetbrains.android.dom.attrs.AttributeDefinition)8 NlPropertyItem (com.android.tools.idea.uibuilder.property.NlPropertyItem)7 XmlFile (com.intellij.psi.xml.XmlFile)7 XmlTag (com.intellij.psi.xml.XmlTag)7 ArrayList (java.util.ArrayList)7 ModelBuilder (com.android.tools.idea.uibuilder.fixtures.ModelBuilder)6 NlGraphics (com.android.tools.idea.uibuilder.graphics.NlGraphics)6 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)6 Project (com.intellij.openapi.project.Project)6 List (java.util.List)6