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