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.g2d.elementclass;
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.RouteGraph;
20 import org.simantics.diagram.connection.rendering.IRouteGraphRenderer;
21 import org.simantics.g2d.connection.ConnectionEntity;
22 import org.simantics.g2d.connection.handler.ConnectionHandler;
23 import org.simantics.g2d.diagram.handler.PickRequest.PickPolicy;
24 import org.simantics.g2d.diagram.handler.Topology.Connection;
25 import org.simantics.g2d.element.ElementClass;
26 import org.simantics.g2d.element.ElementHints;
27 import org.simantics.g2d.element.ElementUtils;
28 import org.simantics.g2d.element.IElement;
29 import org.simantics.g2d.element.SceneGraphNodeKey;
30 import org.simantics.g2d.element.handler.InternalSize;
31 import org.simantics.g2d.element.handler.Outline;
32 import org.simantics.g2d.element.handler.Pick;
33 import org.simantics.g2d.element.handler.SceneGraph;
34 import org.simantics.g2d.element.handler.SelectionOutline;
35 import org.simantics.g2d.element.handler.impl.ConfigurableEdgeVisuals;
36 import org.simantics.g2d.element.handler.impl.ConnectionSelectionOutline;
37 import org.simantics.g2d.element.handler.impl.FillColorImpl;
38 import org.simantics.g2d.element.handler.impl.TextImpl;
39 import org.simantics.g2d.elementclass.connection.EdgeClass.FixedTransform;
40 import org.simantics.scenegraph.g2d.G2DParentNode;
41 import org.simantics.scenegraph.g2d.nodes.connection.IRouteGraphListener;
42 import org.simantics.scenegraph.g2d.nodes.connection.RouteGraphNode;
43 import org.simantics.utils.datastructures.hints.IHintContext.Key;
44 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
47 * An element class for single connection entity elements. A connection entity
48 * consists of connection edge segments and branch points as its children.
50 * @author Tuukka Lehtonen
52 public class RouteGraphConnectionClass {
54 public static final Key KEY_ROUTEGRAPH = new KeyOf(RouteGraph.class, "ROUTE_GRAPH");
55 public static final Key KEY_RENDERER = new KeyOf(IRouteGraphRenderer.class, "ROUTE_GRAPH_RENDERER");
56 public static final Key KEY_PICK_TOLERANCE = new KeyOf(Double.class, "PICK_TOLERANCE");
57 public static final Key KEY_USE_TOLERANCE_IN_SELECTION = new KeyOf(Boolean.class, "PICK_TOLERANCE_SELECTION");
58 public static final Key KEY_RG_LISTENER = new KeyOf(IRouteGraphListener.class, "ROUTE_GRAPH_LISTENER");
59 public static final Key KEY_RG_NODE = new SceneGraphNodeKey(RouteGraphNode.class, "ROUTE_GRAPH_NODE");
61 public static final double BOUND_TOLERANCE = 0.9;
63 public static final ElementClass CLASS =
67 FixedTransform.INSTANCE,
69 ConnectionBoundsAndPick.INSTANCE,
70 ConnectionSelectionOutline.INSTANCE,
71 ConnectionHandlerImpl.INSTANCE,
72 ConnectionSceneGraph.INSTANCE,
73 //SimpleElementLayers.INSTANCE,
75 // Exists only loading connection visuals through ConnectionVisualsLoader
76 ConfigurableEdgeVisuals.DEFAULT,
78 ).setId(RouteGraphConnectionClass.class.getSimpleName());
81 static class ConnectionHandlerImpl implements ConnectionHandler {
83 public static final ConnectionHandlerImpl INSTANCE = new ConnectionHandlerImpl();
85 private static final long serialVersionUID = 3267139233182458330L;
88 public Collection<IElement> getBranchPoints(IElement connection, Collection<IElement> result) {
89 return Collections.<IElement>emptySet();
93 public Collection<IElement> getChildren(IElement connection, Collection<IElement> result) {
94 return Collections.emptySet();
98 public Collection<IElement> getSegments(IElement connection, Collection<IElement> result) {
99 return Collections.<IElement>emptySet();
103 public Collection<Connection> getTerminalConnections(IElement connection, Collection<Connection> result) {
104 ConnectionEntity ce = connection.getHint(ElementHints.KEY_CONNECTION_ENTITY);
106 return Collections.<Connection>emptySet();
107 return ce.getTerminalConnections(result);
112 static final class ConnectionSceneGraph implements SceneGraph {
114 public static final ConnectionSceneGraph INSTANCE = new ConnectionSceneGraph();
116 private static final long serialVersionUID = 4232871859964883266L;
119 public void init(IElement connection, G2DParentNode parent) {
120 RouteGraph rg = connection.getHint(KEY_ROUTEGRAPH);
121 IRouteGraphRenderer renderer = connection.getHint(KEY_RENDERER);
122 if (rg == null || renderer == null) {
125 RouteGraphNode rgn = connection.getHint(KEY_RG_NODE);
127 rgn = parent.addNode(ElementUtils.generateNodeId(connection), RouteGraphNode.class);
128 connection.setHint(KEY_RG_NODE, rgn);
130 rgn.setRouteGraph(rg);
131 rgn.setRenderer(renderer);
133 IRouteGraphListener listener = connection.getHint(KEY_RG_LISTENER);
134 rgn.setRouteGraphListener(listener);
136 Double tolerance = connection.getHint(KEY_PICK_TOLERANCE);
137 if (tolerance != null)
138 rgn.setPickTolerance(tolerance);
143 public void cleanup(IElement connection) {
144 ElementUtils.removePossibleNode(connection, KEY_RG_NODE);
145 connection.removeHint(KEY_RG_NODE);
149 static final class ConnectionBoundsAndPick implements InternalSize, Outline, Pick {
151 private static final long serialVersionUID = 4232871859964883266L;
153 public static final ConnectionBoundsAndPick INSTANCE = new ConnectionBoundsAndPick();
155 // Single-threaded system, should be fine to use this for everything.
156 Rectangle2D temp = new Rectangle2D.Double();
158 private Shape getSelectionShape(IElement e) {
159 for (SelectionOutline so : e.getElementClass().getItemsByClass(SelectionOutline.class)) {
160 Shape shape = so.getSelectionShape(e);
164 // Using on-diagram coordinates because neither connections nor
165 // edges have a non-identity transform which means that
166 // coordinates are always absolute. Therefore branch point
167 // shape also needs to be calculated in absolute coordinates.
168 Shape shape = ElementUtils.getElementShapeOrBoundsOnDiagram(e);
173 public boolean pickTest(IElement e, Shape s, PickPolicy policy) {
174 RouteGraph rg = getRouteGraph(e);
178 Rectangle2D bounds = getBounds(s);
180 case PICK_CONTAINED_OBJECTS:
181 Shape selectionShape = getSelectionShape(e);
182 return bounds.contains(selectionShape.getBounds2D());
183 case PICK_INTERSECTING_OBJECTS:
184 double tolerance = 0.0;
185 if (e.containsHint(KEY_USE_TOLERANCE_IN_SELECTION))
186 tolerance = getTolerance(e);
188 tolerance = (bounds.getHeight()+bounds.getHeight()) * 0.25;
189 Object node = rg.pickLine(bounds.getCenterX(), bounds.getCenterY(), tolerance);
196 public Rectangle2D getBounds(IElement e, Rectangle2D size) {
197 RouteGraph rg = getRouteGraph(e);
200 size = new Rectangle2D.Double();
207 public Shape getElementShape(IElement e) {
208 RouteGraph rg = getRouteGraph(e);
209 return rg == null ? null : rg.getPath2D();
212 private Rectangle2D getBounds(Shape shape) {
213 if (shape instanceof Rectangle2D)
214 return (Rectangle2D) shape;
215 return shape.getBounds2D();
218 private RouteGraph getRouteGraph(IElement e) {
219 RouteGraphNode rgn = e.getHint(KEY_RG_NODE);
220 return rgn == null ? null : rgn.getRouteGraph();
223 private double getTolerance(IElement e) {
224 RouteGraphNode rgn = e.getHint(KEY_RG_NODE);
225 return rgn.getPickTolerance();
230 public static int shortestDirectionOutOfBounds(double x, double y, Rectangle2D bounds) {
231 double mx = bounds.getMinX();
232 double Mx = bounds.getMaxX();
233 double my = bounds.getMinY();
234 double My = bounds.getMaxY();
237 double down = My - y;
238 double left = x - mx;
239 double right = Mx - x;
242 double[] dists = { right, down, left, up };
243 byte[] masks = { 0x1, 0x2, 0x4, 0x8 };
244 for (int i = 1; i < 4; ++i) {
245 double value = dists[i];
246 byte mask = masks[i];
248 while (j >= 0 && dists[j] > value) {
249 dists[j + 1] = dists[j];
250 masks[j + 1] = masks[j];
253 dists[j + 1] = value;
257 // Construct mask out of the shortest equal directions
259 double value = dists[0] / BOUND_TOLERANCE;
260 for (int i = 1; i < 4; ++i) {
261 if (dists[i] > value)