001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019: package org.netbeans.modules.vmd.api.flow.visual;
020:
021: import org.netbeans.api.visual.router.ConnectionWidgetCollisionsCollector;
022: import org.netbeans.api.visual.widget.ConnectionWidget;
023: import org.netbeans.api.visual.widget.LayerWidget;
024: import org.netbeans.api.visual.widget.Widget;
025:
026: import java.awt.*;
027: import java.util.ArrayList;
028: import java.util.Collection;
029:
030: /**
031: * @author David Kaspar
032: */
033: public class OrthogonalCollisionsCollector implements
034: ConnectionWidgetCollisionsCollector {
035:
036: private static final int SPACING_EDGE = 8;
037: private static final int SPACING_NODE = 16;
038:
039: private LayerWidget[] layers;
040:
041: public OrthogonalCollisionsCollector(LayerWidget... layers) {
042: this .layers = layers;
043: }
044:
045: public void collectCollisions(ConnectionWidget connectionWidget,
046: java.util.List<Rectangle> verticalCollisions,
047: java.util.List<Rectangle> horizontalCollisions) {
048: for (Widget widget : getWidgets()) {
049: if (!widget.isValidated())
050: continue;
051: if (widget instanceof ConnectionWidget) {
052: ConnectionWidget conn = (ConnectionWidget) widget;
053: if (!conn.isRouted())
054: continue;
055: java.util.List<Point> controlPoints = conn
056: .getControlPoints();
057: int last = controlPoints.size() - 1;
058: for (int i = 0; i < last; i++) {
059: if (i == 0
060: && (connectionWidget.getSourceAnchor() == conn
061: .getSourceAnchor() || connectionWidget
062: .getTargetAnchor() == conn
063: .getSourceAnchor()))
064: continue;
065: if (i == last - 1
066: && (connectionWidget.getSourceAnchor() == conn
067: .getTargetAnchor() || connectionWidget
068: .getTargetAnchor() == conn
069: .getTargetAnchor()))
070: continue;
071: Point point1 = controlPoints.get(i);
072: Point point2 = controlPoints.get(i + 1);
073: if (point1.x == point2.x) {
074: Rectangle rectangle = new Rectangle(point1.x,
075: Math.min(point1.y, point2.y), 0, Math
076: .abs(point2.y - point1.y));
077: rectangle.grow(SPACING_EDGE, SPACING_EDGE);
078: verticalCollisions.add(rectangle);
079: } else if (point1.y == point2.y) {
080: Rectangle rectangle = new Rectangle(Math.min(
081: point1.x, point2.x), point1.y, Math
082: .abs(point2.x - point1.x), 0);
083: rectangle.grow(SPACING_EDGE, SPACING_EDGE);
084: horizontalCollisions.add(rectangle);
085: }
086: }
087: } else {
088: Rectangle bounds = widget.getBounds();
089: Rectangle rectangle = widget
090: .convertLocalToScene(bounds);
091: rectangle.grow(SPACING_NODE, SPACING_NODE);
092: verticalCollisions.add(rectangle);
093: horizontalCollisions.add(rectangle);
094: }
095: }
096: }
097:
098: protected Collection<Widget> getWidgets() {
099: ArrayList<Widget> list = new ArrayList<Widget>();
100: for (LayerWidget layer : layers)
101: list.addAll(layer.getChildren());
102: return list;
103: }
104:
105: }
|