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