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.Line2D;
9 import java.awt.geom.Path2D;
10 import java.awt.geom.Point2D;
11 import java.awt.geom.Rectangle2D;
13 import org.simantics.district.network.ModelledCRS;
14 import org.simantics.district.network.ui.DistrictNetworkEdge;
15 import org.simantics.district.network.ui.adapters.DistrictNetworkEdgeElementFactory;
16 import org.simantics.maps.MapScalingTransform;
17 import org.simantics.scenegraph.INode;
18 import org.simantics.scenegraph.ISelectionPainterNode;
19 import org.simantics.scenegraph.g2d.G2DNode;
20 import org.simantics.scenegraph.g2d.G2DParentNode;
21 import org.simantics.scenegraph.g2d.nodes.SVGNode;
22 import org.simantics.scenegraph.utils.GeometryUtils;
23 import org.simantics.scenegraph.utils.NodeUtil;
25 public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelectionPainterNode {
27 private static final long serialVersionUID = 8049769475036519806L;
29 private static final BasicStroke STROKE = new BasicStroke(4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
30 private static final Color SELECTION_COLOR = new Color(255, 0, 255, 96);
32 private DistrictNetworkEdge edge;
33 private Rectangle2D bounds;
34 private transient Path2D path;
36 private boolean scaleStroke = true;
38 private Double stroke;
39 private transient Color dynamicColor = null;
41 // Dimensions for shut-off valve symbol
42 private static final double left = -0.25;
43 private static final double top = -0.25;
44 private static final double width = 0.5;
45 private static final double height = 0.5;
47 private static final Rectangle2D NORMAL = new Rectangle2D.Double(left, top, width, height);
49 private transient Point2D centerPoint;
50 private transient Rectangle2D symbolRect;
51 private transient AffineTransform symbolTransform;
53 private Double arrowLength;
55 private static double startX;
56 private static double startY;
57 private static double endX;
58 private static double endY;
65 public void render(Graphics2D g2d) {
66 AffineTransform ot = null;
67 AffineTransform t = getTransform();
68 if (t != null && !t.isIdentity()) {
69 ot = g2d.getTransform();
70 g2d.transform(getTransform());
73 Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
74 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
76 Color oldColor = g2d.getColor();
77 BasicStroke oldStroke = (BasicStroke) g2d.getStroke();
79 BasicStroke bs = null;
81 AffineTransform tr = g2d.getTransform();
82 scale = DistrictNetworkNodeUtils.getScale(tr);
83 bs = GeometryUtils.scaleStroke(STROKE, getStrokeWidth(scale));
87 int zoomLevel = MapScalingTransform.zoomLevel(ot);
88 path = calculatePath(edge, path, zoomLevel > 15);
91 g2d.setColor(SELECTION_COLOR);
92 g2d.setStroke(GeometryUtils.scaleAndOffsetStrokeWidth(bs, 1.f, (float)(2 * STROKE.getLineWidth() / scale)));
96 g2d.setColor(dynamicColor != null ? dynamicColor : color);
101 if (arrowLength != null) {
102 g2d.setColor(Color.BLACK);
103 g2d.setStroke(new BasicStroke(bs.getLineWidth(), BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
105 double l = arrowLength;
106 double w = 2 * (double) bs.getLineWidth() * Math.signum(l);
107 if (Math.abs(w) > Math.abs(l)) w = l;
108 double offset = 2 * (double) bs.getLineWidth();
110 double centerX = (startX + endX) / 2, centerY = (startY + endY) / 2;
111 double deltaX = endX - startX, deltaY = endY - startY;
112 double length = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
116 double x0 = centerX - l/2 * deltaX + offset * deltaY;
117 double y0 = centerY - l/2 * deltaY - offset * deltaX;
118 double x1 = centerX + (l/2 - w) * deltaX + offset * deltaY;
119 double y1 = centerY + (l/2 - w) * deltaY - offset * deltaX;
121 g2d.draw(new Line2D.Double(x0, y0, x1, y1));
123 Path2D path = new Path2D.Double();
124 path.moveTo(x1 + w * deltaX, y1 + w * deltaY);
125 path.lineTo(x1 + w * deltaY, y1 - w * deltaX);
126 path.lineTo(x1 - w * deltaY, y1 + w * deltaX);
132 g2d.setStroke(oldStroke);
133 g2d.setColor(oldColor);
136 double viewScaleRecip = 10;
138 double scale = GeometryUtils.getScale(g2d.getTransform());
139 scale = Math.max(10000, Math.min(scale, 50000));
140 viewScaleRecip /= scale;
143 Point2D p = getCenterPoint();
144 symbolRect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, NORMAL, symbolRect, viewScaleRecip);
145 symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(symbolRect, symbolTransform);
147 for (INode nn : getNodes()) {
148 G2DNode g2dNode = (G2DNode)nn;
149 if (g2dNode instanceof SVGNode)
150 g2dNode.setTransform(symbolTransform);
154 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
157 g2d.setTransform(ot);
160 public float getStrokeWidth(AffineTransform tr, boolean selection) {
161 double scale = DistrictNetworkNodeUtils.getScale(tr);
162 float width = STROKE.getLineWidth() * getStrokeWidth(scale);
163 if (selection) width = width + (float) (2 * STROKE.getLineWidth() / scale);
167 private float getStrokeWidth(double scale) {
169 double str = stroke != null ? Math.abs(stroke) : 1.0;
170 float strokeWidth = (float) (str / scale);
178 public Path2D getPath() {
182 private Point2D getCenterPoint() {
183 if (centerPoint == null)
184 centerPoint = new Point2D.Double();
186 Rectangle2D bounds = path.getBounds2D();
187 centerPoint.setLocation(bounds.getCenterX(), bounds.getCenterY());
191 // public static Line2D calculateLine(DistrictNetworkEdge edge, Line2D result) {
192 // // Convert to screen coordinates
193 // double startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX());
194 // double startY = ModelledCRS.latitudeToY(-edge.getStartPoint().getY()); // Invert for Simantics
195 // double endX = ModelledCRS.longitudeToX(edge.getEndPoint().getX());
196 // double endY = ModelledCRS.latitudeToY(-edge.getEndPoint().getY());// Invert for Simantics
198 // if (result == null)
199 // result = new Line2D.Double();
200 // result.setLine(startX, startY, endX, endY);
204 public static Path2D calculatePath(DistrictNetworkEdge edge, Path2D result, boolean detailed) {
205 startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX());
206 startY = ModelledCRS.latitudeToY(-edge.getStartPoint().getY());
207 endX = ModelledCRS.longitudeToX(edge.getEndPoint().getX());
208 endY = ModelledCRS.latitudeToY(-edge.getEndPoint().getY());
210 if (result == null) {
211 result = new Path2D.Double();
215 result.moveTo(startX, startY);
217 double[] detailedGeometry = edge.getGeometry();
218 if (detailedGeometry != null && !DistrictNetworkEdgeElementFactory.EMPTY.equals(detailedGeometry)) {
221 for (int i = 0; i < detailedGeometry.length; i += 2) {
222 double x = ModelledCRS.longitudeToX(detailedGeometry[i]);
223 double y = ModelledCRS.latitudeToY(-detailedGeometry[i+1]);// Invert for Simantics
228 result.lineTo(endX, endY);
232 private boolean isSelected() {
233 return NodeUtil.isSelected(this, 1);
237 public Rectangle2D getBoundsInLocal() {
241 private void updateBounds() {
242 Rectangle2D oldBounds = bounds;
243 if (oldBounds == null)
244 oldBounds = new Rectangle2D.Double();
245 bounds = calculateBounds(oldBounds);
248 private Rectangle2D calculateBounds(Rectangle2D rect) {
249 return calculatePath(edge, null, false).getBounds2D();
252 public void setDNEdge(DistrictNetworkEdge edge) {
257 public void setColor(Color color) {
261 public Color getColor() {
265 @PropertySetter(value = "stroke")
266 public void setStroke(Double stroke) {
267 this.stroke = stroke;
270 @PropertySetter(value = "dynamicColor")
271 public void setDynamicColor(Color color) {
272 this.dynamicColor = color;
275 @PropertySetter(value = "arrowLength")
276 public void setArroLength(Double length) {
277 arrowLength = length;
280 @PropertySetter(value = "SVG")
281 public void setSVG(String value) {
282 for (INode nn : this.getNodes())
283 if (nn instanceof SVGNode)
284 ((SVGNode)nn).setData(value);