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;
82 scale = GeometryUtils.getScale(g2d.getTransform());
83 scale = Math.max(10000, Math.min(scale, 50000));
84 double str = stroke != null ? Math.abs(stroke) : 1.0;
85 bs = GeometryUtils.scaleStroke(STROKE, (float) (str / scale));
89 int zoomLevel = MapScalingTransform.zoomLevel(ot);
90 path = calculatePath(edge, path, zoomLevel > 15);
93 g2d.setColor(SELECTION_COLOR);
94 g2d.setStroke(GeometryUtils.scaleStroke(bs, 4f));
98 g2d.setColor(dynamicColor != null ? dynamicColor : color);
103 if (arrowLength != null) {
104 g2d.setColor(Color.BLACK);
105 float lw = STROKE.getLineWidth() / (float)scale;
106 g2d.setStroke(new BasicStroke(lw, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
108 double l = arrowLength;
109 double w = 2 * (double) lw * Math.signum(l);
110 if (Math.abs(w) > Math.abs(l)) w = l;
111 double offset = 2 * (double) lw;
113 double centerX = (startX + endX) / 2, centerY = (startY + endY) / 2;
114 double deltaX = endX - startX, deltaY = endY - startY;
115 double length = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
119 double x0 = centerX - l/2 * deltaX + offset * deltaY;
120 double y0 = centerY - l/2 * deltaY - offset * deltaX;
121 double x1 = centerX + (l/2 - w) * deltaX + offset * deltaY;
122 double y1 = centerY + (l/2 - w) * deltaY - offset * deltaX;
124 g2d.draw(new Line2D.Double(x0, y0, x1, y1));
126 Path2D path = new Path2D.Double();
127 path.moveTo(x1 + w * deltaX, y1 + w * deltaY);
128 path.lineTo(x1 + w * deltaY, y1 - w * deltaX);
129 path.lineTo(x1 - w * deltaY, y1 + w * deltaX);
135 g2d.setStroke(oldStroke);
136 g2d.setColor(oldColor);
139 double viewScaleRecip = 10;
141 viewScaleRecip /= scale;
144 Point2D p = getCenterPoint();
145 symbolRect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, NORMAL, symbolRect, viewScaleRecip);
146 symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(symbolRect, symbolTransform);
148 for (INode nn : getNodes()) {
149 G2DNode g2dNode = (G2DNode)nn;
150 g2dNode.setTransform(symbolTransform);
154 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
157 g2d.setTransform(ot);
160 private Point2D getCenterPoint() {
161 if (centerPoint == null)
162 centerPoint = new Point2D.Double();
164 Rectangle2D bounds = path.getBounds2D();
165 centerPoint.setLocation(bounds.getCenterX(), bounds.getCenterY());
169 // public static Line2D calculateLine(DistrictNetworkEdge edge, Line2D result) {
170 // // Convert to screen coordinates
171 // double startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX());
172 // double startY = ModelledCRS.latitudeToY(-edge.getStartPoint().getY()); // Invert for Simantics
173 // double endX = ModelledCRS.longitudeToX(edge.getEndPoint().getX());
174 // double endY = ModelledCRS.latitudeToY(-edge.getEndPoint().getY());// Invert for Simantics
176 // if (result == null)
177 // result = new Line2D.Double();
178 // result.setLine(startX, startY, endX, endY);
182 public static Path2D calculatePath(DistrictNetworkEdge edge, Path2D result, boolean detailed) {
183 startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX());
184 startY = ModelledCRS.latitudeToY(-edge.getStartPoint().getY());
185 endX = ModelledCRS.longitudeToX(edge.getEndPoint().getX());
186 endY = ModelledCRS.latitudeToY(-edge.getEndPoint().getY());
188 if (result == null) {
189 result = new Path2D.Double();
193 result.moveTo(startX, startY);
195 double[] detailedGeometry = edge.getGeometry();
196 if (detailedGeometry != null && !DistrictNetworkEdgeElementFactory.EMPTY.equals(detailedGeometry)) {
199 for (int i = 0; i < detailedGeometry.length; i += 2) {
200 double x = ModelledCRS.longitudeToX(detailedGeometry[i]);
201 double y = ModelledCRS.latitudeToY(-detailedGeometry[i+1]);// Invert for Simantics
206 result.lineTo(endX, endY);
210 private boolean isSelected() {
211 return NodeUtil.isSelected(this, 1);
215 public Rectangle2D getBoundsInLocal() {
219 private void updateBounds() {
220 Rectangle2D oldBounds = bounds;
221 if (oldBounds == null)
222 oldBounds = new Rectangle2D.Double();
223 bounds = calculateBounds(oldBounds);
226 private Rectangle2D calculateBounds(Rectangle2D rect) {
227 return calculatePath(edge, null, false).getBounds2D();
230 public void setDNEdge(DistrictNetworkEdge edge) {
235 public void setColor(Color color) {
239 public Color getColor() {
243 @PropertySetter(value = "stroke")
244 public void setStroke(Double stroke) {
245 this.stroke = stroke;
248 @PropertySetter(value = "dynamicColor")
249 public void setDynamicColor(Color color) {
250 this.dynamicColor = color;
253 @PropertySetter(value = "arrowLength")
254 public void setArroLength(Double length) {
255 arrowLength = length;
258 @PropertySetter(value = "SVG")
259 public void setSVG(String value) {
260 for (INode nn : this.getNodes())
261 if (nn instanceof SVGNode)
262 ((SVGNode)nn).setData(value);