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.scenegraph.g2d.nodes;
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;
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;
32 * @author Tuukka Lehtonen
34 public class ConnectionNode extends SingleElementNode implements InitValueSupport {
36 private static final long serialVersionUID = 8657421058765967521L;
38 private static class SetAlphaCompositeProcedure implements NodeProcedure<Object> {
39 public Composite composite;
41 public Object execute(INode node, String id) {
42 if (node instanceof EdgeNode)
43 ((EdgeNode) node).setAlphaComposite(composite);
44 // NodeUtil.setPropertyIfSupported("alpha", alphaComposite, edge);
49 private static class SetDynamicStrokeProcedure implements NodeProcedure<Object> {
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);
62 private static class SetDynamicColorProcedure implements NodeProcedure<Object> {
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);
75 // private static class SetValueProcedure implements NodeProcedure<Object> {
77 // public Object value;
79 // public Object execute(INode node, String id) {
80 // NodeUtil.setPropertyIfSupported(key, value, node);
81 //// edge.setValue(key, value);
86 private static final SetAlphaCompositeProcedure setAlphaCompositeProcedure = new SetAlphaCompositeProcedure();
87 private static final SetDynamicStrokeProcedure setDynamicStrokeProcedure = new SetDynamicStrokeProcedure();
88 private static final SetDynamicColorProcedure setDynamicColorProcedure = new SetDynamicColorProcedure();
90 private static NodeProcedure<?> initValuesProcedure = new NodeProcedure<Object>() {
92 public Object execute(INode node, String id) {
93 if (node instanceof InitValueSupport)
94 ((InitValueSupport) node).initValues();
99 @PropertySetter("alpha")
100 public void setAlphaComposite(Composite alphaComposite) {
101 setAlphaCompositeProcedure.composite = alphaComposite;
102 forEdges(this, setAlphaCompositeProcedure);
105 @PropertySetter("width")
106 public void setDynamicStroke(Stroke stroke) {
107 setDynamicStrokeProcedure.stroke = stroke;
108 forEdges(this, setDynamicStrokeProcedure);
111 @PropertySetter("color")
112 public void setDynamicColor(Color color) {
113 setDynamicColorProcedure.color = color;
114 forEdges(this, setDynamicColorProcedure);
118 public void initValues() {
119 NodeUtil.forChildren(this, initValuesProcedure, null);
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);
136 protected boolean hitTest(MouseEvent event) {
137 if (!super.hitTest(event))
140 Point2D pos = NodeUtil.worldToLocal(this, event.controlPosition, new Point2D.Double());
141 double tolerance = 0.5;
142 // TODO: change tolerance based on zoom level
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);
157 public void beforeRender(Graphics2D g) {
158 g.setRenderingHint(G2DRenderingHints.KEY_BEGIN_ELEMENT, "connection");
162 public void afterRender(Graphics2D g) {
163 g.setRenderingHint(G2DRenderingHints.KEY_END_ELEMENT, "connection");
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
175 return super.getBoundsInLocal(true);