1 /*******************************************************************************
2 * Copyright (c) 2011 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.diagram.connection;
14 import gnu.trove.map.hash.THashMap;
16 import java.awt.geom.AffineTransform;
17 import java.awt.geom.Rectangle2D;
18 import java.io.PrintStream;
19 import java.io.Serializable;
20 import java.util.ArrayList;
22 import org.simantics.diagram.connection.RouteGraph.Interval;
23 import org.simantics.diagram.connection.RouteGraph.IntervalCache;
24 import org.simantics.diagram.connection.rendering.arrows.ILineEndStyle;
26 public class RouteTerminal extends RoutePoint implements RouteNode, Serializable {
28 private static final long serialVersionUID = -8839093950347737029L;
30 public static final int DIR_RIGHT = (1 << 0);
31 public static final int DIR_DOWN = (1 << 1);
32 public static final int DIR_LEFT = (1 << 2);
33 public static final int DIR_UP = (1 << 3);
34 public static final int DIR_DIRECT = (1 << 4);
37 private double minX, minY;
38 private double maxX, maxY;
39 private int allowedDirections;
40 private ILineEndStyle style;
41 private ILineEndStyle dynamicStyle;
42 private boolean routeToBounds;
43 private RouteTerminalPosition dynamicPosition;
47 public RouteTerminal(double x, double y, double minX, double minY,
48 double maxX, double maxY, int allowedDirections,
49 boolean routeToBounds,
50 ILineEndStyle style, RouteTerminalPosition dynamicPosition) {
56 this.allowedDirections = allowedDirections;
57 this.routeToBounds = routeToBounds;
59 this.dynamicPosition = dynamicPosition;
63 public void setData(Object data) {
68 public Object getData() {
72 public int getAllowedDirections() {
73 return allowedDirections;
76 public double getMinX() {
80 public double getMinY() {
84 public double getMaxX() {
88 public double getMaxY() {
92 public Rectangle2D getBounds() {
93 return new Rectangle2D.Double(minX, minY, maxX-minX, maxY-minY);
97 * Routes connection from the terminal to route line
98 * adding necessary transient route lines.
101 protected void route(ArrayList<RouteLine> lines, IntervalCache cache, boolean boundingBoxesIntersect) {
104 boolean routeLineDoesNotIntersectTerminal;
105 double linePosition = line.position;
106 if(line.isHorizontal) {
107 lineDir = linePosition < y ? 3 : 1;
108 routeLineDoesNotIntersectTerminal =
109 linePosition <= minY || linePosition >= maxY;
112 lineDir = linePosition < x ? 2 : 0;
113 routeLineDoesNotIntersectTerminal =
114 linePosition <= minX || linePosition >= maxX;
117 if(routeLineDoesNotIntersectTerminal) {
118 RouteLine line0 = createLine0(lineDir);
119 new RouteLink(line0, line);
142 if (!line.getPoints().contains(this))
144 Interval interval = cache.get(line);
145 if(line.isHorizontal) {
146 if(interval.min < minX)
154 if(interval.min < minY)
162 // In which direction the route line is?
164 boolean routeLineDoesNotIntersectTerminal;
165 double linePosition = line.position;
166 if(line.isHorizontal) {
167 lineDir = linePosition < y ? 3 : 1;
168 routeLineDoesNotIntersectTerminal = linePosition <= minY || linePosition >= maxY
169 || boundingBoxesIntersect /* we ignore intersection in this case */;
172 lineDir = linePosition < x ? 2 : 0;
173 routeLineDoesNotIntersectTerminal = linePosition <= minX || linePosition >= maxX
174 || boundingBoxesIntersect /* we ignore intersection in this case */;
177 // We can route the connection directly to the right direction
178 if(routeLineDoesNotIntersectTerminal &&
179 Directions.isAllowed(allowedDirections, lineDir)) {
180 RouteLine line0 = createLine0(lineDir);
181 new RouteLink(line0, line);
186 // We must make one bend
188 int dir = 1-(lineDir&1);
189 if(Directions.isAllowed(allowedDirections, dir)) {
190 if(Directions.isAllowed(allowedDirections, dir+2)) {
191 Interval interval = cache.get(line);
193 if(interval.max <= maxX)
196 else /* dir == 1 */ {
197 if(interval.max <= maxY)
206 if(Directions.isAllowed(allowedDirections, dir+2)) {
214 RouteLine line0 = createLine0(dir);
215 RouteLine line1 = createLine1(dir);
216 new RouteLink(line0, line1);
217 new RouteLink(line1, line);
220 line0.nextTransient = line1;
224 // We can begin to the right direction but do two bends
225 if(!routeLineDoesNotIntersectTerminal &&
226 Directions.isAllowed(allowedDirections, lineDir)) {
227 RouteLine line0 = createLine0(lineDir);
228 RouteLine line1 = createLine1(lineDir);
229 RouteLine line2 = createLine2(lineDir, cache);
230 new RouteLink(line0, line1);
231 new RouteLink(line1, line2);
232 new RouteLink(line2, line);
236 line0.nextTransient = line1;
237 line1.nextTransient = line2;
241 // Only allowed direction is to completely wrong direction:
242 // we must make two bends
245 RouteLine line0 = createLine0(dir);
246 RouteLine line1 = createLine1(dir);
247 RouteLine line2 = createLine2(dir, cache);
248 new RouteLink(line0, line1);
249 new RouteLink(line1, line2);
250 new RouteLink(line2, line);
254 line0.nextTransient = line1;
255 line1.nextTransient = line2;
261 protected RouteLine createLine0(int dir) {
262 RouteLine line0 = (dir&1) == 0
263 ? new RouteLine(true, y)
264 : new RouteLine(false, x)
266 line0.addPoint(this);
267 line0.terminal = this;
271 private RouteLine createLine1(int dir) {
272 RouteLine line1 = (dir&1) == 0
273 ? new RouteLine(false, (dir&2) == 0 ? maxX : minX)
274 : new RouteLine(true, (dir&2) == 0 ? maxY : minY)
276 line1.terminal = this;
280 private RouteLine createLine2(int dir, IntervalCache cache) {
281 Interval interval = cache.get(line);
285 if(minY < interval.min) {
286 if(maxY > interval.max) {
287 position = 2*maxY-y-interval.max < interval.min+y-2*minY ? maxY : minY;
294 if(maxY > interval.max) {
298 position = maxY-y < y-minY ? maxY : minY;
301 line2 = new RouteLine(true, position);
305 if(minX < interval.min) {
306 if(maxX > interval.max) {
307 position = 2*maxX-x-interval.max < interval.min+x-2*minX ? maxX : minX;
314 if(maxX > interval.max) {
318 position = maxX-x < x-minX ? maxX : minX;
321 line2 = new RouteLine(false, position);
323 line2.terminal = this;
327 public boolean isNear(double x2, double y2) {
328 return minX <= x2 && x2 <= maxX && minY <= y2 && y2 <= maxY;
331 void setLocation(double x2, double y2) {
342 void rotate(int amount) {
347 int temp = (allowedDirections&15) << amount;
348 allowedDirections = (temp&15) | (temp >> 4) | (allowedDirections&16);
351 public double approximatePositionToLine() {
352 // In which direction the route line is?
353 int lineDir = line.isHorizontal
354 ? (line.position < y ? 3 : 1)
355 : (line.position < x ? 2 : 0)
358 // We can route the connection directly to the right direction
359 if(Directions.isAllowed(allowedDirections, lineDir))
360 return line.isHorizontal ? x : y;
362 // We must make one bend
363 for(int dir = 0;dir < 4;++dir) {
364 if(Directions.isAllowed(allowedDirections, dir) && ((dir^lineDir)&1) == 1) {
373 // Only allowed direction is to completely wrong direction:
374 // we must make two bends
377 return line.isHorizontal ? x : y;
381 public RouteTerminal copy(THashMap<Object, Object> map) {
382 RouteTerminal copy = (RouteTerminal)map.get(this);
384 copy = new RouteTerminal(x, y, minX, minY, maxX, maxY,
385 allowedDirections, routeToBounds, style, dynamicPosition);
386 copy.setDynamicStyle(dynamicStyle);
389 copy.line = line == null ? null : line.copy(map);
394 public void print(PrintStream out) {
395 out.print(" (" + x + "," + y + ") " + allowedDirections + " -> ");
399 out.print("NO LINE");
400 out.print(" (data=" + data + ")");
404 public ILineEndStyle getStyle() {
408 public ILineEndStyle getRenderStyle() {
409 if (dynamicStyle != null)
414 public boolean hasDirectConnection() {
415 return (allowedDirections&16) == 16;
418 public RouteLine getLine() {
422 public void setLine(RouteLine line) {
426 public void setMinX(double minX) {
429 public void setMinY(double minY) {
433 public void setMaxX(double maxX) {
437 public void setMaxY(double maxY) {
441 public void toggleDirectLines() {
442 this.allowedDirections ^= 16;
445 public boolean isRouteToBounds() {
446 return routeToBounds;
449 public void setStyle(ILineEndStyle style) {
453 public ILineEndStyle getDynamicStyle() {
457 public void setDynamicStyle(ILineEndStyle dynamicStyle) {
458 this.dynamicStyle = dynamicStyle;
461 public RouteTerminalPosition getDynamicPosition() {
462 return dynamicPosition;
467 public boolean updateDynamicPosition() {
468 boolean changed = false;
469 if (dynamicPosition != null) {
470 AffineTransform tr = dynamicPosition.getTransform();
472 double nx = tr.getTranslateX();
475 double ny = tr.getTranslateY();