]> gerrit.simantics Code Review - simantics/district.git/blob
377c7f7e8d11840ff6c706b0798a52e9e61fe43f
[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 BasicStroke STROKE           = new BasicStroke(4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
27     private static final Color       SELECTION_COLOR  = new Color(255, 0, 255, 96);
28
29     private static final double left = -0.25;
30     private static final double top = -0.25;
31     private static final double width = 0.5;
32     private static final double height = 0.5;
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 = 1;
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(getTransform());
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 = 10;
81         if (scaleStroke) {
82             double scale = GeometryUtils.getScale(g2d.getTransform());
83             scale = Math.max(10000, Math.min(scale, 50000));
84             viewScaleRecip /= scale;
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*0.5));
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             symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(toDraw, symbolTransform);
115             g2dNode.setTransform(symbolTransform);
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         return new Rectangle2D.Double(calcPoint.getX(), calcPoint.getY(), width / at.getScaleX(), height / at.getScaleY()).getBounds2D();
156     }
157
158     public void setVertex(DistrictNetworkVertex vertex) {
159         this.vertex = vertex;
160         updateBounds();
161     }
162
163     public boolean hover(boolean hover) {
164 //        if (hover && LOGGER.isDebugEnabled())
165 //            LOGGER.debug("Hovering " + this);
166         boolean changed = false;
167         if (this.hover != hover) {
168             this.hover = hover;
169             changed = true;
170         }
171         return changed;
172     }
173
174     public void setColor(Color color) {
175         this.color = color;
176     }
177
178     public Color getColor() {
179         return color;
180     }
181
182     @PropertySetter(value = "SVG")
183     public void setSVG(String value) {
184         for (INode nn : this.getNodes())
185             if (nn instanceof SVGNode)
186                 ((SVGNode)nn).setData(value);
187     }
188     
189     
190     @PropertySetter(value = "size")
191     public void setSize(Double size) {
192         boolean changed = false;
193         if (size != null) {
194             changed = size != this.nodeSize;
195             this.nodeSize = size;
196         } else {
197             changed = this.nodeSize != 1.0;
198             this.nodeSize = 1.0;
199         }
200         if (changed)
201             updateBounds();
202     }
203
204     @PropertySetter(value = "dynamicColor")
205     public void setDynamicColor(Color color) {
206         this.dynamicColor = color;
207     }
208
209 }