]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/connection/RouteGraphConnectionClass.java
Some enhancements to GraphLayer-related utilities for Diagram layers
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / connection / RouteGraphConnectionClass.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.diagram.connection;
13
14 import java.awt.Shape;
15 import java.awt.geom.Rectangle2D;
16 import java.util.Collection;
17 import java.util.Collections;
18
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;
44
45 /**
46  * An element class for single connection entity elements. A connection entity
47  * consists of connection edge segments and branch points as its children.
48  * 
49  * @author Tuukka Lehtonen
50  */
51 public class RouteGraphConnectionClass {
52
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");
59     
60     public static final double       BOUND_TOLERANCE = 0.9;
61
62     public static final ElementClass CLASS =
63         ElementClass.compile(
64                 TextImpl.INSTANCE,
65
66                 FixedTransform.INSTANCE,
67
68                 ConnectionBoundsAndPick.INSTANCE,
69                 ConnectionSelectionOutline.INSTANCE,
70                 ConnectionHandlerImpl.INSTANCE,
71                 ConnectionSceneGraph.INSTANCE,
72                 //SimpleElementLayers.INSTANCE,
73
74                 // Exists only loading connection visuals through ConnectionVisualsLoader
75                 ConfigurableEdgeVisuals.DEFAULT,
76                 FillColorImpl.BLACK
77         ).setId(RouteGraphConnectionClass.class.getSimpleName());
78
79
80     static class ConnectionHandlerImpl implements ConnectionHandler {
81
82         public static final ConnectionHandlerImpl INSTANCE = new ConnectionHandlerImpl();
83
84         private static final long serialVersionUID = 3267139233182458330L;
85
86         @Override
87         public Collection<IElement> getBranchPoints(IElement connection, Collection<IElement> result) {
88             return Collections.<IElement>emptySet();
89         }
90
91         @Override
92         public Collection<IElement> getChildren(IElement connection, Collection<IElement> result) {
93             return Collections.emptySet();
94         }
95
96         @Override
97         public Collection<IElement> getSegments(IElement connection, Collection<IElement> result) {
98             return Collections.<IElement>emptySet();
99         }
100
101         @Override
102         public Collection<Connection> getTerminalConnections(IElement connection, Collection<Connection> result) {
103             ConnectionEntity ce = connection.getHint(ElementHints.KEY_CONNECTION_ENTITY);
104             if (ce == null)
105                 return Collections.<Connection>emptySet();
106             return ce.getTerminalConnections(result);
107         }
108
109     }
110
111     static final class ConnectionSceneGraph implements SceneGraph {
112
113         public static final ConnectionSceneGraph INSTANCE = new ConnectionSceneGraph();
114
115         private static final long serialVersionUID = 4232871859964883266L;
116
117         @Override
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) {
122                 cleanup(connection);
123             } else {
124                 RouteGraphNode rgn = connection.getHint(KEY_RG_NODE);
125                 if (rgn == null) {
126                     rgn = parent.addNode(ElementUtils.generateNodeId(connection), RouteGraphNode.class);
127                     connection.setHint(KEY_RG_NODE, rgn);
128                 }
129                 rgn.setRouteGraph(rg);
130                 rgn.setRenderer(renderer);
131
132                 IRouteGraphListener listener = connection.getHint(KEY_RG_LISTENER);
133                 rgn.setRouteGraphListener(listener);
134
135                 Double tolerance = connection.getHint(KEY_PICK_TOLERANCE);
136                 if (tolerance != null)
137                     rgn.setPickTolerance(tolerance);
138             }
139         }
140
141         @Override
142         public void cleanup(IElement connection) {
143             ElementUtils.removePossibleNode(connection, KEY_RG_NODE);
144             connection.removeHint(KEY_RG_NODE);
145         }
146     }
147
148     static final class ConnectionBoundsAndPick implements InternalSize, Outline, Pick {
149
150         private static final long serialVersionUID = 4232871859964883266L;
151
152         public static final ConnectionBoundsAndPick INSTANCE = new ConnectionBoundsAndPick();
153
154         // Single-threaded system, should be fine to use this for everything.
155         Rectangle2D temp = new Rectangle2D.Double();
156
157         private Shape getSelectionShape(IElement e) {
158             for (SelectionOutline so : e.getElementClass().getItemsByClass(SelectionOutline.class)) {
159                 Shape shape = so.getSelectionShape(e);
160                 if (shape != null)
161                     return shape;
162             }
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);
168             return shape;
169         }
170
171         @Override
172         public boolean pickTest(IElement e, Shape s, PickPolicy policy) {
173             RouteGraph rg = getRouteGraph(e);
174             if (rg == null)
175                 return false;
176
177             Rectangle2D bounds = getBounds(s);
178             switch (policy) {
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);
186                         else
187                                 tolerance = (bounds.getHeight()+bounds.getHeight()) * 0.25;
188                         Object node = rg.pickLine(bounds.getCenterX(), bounds.getCenterY(), tolerance);
189                     return node != null;
190             }
191             return false;
192         }
193
194         @Override
195         public Rectangle2D getBounds(IElement e, Rectangle2D size) {
196             RouteGraph rg = getRouteGraph(e);
197             if (rg != null) {
198                 if (size == null)
199                     size = new Rectangle2D.Double();
200                 rg.getBounds(size);
201             }
202             return size;
203         }
204
205         @Override
206         public Shape getElementShape(IElement e) {
207             RouteGraph rg = getRouteGraph(e);
208             return rg == null ? null : rg.getPath2D();
209         }
210
211         private Rectangle2D getBounds(Shape shape) {
212             if (shape instanceof Rectangle2D)
213                 return (Rectangle2D) shape;
214             return shape.getBounds2D();
215         }
216
217         private RouteGraph getRouteGraph(IElement e) {
218             RouteGraphNode rgn = e.getHint(KEY_RG_NODE);
219             return rgn == null ? null : rgn.getRouteGraph();
220         }
221         
222         private double getTolerance(IElement e) {
223                 RouteGraphNode rgn = e.getHint(KEY_RG_NODE);
224                 return rgn.getPickTolerance();
225         }
226
227     }
228
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();
234
235         double up = y - my;
236         double down = My - y;
237         double left = x - mx;
238         double right = Mx - x;
239
240         // Insertion sort
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];
246             int j = i - 1;
247             while (j >= 0 && dists[j] > value) {
248                 dists[j + 1] = dists[j];
249                 masks[j + 1] = masks[j];
250                 --j;
251             }
252             dists[j + 1] = value;
253             masks[j + 1] = mask;
254         }
255
256         // Construct mask out of the shortest equal directions 
257         int mask = masks[0];
258         double value = dists[0] / BOUND_TOLERANCE;
259         for (int i = 1; i < 4; ++i) {
260             if (dists[i] > value)
261                 break;
262             mask |= masks[i];
263         }
264         return mask;
265     }
266
267 }