001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.visual.router;
042:
043: import org.netbeans.api.visual.anchor.Anchor;
044: import org.netbeans.api.visual.router.Router;
045: import org.netbeans.api.visual.router.CollisionsCollector;
046: import org.netbeans.api.visual.router.ConnectionWidgetCollisionsCollector;
047: import org.netbeans.api.visual.widget.ConnectionWidget;
048:
049: import java.awt.*;
050: import java.util.ArrayList;
051: import java.util.Arrays;
052: import java.util.Collections;
053: import java.util.List;
054:
055: /**
056: * @author David Kaspar
057: */
058: public final class OrthogonalSearchRouter implements Router {
059:
060: static final int SPACING_EDGE = 8;
061: static final int SPACING_NODE = 16;
062:
063: private CollisionsCollector collector;
064: private ConnectionWidgetCollisionsCollector connectionWidgetCollector;
065:
066: public OrthogonalSearchRouter(CollisionsCollector collector) {
067: this .collector = collector;
068: }
069:
070: public OrthogonalSearchRouter(
071: ConnectionWidgetCollisionsCollector collector) {
072: this .connectionWidgetCollector = collector;
073: }
074:
075: public java.util.List<Point> routeConnection(ConnectionWidget widget) {
076: Anchor sourceAnchor = widget.getSourceAnchor();
077: Anchor targetAnchor = widget.getTargetAnchor();
078: if (sourceAnchor == null || targetAnchor == null)
079: return Collections.emptyList();
080:
081: ArrayList<Rectangle> verticalCollisions = new ArrayList<Rectangle>();
082: ArrayList<Rectangle> horizontalCollisions = new ArrayList<Rectangle>();
083: if (collector != null)
084: collector.collectCollisions(verticalCollisions,
085: horizontalCollisions);
086: else
087: connectionWidgetCollector.collectCollisions(widget,
088: verticalCollisions, horizontalCollisions);
089:
090: Anchor.Result sourceResult = sourceAnchor.compute(widget
091: .getSourceAnchorEntry());
092: Anchor.Result targetResult = targetAnchor.compute(widget
093: .getTargetAnchorEntry());
094: Point sourcePoint = sourceResult.getAnchorSceneLocation();
095: Point targetPoint = targetResult.getAnchorSceneLocation();
096:
097: Solution bestSolution = new Solution(Integer.MAX_VALUE >> 2,
098: Arrays.asList(sourcePoint, targetPoint));
099:
100: for (Anchor.Direction sourceDirection : sourceResult
101: .getDirections()) {
102: for (Anchor.Direction targetDirection : targetResult
103: .getDirections()) {
104: Solution solution = new OrthogonalSearchRouterCore(
105: widget.getScene(), verticalCollisions,
106: horizontalCollisions, sourcePoint,
107: sourceDirection, targetPoint, targetDirection)
108: .route();
109: if (solution != null
110: && solution.compareTo(bestSolution) > 0)
111: bestSolution = solution;
112: }
113: }
114:
115: return bestSolution.getPoints();
116: }
117:
118: static final class Solution implements Comparable<Solution> {
119:
120: private int price;
121: private List<Point> points;
122:
123: public Solution(int price, List<Point> points) {
124: this .price = price;
125: this .points = points;
126: }
127:
128: public int getPrice() {
129: return price;
130: }
131:
132: public List<Point> getPoints() {
133: return points;
134: }
135:
136: public int compareTo(Solution other) {
137: return other.price - price;
138: }
139:
140: }
141:
142: }
|