]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/ConnectionNode.java
Configurable connection crossing styles
[simantics/platform.git] / bundles / org.simantics.scenegraph / src / org / simantics / scenegraph / g2d / nodes / ConnectionNode.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.scenegraph.g2d.nodes;
13
14 import java.awt.Color;
15 import java.awt.Composite;
16 import java.awt.Graphics2D;
17 import java.awt.Stroke;
18 import java.awt.geom.Point2D;
19 import java.awt.geom.Rectangle2D;
20
21 import org.simantics.diagram.connection.RouteGraph;
22 import org.simantics.scenegraph.INode;
23 import org.simantics.scenegraph.g2d.G2DRenderingHints;
24 import org.simantics.scenegraph.g2d.IG2DNode;
25 import org.simantics.scenegraph.g2d.events.MouseEvent;
26 import org.simantics.scenegraph.g2d.nodes.connection.RouteGraphNode;
27 import org.simantics.scenegraph.utils.InitValueSupport;
28 import org.simantics.scenegraph.utils.NodeUtil;
29 import org.simantics.scenegraph.utils.NodeUtil.NodeProcedure;
30
31 /**
32  * @author Tuukka Lehtonen
33  */
34 public class ConnectionNode extends SingleElementNode implements InitValueSupport {
35
36     private static final long serialVersionUID = 8657421058765967521L;
37
38     private static class SetAlphaCompositeProcedure implements NodeProcedure<Object> {
39         public Composite composite;
40         @Override
41         public Object execute(INode node, String id) {
42             if (node instanceof EdgeNode)
43                 ((EdgeNode) node).setAlphaComposite(composite);
44 //          NodeUtil.setPropertyIfSupported("alpha", alphaComposite, edge);
45             return null;
46         }
47     };
48
49     private static class SetDynamicStrokeProcedure implements NodeProcedure<Object> {
50         public Stroke stroke;
51         @Override
52         public Object execute(INode node, String id) {
53             if (node instanceof EdgeNode)
54                 ((EdgeNode) node).setDynamicStroke(stroke);
55             else if (node instanceof RouteGraphNode)
56                 ((RouteGraphNode) node).setDynamicStroke(stroke);
57 //          NodeUtil.setPropertyIfSupported("width", stroke, edge);
58             return null;
59         }
60     };
61
62     private static class SetDynamicColorProcedure implements NodeProcedure<Object> {
63         public Color color;
64         @Override
65         public Object execute(INode node, String id) {
66             if (node instanceof EdgeNode)
67                 ((EdgeNode) node).setDynamicColor(color);
68             else if (node instanceof RouteGraphNode)
69                 ((RouteGraphNode) node).setDynamicColor(color);
70 //          NodeUtil.setPropertyIfSupported("color", color, edge);
71             return null;
72         }
73     };
74
75 //    private static class SetValueProcedure implements NodeProcedure<Object> {
76 //        public String key;
77 //        public Object value;
78 //        @Override
79 //        public Object execute(INode node, String id) {
80 //            NodeUtil.setPropertyIfSupported(key, value, node);
81 ////            edge.setValue(key, value);
82 //            return null;
83 //        }
84 //    };
85
86     private static final SetAlphaCompositeProcedure setAlphaCompositeProcedure = new SetAlphaCompositeProcedure();
87     private static final SetDynamicStrokeProcedure  setDynamicStrokeProcedure  = new SetDynamicStrokeProcedure();
88     private static final SetDynamicColorProcedure   setDynamicColorProcedure   = new SetDynamicColorProcedure();
89
90     private static NodeProcedure<?> initValuesProcedure = new NodeProcedure<Object>() {
91         @Override
92         public Object execute(INode node, String id) {
93             if (node instanceof InitValueSupport)
94                 ((InitValueSupport) node).initValues();
95             return null;
96         }
97     };
98
99     @PropertySetter("alpha")
100     public void setAlphaComposite(Composite alphaComposite) {
101         setAlphaCompositeProcedure.composite = alphaComposite;
102         forEdges(this, setAlphaCompositeProcedure);
103     }
104     
105     @PropertySetter("width")
106     public void setDynamicStroke(Stroke stroke) {
107         setDynamicStrokeProcedure.stroke = stroke;
108         forEdges(this, setDynamicStrokeProcedure);
109     }
110
111     @PropertySetter("color")
112     public void setDynamicColor(Color color) {
113         setDynamicColorProcedure.color = color;
114         forEdges(this, setDynamicColorProcedure);
115     }
116
117     @Override
118     public void initValues() {
119         NodeUtil.forChildren(this, initValuesProcedure, null);
120     }
121
122     private void forEdges(SingleElementNode node, NodeProcedure<?> procedure) {
123         for (String childId : node.getSortedNodesById()) {
124             IG2DNode child = node.getNode(childId);
125             if (child instanceof SingleElementNode) {
126                 forEdges((SingleElementNode) child, procedure);
127             } else if (child instanceof EdgeNode) {
128                 procedure.execute(child, childId);
129             } else if (child instanceof RouteGraphNode) {
130                 procedure.execute(child, childId);
131             }
132         }
133     }
134
135     @Override
136     protected boolean hitTest(MouseEvent event) {
137         if (!super.hitTest(event))
138             return false;
139
140         Point2D pos = NodeUtil.worldToLocal(this, event.controlPosition, new Point2D.Double());
141         double tolerance = 0.5;
142         // TODO: change tolerance based on zoom level
143
144         for (IG2DNode child : getSortedNodes()) {
145             if (child instanceof RouteGraphNode) {
146                 RouteGraphNode rgn = (RouteGraphNode) child;
147                 RouteGraph rg = rgn.getRouteGraph();
148                 Object pick = rg.pickLine(pos.getX(), pos.getY(), tolerance);
149                 if (pick != null)
150                     return true;
151             }
152         }
153         return false;
154     }
155
156     @Override
157     public void beforeRender(Graphics2D g) {
158         g.setRenderingHint(G2DRenderingHints.KEY_BEGIN_ELEMENT, "connection");
159     }
160     
161     @Override
162     public void afterRender(Graphics2D g) {
163         g.setRenderingHint(G2DRenderingHints.KEY_END_ELEMENT, "connection");
164     }
165
166     @Override
167     public Rectangle2D getBoundsInLocal() {
168         // #134: Route graph connections render their own selection.
169         // ElementPainter will place an empty G2DParentNode
170         // called "selection" under this ConnectionNode which
171         // should be ignored in bounds calculations.
172         // Otherwise this node will not support being inserted
173         // into a spatial search structure and further selections
174         // will fail.
175         return super.getBoundsInLocal(true);
176     }
177
178 }