1 package org.simantics.district.network.ui.nodes;
3 import java.awt.BasicStroke;
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;
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;
24 public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelectionPainterNode {
26 private static final long serialVersionUID = 8049769475036519806L;
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);
31 private DistrictNetworkEdge edge;
32 private Rectangle2D bounds;
33 private transient Path2D path;
35 private boolean scaleStroke = true;
37 private Double stroke;
38 private transient Color dynamicColor = null;
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;
46 private static final Rectangle2D NORMAL = new Rectangle2D.Double(left, top, width, height);
48 private transient Point2D centerPoint;
49 private transient Rectangle2D symbolRect;
50 private transient AffineTransform symbolTransform;
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());
65 Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
66 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
68 Color oldColor = g2d.getColor();
69 BasicStroke oldStroke = (BasicStroke) g2d.getStroke();
71 BasicStroke bs = null;
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));
80 int zoomLevel = MapScalingTransform.zoomLevel(ot);
81 path = calculatePath(edge, path, zoomLevel > 15);
84 g2d.setColor(SELECTION_COLOR);
85 g2d.setStroke(GeometryUtils.scaleStroke(bs, 4f));
89 g2d.setColor(dynamicColor != null ? dynamicColor : color);
94 g2d.setStroke(oldStroke);
95 g2d.setColor(oldColor);
98 double viewScaleRecip = 10;
100 double scale = GeometryUtils.getScale(g2d.getTransform());
101 scale = Math.max(10000, Math.min(scale, 50000));
102 viewScaleRecip /= scale;
105 Point2D p = getCenterPoint();
106 symbolRect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, NORMAL, symbolRect, viewScaleRecip);
107 symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(symbolRect, symbolTransform);
109 for (INode nn : getNodes()) {
110 G2DNode g2dNode = (G2DNode)nn;
111 g2dNode.setTransform(symbolTransform);
115 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
118 g2d.setTransform(ot);
121 private Point2D getCenterPoint() {
122 if (centerPoint == null)
123 centerPoint = new Point2D.Double();
125 Rectangle2D bounds = path.getBounds2D();
126 centerPoint.setLocation(bounds.getCenterX(), bounds.getCenterY());
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
137 // if (result == null)
138 // result = new Line2D.Double();
139 // result.setLine(startX, startY, endX, endY);
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
150 if (result == null) {
151 result = new Path2D.Double();
155 result.moveTo(startX, startY);
157 double[] detailedGeometry = edge.getGeometry();
158 if (detailedGeometry != null && !DistrictNetworkEdgeElementFactory.EMPTY.equals(detailedGeometry)) {
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
168 result.lineTo(endX, endY);
172 private boolean isSelected() {
173 return NodeUtil.isSelected(this, 1);
177 public Rectangle2D getBoundsInLocal() {
181 private void updateBounds() {
182 Rectangle2D oldBounds = bounds;
183 if (oldBounds == null)
184 oldBounds = new Rectangle2D.Double();
185 bounds = calculateBounds(oldBounds);
188 private Rectangle2D calculateBounds(Rectangle2D rect) {
189 return calculatePath(edge, null, false).getBounds2D();
192 public void setDNEdge(DistrictNetworkEdge edge) {
197 public void setColor(Color color) {
201 public Color getColor() {
205 @PropertySetter(value = "stroke")
206 public void setStroke(Double stroke) {
207 this.stroke = stroke;
210 @PropertySetter(value = "dynamicColor")
211 public void setDynamicColor(Color color) {
212 this.dynamicColor = color;
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);