1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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 java.awt.Shape;
15 import java.awt.geom.Rectangle2D;
16 import java.util.Collection;
17 import java.util.Collections;
19 import org.simantics.diagram.connection.rendering.IRouteGraphRenderer;
20 import org.simantics.g2d.connection.ConnectionEntity;
21 import org.simantics.g2d.connection.handler.ConnectionHandler;
22 import org.simantics.g2d.diagram.handler.PickRequest.PickPolicy;
23 import org.simantics.g2d.diagram.handler.Topology.Connection;
24 import org.simantics.g2d.element.ElementClass;
25 import org.simantics.g2d.element.ElementHints;
26 import org.simantics.g2d.element.ElementUtils;
27 import org.simantics.g2d.element.IElement;
28 import org.simantics.g2d.element.SceneGraphNodeKey;
29 import org.simantics.g2d.element.handler.InternalSize;
30 import org.simantics.g2d.element.handler.Outline;
31 import org.simantics.g2d.element.handler.Pick;
32 import org.simantics.g2d.element.handler.SceneGraph;
33 import org.simantics.g2d.element.handler.SelectionOutline;
34 import org.simantics.g2d.element.handler.impl.ConfigurableEdgeVisuals;
35 import org.simantics.g2d.element.handler.impl.ConnectionSelectionOutline;
36 import org.simantics.g2d.element.handler.impl.FillColorImpl;
37 import org.simantics.g2d.element.handler.impl.TextImpl;
38 import org.simantics.g2d.elementclass.connection.EdgeClass.FixedTransform;
39 import org.simantics.scenegraph.g2d.G2DParentNode;
40 import org.simantics.scenegraph.g2d.nodes.connection.IRouteGraphListener;
41 import org.simantics.scenegraph.g2d.nodes.connection.RouteGraphNode;
42 import org.simantics.utils.datastructures.hints.IHintContext.Key;
43 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
46 * An element class for single connection entity elements. A connection entity
47 * consists of connection edge segments and branch points as its children.
49 * @author Tuukka Lehtonen
51 public class RouteGraphConnectionClass {
53 public static final Key KEY_ROUTEGRAPH = new KeyOf(RouteGraph.class, "ROUTE_GRAPH");
54 public static final Key KEY_RENDERER = new KeyOf(IRouteGraphRenderer.class, "ROUTE_GRAPH_RENDERER");
55 public static final Key KEY_PICK_TOLERANCE = new KeyOf(Double.class, "PICK_TOLERANCE");
56 public static final Key KEY_USE_TOLERANCE_IN_SELECTION = new KeyOf(Boolean.class, "PICK_TOLERANCE_SELECTION");
57 public static final Key KEY_RG_LISTENER = new KeyOf(IRouteGraphListener.class, "ROUTE_GRAPH_LISTENER");
58 public static final Key KEY_RG_NODE = new SceneGraphNodeKey(RouteGraphNode.class, "ROUTE_GRAPH_NODE");
60 public static final double BOUND_TOLERANCE = 0.9;
62 public static final ElementClass CLASS =
66 FixedTransform.INSTANCE,
68 ConnectionBoundsAndPick.INSTANCE,
69 ConnectionSelectionOutline.INSTANCE,
70 ConnectionHandlerImpl.INSTANCE,
71 ConnectionSceneGraph.INSTANCE,
72 //SimpleElementLayers.INSTANCE,
74 // Exists only loading connection visuals through ConnectionVisualsLoader
75 ConfigurableEdgeVisuals.DEFAULT,
77 ).setId(RouteGraphConnectionClass.class.getSimpleName());
80 static class ConnectionHandlerImpl implements ConnectionHandler {
82 public static final ConnectionHandlerImpl INSTANCE = new ConnectionHandlerImpl();
84 private static final long serialVersionUID = 3267139233182458330L;
87 public Collection<IElement> getBranchPoints(IElement connection, Collection<IElement> result) {
88 return Collections.<IElement>emptySet();
92 public Collection<IElement> getChildren(IElement connection, Collection<IElement> result) {
93 return Collections.emptySet();
97 public Collection<IElement> getSegments(IElement connection, Collection<IElement> result) {
98 return Collections.<IElement>emptySet();
102 public Collection<Connection> getTerminalConnections(IElement connection, Collection<Connection> result) {
103 ConnectionEntity ce = connection.getHint(ElementHints.KEY_CONNECTION_ENTITY);
105 return Collections.<Connection>emptySet();
106 return ce.getTerminalConnections(result);
111 static final class ConnectionSceneGraph implements SceneGraph {
113 public static final ConnectionSceneGraph INSTANCE = new ConnectionSceneGraph();
115 private static final long serialVersionUID = 4232871859964883266L;
118 public void init(IElement connection, G2DParentNode parent) {
119 RouteGraph rg = connection.getHint(KEY_ROUTEGRAPH);
120 IRouteGraphRenderer renderer = connection.getHint(KEY_RENDERER);
121 if (rg == null || renderer == null) {
124 RouteGraphNode rgn = connection.getHint(KEY_RG_NODE);
126 rgn = parent.addNode(ElementUtils.generateNodeId(connection), RouteGraphNode.class);
127 connection.setHint(KEY_RG_NODE, rgn);
129 rgn.setRouteGraph(rg);
130 rgn.setRenderer(renderer);
132 IRouteGraphListener listener = connection.getHint(KEY_RG_LISTENER);
133 rgn.setRouteGraphListener(listener);
135 Double tolerance = connection.getHint(KEY_PICK_TOLERANCE);
136 if (tolerance != null)
137 rgn.setPickTolerance(tolerance);
142 public void cleanup(IElement connection) {
143 ElementUtils.removePossibleNode(connection, KEY_RG_NODE);
144 connection.removeHint(KEY_RG_NODE);
148 static final class ConnectionBoundsAndPick implements InternalSize, Outline, Pick {
150 private static final long serialVersionUID = 4232871859964883266L;
152 public static final ConnectionBoundsAndPick INSTANCE = new ConnectionBoundsAndPick();
154 // Single-threaded system, should be fine to use this for everything.
155 Rectangle2D temp = new Rectangle2D.Double();
157 private Shape getSelectionShape(IElement e) {
158 for (SelectionOutline so : e.getElementClass().getItemsByClass(SelectionOutline.class)) {
159 Shape shape = so.getSelectionShape(e);
163 // Using on-diagram coordinates because neither connections nor
164 // edges have a non-identity transform which means that
165 // coordinates are always absolute. Therefore branch point
166 // shape also needs to be calculated in absolute coordinates.
167 Shape shape = ElementUtils.getElementShapeOrBoundsOnDiagram(e);
172 public boolean pickTest(IElement e, Shape s, PickPolicy policy) {
173 RouteGraph rg = getRouteGraph(e);
177 Rectangle2D bounds = getBounds(s);
179 case PICK_CONTAINED_OBJECTS:
180 Shape selectionShape = getSelectionShape(e);
181 return bounds.contains(selectionShape.getBounds2D());
182 case PICK_INTERSECTING_OBJECTS:
183 double tolerance = 0.0;
184 if (e.containsHint(KEY_USE_TOLERANCE_IN_SELECTION))
185 tolerance = getTolerance(e);
187 tolerance = (bounds.getHeight()+bounds.getHeight()) * 0.25;
188 Object node = rg.pickLine(bounds.getCenterX(), bounds.getCenterY(), tolerance);
195 public Rectangle2D getBounds(IElement e, Rectangle2D size) {
196 RouteGraph rg = getRouteGraph(e);
199 size = new Rectangle2D.Double();
206 public Shape getElementShape(IElement e) {
207 RouteGraph rg = getRouteGraph(e);
208 return rg == null ? null : rg.getPath2D();
211 private Rectangle2D getBounds(Shape shape) {
212 if (shape instanceof Rectangle2D)
213 return (Rectangle2D) shape;
214 return shape.getBounds2D();
217 private RouteGraph getRouteGraph(IElement e) {
218 RouteGraphNode rgn = e.getHint(KEY_RG_NODE);
219 return rgn == null ? null : rgn.getRouteGraph();
222 private double getTolerance(IElement e) {
223 RouteGraphNode rgn = e.getHint(KEY_RG_NODE);
224 return rgn.getPickTolerance();
229 public static int shortestDirectionOutOfBounds(double x, double y, Rectangle2D bounds) {
230 double mx = bounds.getMinX();
231 double Mx = bounds.getMaxX();
232 double my = bounds.getMinY();
233 double My = bounds.getMaxY();
236 double down = My - y;
237 double left = x - mx;
238 double right = Mx - x;
241 double[] dists = { right, down, left, up };
242 byte[] masks = { 0x1, 0x2, 0x4, 0x8 };
243 for (int i = 1; i < 4; ++i) {
244 double value = dists[i];
245 byte mask = masks[i];
247 while (j >= 0 && dists[j] > value) {
248 dists[j + 1] = dists[j];
249 masks[j + 1] = masks[j];
252 dists[j + 1] = value;
256 // Construct mask out of the shortest equal directions
258 double value = dists[0] / BOUND_TOLERANCE;
259 for (int i = 1; i < 4; ++i) {
260 if (dists[i] > value)