]> gerrit.simantics Code Review - simantics/district.git/blob
0533e3117bf38b32a4f94191139fe55d8876e148
[simantics/district.git] /
1 package org.simantics.district.network.ui.nodes;
2
3 import java.awt.BasicStroke;
4 import java.awt.Color;
5 import java.awt.Graphics2D;
6 import java.awt.RenderingHints;
7 import java.awt.geom.AffineTransform;
8 import java.awt.geom.Point2D;
9 import java.awt.geom.Rectangle2D;
10
11 import org.simantics.district.network.ui.adapters.DistrictNetworkVertex;
12 import org.simantics.maps.MapScalingTransform;
13 import org.simantics.scenegraph.INode;
14 import org.simantics.scenegraph.ISelectionPainterNode;
15 import org.simantics.scenegraph.g2d.G2DNode;
16 import org.simantics.scenegraph.g2d.G2DParentNode;
17 import org.simantics.scenegraph.g2d.IG2DNode;
18 import org.simantics.scenegraph.g2d.nodes.SVGNode;
19 import org.simantics.scenegraph.utils.GeometryUtils;
20 import org.simantics.scenegraph.utils.NodeUtil;
21
22 public class DistrictNetworkVertexNode extends G2DParentNode implements ISelectionPainterNode, HoverSensitiveNode {
23
24     //private static final Logger LOGGER = LoggerFactory.getLogger(DistrictNetworkVertexNode.class);
25
26     private static final long serialVersionUID = -2641639101400236719L;
27
28     private static final double left = -15;
29     private static final double top = left;
30     public static final double width = 30;
31     private static final double height = width;
32
33     private static final BasicStroke STROKE           = new BasicStroke((float)width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
34     private static final Color       SELECTION_COLOR  = new Color(255, 0, 255, 96);
35
36     private static final Rectangle2D NORMAL = new Rectangle2D.Double(left, top, width, height);
37     private static final Rectangle2D HOVERED = new Rectangle2D.Double(left * 2, top * 2, width * 2, height * 2);
38
39     private DistrictNetworkVertex vertex;
40
41     private boolean scaleStroke = true;
42     private boolean hover;
43
44     private Color color;
45     private transient Color dynamicColor;
46
47     private Rectangle2D bounds;
48     private transient Point2D point;
49     private transient Rectangle2D rect;
50     private transient AffineTransform symbolTransform;
51
52     private double nodeSize = 1.0;
53
54     @Override
55     public void init() {
56         setZIndex(2);
57     }
58
59     @Override
60     public void render(Graphics2D g2d) {
61         if (nodeSize <= 0.0)
62             return;
63
64         AffineTransform ot = null;
65         AffineTransform t = getTransform();
66         if (t != null && !t.isIdentity()) {
67             ot = g2d.getTransform();
68             g2d.transform(t);
69         }
70
71         Object oaaHint = null;
72         Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
73         if (aaHint != RenderingHints.VALUE_ANTIALIAS_OFF) {
74             oaaHint = aaHint;
75             g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
76         }
77
78         Color oldColor = g2d.getColor();
79         Color newColor = dynamicColor != null ? dynamicColor : color;
80         boolean changeColor = !oldColor.equals(newColor);
81
82         double viewScaleRecip = 1;
83         if (scaleStroke) {
84             viewScaleRecip *= DistrictNetworkNodeUtils.calculateScaleRecip(g2d.getTransform());
85         }
86         double scaleRecip = viewScaleRecip * nodeSize;
87
88         // Translate lat and lon to X and Y
89         Point2D p = point = DistrictNetworkNodeUtils.calculatePoint2D(vertex.getPoint(), point);
90         Rectangle2D toDraw = rect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, hover ? HOVERED : NORMAL, rect, scaleRecip);
91
92         if (NodeUtil.isSelected(this, 1)) {
93             changeColor = true;
94             g2d.setColor(SELECTION_COLOR);
95             BasicStroke ss = GeometryUtils.scaleStroke(STROKE, (float)viewScaleRecip);
96             g2d.setStroke(ss);
97             g2d.draw(toDraw);
98         }
99
100         // render
101         if (changeColor)
102             g2d.setColor(newColor);
103         g2d.fill(toDraw);
104         
105         // Reset settings
106         if (changeColor)
107             g2d.setColor(oldColor);
108         if (oaaHint != null)
109             g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
110
111         // Render SVG symbol
112         for (INode nn : getNodes()) {
113             G2DNode g2dNode = (G2DNode)nn;
114             if (nn instanceof SVGNode) {
115                 symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(toDraw, symbolTransform);
116                 g2dNode.setTransform(symbolTransform);
117             }
118             g2dNode.render(g2d);
119         }
120         
121         if (ot != null)
122             g2d.setTransform(ot);
123     }
124
125     @Override
126     public Rectangle2D getBounds() {
127         return super.getBounds();
128     }
129
130     @Override
131     public Rectangle2D getBoundsInLocal() {
132         updateBounds();
133         return bounds;
134     }
135
136     private void updateBounds() {
137         Rectangle2D oldBounds = bounds;
138         if (oldBounds == null)
139             oldBounds = new Rectangle2D.Double();
140         bounds = calculateBounds(oldBounds);
141     }
142
143     @Override
144     public void setTransform(AffineTransform transform) {
145         super.setTransform(transform);
146         // Update bounds
147         updateBounds();
148     }
149
150     @Override
151     public AffineTransform getTransform() {
152         return super.getTransform();
153     }
154
155     private Rectangle2D calculateBounds(Rectangle2D rect) {
156         Point2D calcPoint = DistrictNetworkNodeUtils.calculatePoint2D(vertex.getPoint(), point);
157         AffineTransform at = NodeUtil.getLocalToGlobalTransform(this);
158         at.concatenate(MapScalingTransform.INSTANCE);
159         double x = calcPoint.getX();
160         double y = calcPoint.getY();
161         double scaleRecip = DistrictNetworkNodeUtils.calculateScaleRecip(at);
162         double widthh = width * scaleRecip * nodeSize;
163         double heighth = height * scaleRecip * nodeSize;
164         if (rect == null)
165             rect = new Rectangle2D.Double();
166         rect.setRect(x - widthh/2, y - heighth/2, widthh, heighth);
167         return rect;
168     }
169
170     public void setVertex(DistrictNetworkVertex vertex) {
171         this.vertex = vertex;
172         updateBounds();
173     }
174
175     @Override
176     public boolean hover(boolean hover, boolean isConnectionTool) {
177         // Only react to hover when the connection tool is active
178         boolean doHover = hover && isConnectionTool;
179         boolean changed = this.hover != doHover;
180         this.hover = doHover;
181         
182         for (IG2DNode child : getNodes()) {
183             if (child instanceof HoverSensitiveNode)
184                 changed = ((HoverSensitiveNode)child).hover(hover, isConnectionTool) || changed;
185         }
186         
187         return changed;
188     }
189
190     public void setColor(Color color) {
191         this.color = color;
192     }
193
194     public Color getColor() {
195         return color;
196     }
197
198     @PropertySetter(value = "SVG")
199     public void setSVG(String value) {
200         for (INode nn : this.getNodes())
201             if (nn instanceof SVGNode)
202                 ((SVGNode)nn).setData(value);
203     }
204     
205     
206     @PropertySetter(value = "size")
207     public void setSize(Double size) {
208         boolean changed = false;
209         if (size != null) {
210             changed = size != this.nodeSize;
211             this.nodeSize = size;
212         } else {
213             changed = this.nodeSize != 1.0;
214             this.nodeSize = 1.0;
215         }
216         if (changed)
217             updateBounds();
218     }
219
220     @PropertySetter(value = "dynamicColor")
221     public void setDynamicColor(Color color) {
222         this.dynamicColor = color;
223     }
224
225 }