]> gerrit.simantics Code Review - simantics/district.git/blob
00a1427f5f2e6d07f72c6770364f94892afb64c1
[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.Line2D;
9 import java.awt.geom.Path2D;
10 import java.awt.geom.Point2D;
11 import java.awt.geom.Rectangle2D;
12
13 import org.simantics.district.network.ModelledCRS;
14 import org.simantics.district.network.ui.DistrictNetworkEdge;
15 import org.simantics.scenegraph.INode;
16 import org.simantics.scenegraph.ISelectionPainterNode;
17 import org.simantics.scenegraph.g2d.G2DNode;
18 import org.simantics.scenegraph.g2d.G2DParentNode;
19 import org.simantics.scenegraph.g2d.nodes.SVGNode;
20 import org.simantics.scenegraph.utils.GeometryUtils;
21 import org.simantics.scenegraph.utils.NodeUtil;
22
23 public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelectionPainterNode {
24
25     private static final long serialVersionUID = 8049769475036519806L;
26
27     private static final BasicStroke STROKE           = new BasicStroke(4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
28     private static final Color       SELECTION_COLOR  = new Color(255, 0, 255, 96);
29
30     private DistrictNetworkEdge edge;
31     private Rectangle2D bounds;
32     private transient Line2D path;
33
34     private boolean scaleStroke = true;
35     private Color color;
36     private Double stroke;
37     private transient Color dynamicColor = null;
38
39     // Dimensions for shut-off valve symbol
40     private static final double left = -0.25;
41     private static final double top = -0.25;
42     private static final double width = 0.5;
43     private static final double height = 0.5;
44
45     private static final Rectangle2D NORMAL = new Rectangle2D.Double(left, top, width, height);
46     
47     private transient Point2D centerPoint;
48     private transient Rectangle2D symbolRect;
49     private transient AffineTransform symbolTransform;
50     
51     @Override
52     public void init() {
53     }
54
55     @Override
56     public void render(Graphics2D g2d) {
57         AffineTransform ot = null;
58         AffineTransform t = getTransform();
59         if (t != null && !t.isIdentity()) {
60             ot = g2d.getTransform();
61             g2d.transform(getTransform());
62         }
63
64         Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
65         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
66
67         Color oldColor = g2d.getColor();
68         BasicStroke oldStroke = (BasicStroke) g2d.getStroke();
69
70         BasicStroke bs = null;
71         if (scaleStroke) {
72             double scale = GeometryUtils.getScale(g2d.getTransform());
73             scale = Math.max(10000, Math.min(scale, 50000));
74             double str = stroke != null ? Math.abs(stroke) : 1.0;
75             bs = GeometryUtils.scaleStroke(STROKE, (float) (str / scale));
76         } else {
77             bs = STROKE;
78         }
79
80         path = calculateLine(edge, path);
81
82         if (isSelected()) {
83             g2d.setColor(SELECTION_COLOR);
84             g2d.setStroke(GeometryUtils.scaleStroke(bs, 4f));
85             g2d.draw(path);
86         }
87
88         g2d.setColor(dynamicColor != null ? dynamicColor : color);
89         g2d.setStroke(bs);
90         g2d.draw(path);
91
92         // Reset
93         g2d.setStroke(oldStroke);
94         g2d.setColor(oldColor);
95         
96         // Render SVG symbol
97         double viewScaleRecip = 10;
98         if (scaleStroke) {
99             double scale = GeometryUtils.getScale(g2d.getTransform());
100             scale = Math.max(10000, Math.min(scale, 50000));
101             viewScaleRecip /= scale;
102         }
103         
104         Point2D p = getCenterPoint();
105         symbolRect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, NORMAL, symbolRect, viewScaleRecip);
106         symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(symbolRect, symbolTransform);
107         
108         for (INode nn : getNodes()) {
109             G2DNode g2dNode = (G2DNode)nn;
110             g2dNode.setTransform(symbolTransform);
111             g2dNode.render(g2d);
112         }
113         
114         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
115
116         if (ot != null)
117             g2d.setTransform(ot);
118     }
119
120     private Point2D getCenterPoint() {
121         if (centerPoint == null)
122             centerPoint = new Point2D.Double();
123         
124         Rectangle2D bounds = path.getBounds2D();
125         centerPoint.setLocation(bounds.getCenterX(), bounds.getCenterY());
126         return centerPoint;
127     }
128
129     public static Line2D calculateLine(DistrictNetworkEdge edge, Line2D result) {
130         // Convert to screen coordinates
131         double startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX());
132         double startY = ModelledCRS.latitudeToY(-edge.getStartPoint().getY()); // Invert for Simantics
133         double endX = ModelledCRS.longitudeToX(edge.getEndPoint().getX());
134         double endY = ModelledCRS.latitudeToY(-edge.getEndPoint().getY());// Invert for Simantics
135
136         if (result == null)
137             result = new Line2D.Double();
138         result.setLine(startX, startY, endX, endY);
139         return result;
140     }
141
142     public static Path2D calculatePath(DistrictNetworkEdge edge, Path2D result) {
143         // Convert to screen coordinates
144         double startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX());
145         double startY = ModelledCRS.latitudeToY(-edge.getStartPoint().getY()); // Invert for Simantics
146         double endX = ModelledCRS.longitudeToX(edge.getEndPoint().getX());
147         double endY = ModelledCRS.latitudeToY(-edge.getEndPoint().getY());// Invert for Simantics
148
149         if (result == null) {
150              result = new Path2D.Double();
151         } else {
152              result.reset();
153         }
154         result.moveTo(startX, startY);
155         result.lineTo(endX, endY);
156         return result;
157     }
158
159     private boolean isSelected() {
160         return NodeUtil.isSelected(this, 1);
161     }
162
163     @Override
164     public Rectangle2D getBoundsInLocal() {
165         return bounds;
166     }
167
168     private void updateBounds() {
169         Rectangle2D oldBounds = bounds;
170         if (oldBounds == null)
171             oldBounds = new Rectangle2D.Double();
172         bounds = calculateBounds(oldBounds);
173     }
174
175     private Rectangle2D calculateBounds(Rectangle2D rect) {
176         return calculatePath(edge, null).getBounds2D();
177     }
178
179     public void setDNEdge(DistrictNetworkEdge edge) {
180         this.edge = edge;
181         updateBounds();
182     }
183
184     public void setColor(Color color) {
185         this.color = color;
186     }
187
188     public Color getColor() {
189         return color;
190     }
191
192     @PropertySetter(value = "stroke")
193     public void setStroke(Double stroke) {
194         this.stroke = stroke;
195     }
196
197     @PropertySetter(value = "dynamicColor")
198     public void setDynamicColor(Color color) {
199         this.dynamicColor = color;
200     }
201
202     @PropertySetter(value = "SVG")
203     public void setSVG(String value) {
204         for (INode nn : this.getNodes())
205             if (nn instanceof SVGNode)
206                 ((SVGNode)nn).setData(value);
207     }
208     
209 }