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 AffineTransform tr = g2d.getTransform();
83 scale = DistrictNetworkNodeUtils.getScale(tr);
84 bs = GeometryUtils.scaleStroke(STROKE, getStrokeWidth(scale));
88 int zoomLevel = MapScalingTransform.zoomLevel(ot);
89 path = calculatePath(edge, path, zoomLevel > 15);
92 g2d.setColor(SELECTION_COLOR);
93 g2d.setStroke(GeometryUtils.scaleAndOffsetStrokeWidth(bs, 1.f, (float)(2 * STROKE.getLineWidth() / scale)));
97 g2d.setColor(dynamicColor != null ? dynamicColor : color);
102 if (arrowLength != null) {
103 g2d.setColor(Color.BLACK);
104 float lw = STROKE.getLineWidth() / (float)scale;
105 g2d.setStroke(new BasicStroke(lw, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
107 double l = arrowLength;
108 double w = 2 * (double) lw * Math.signum(l);
109 if (Math.abs(w) > Math.abs(l)) w = l;
110 double offset = 2 * (double) lw;
112 double centerX = (startX + endX) / 2, centerY = (startY + endY) / 2;
113 double deltaX = endX - startX, deltaY = endY - startY;
114 double length = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
118 double x0 = centerX - l/2 * deltaX + offset * deltaY;
119 double y0 = centerY - l/2 * deltaY - offset * deltaX;
120 double x1 = centerX + (l/2 - w) * deltaX + offset * deltaY;
121 double y1 = centerY + (l/2 - w) * deltaY - offset * deltaX;
123 g2d.draw(new Line2D.Double(x0, y0, x1, y1));
125 Path2D path = new Path2D.Double();
126 path.moveTo(x1 + w * deltaX, y1 + w * deltaY);
127 path.lineTo(x1 + w * deltaY, y1 - w * deltaX);
128 path.lineTo(x1 - w * deltaY, y1 + w * deltaX);
134 g2d.setStroke(oldStroke);
135 g2d.setColor(oldColor);
138 double viewScaleRecip = 10;
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 g2dNode.setTransform(symbolTransform);
153 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
156 g2d.setTransform(ot);
159 public float getStrokeWidth(AffineTransform tr, boolean selection) {
160 double scale = DistrictNetworkNodeUtils.getScale(tr);
161 float width = STROKE.getLineWidth() * getStrokeWidth(scale);
162 if (selection) width = width + (float) (2 * STROKE.getLineWidth() / scale);
166 private float getStrokeWidth(double scale) {
168 double str = stroke != null ? Math.abs(stroke) : 1.0;
169 float strokeWidth = (float) (str / scale);
177 public Path2D getPath() {
181 private Point2D getCenterPoint() {
182 if (centerPoint == null)
183 centerPoint = new Point2D.Double();
185 Rectangle2D bounds = path.getBounds2D();
186 centerPoint.setLocation(bounds.getCenterX(), bounds.getCenterY());
190 // public static Line2D calculateLine(DistrictNetworkEdge edge, Line2D result) {
191 // // Convert to screen coordinates
192 // double startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX());
193 // double startY = ModelledCRS.latitudeToY(-edge.getStartPoint().getY()); // Invert for Simantics
194 // double endX = ModelledCRS.longitudeToX(edge.getEndPoint().getX());
195 // double endY = ModelledCRS.latitudeToY(-edge.getEndPoint().getY());// Invert for Simantics
197 // if (result == null)
198 // result = new Line2D.Double();
199 // result.setLine(startX, startY, endX, endY);
203 public static Path2D calculatePath(DistrictNetworkEdge edge, Path2D result, boolean detailed) {
204 startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX());
205 startY = ModelledCRS.latitudeToY(-edge.getStartPoint().getY());
206 endX = ModelledCRS.longitudeToX(edge.getEndPoint().getX());
207 endY = ModelledCRS.latitudeToY(-edge.getEndPoint().getY());
209 if (result == null) {
210 result = new Path2D.Double();
214 result.moveTo(startX, startY);
216 double[] detailedGeometry = edge.getGeometry();
217 if (detailedGeometry != null && !DistrictNetworkEdgeElementFactory.EMPTY.equals(detailedGeometry)) {
220 for (int i = 0; i < detailedGeometry.length; i += 2) {
221 double x = ModelledCRS.longitudeToX(detailedGeometry[i]);
222 double y = ModelledCRS.latitudeToY(-detailedGeometry[i+1]);// Invert for Simantics
227 result.lineTo(endX, endY);
231 private boolean isSelected() {
232 return NodeUtil.isSelected(this, 1);
236 public Rectangle2D getBoundsInLocal() {
240 private void updateBounds() {
241 Rectangle2D oldBounds = bounds;
242 if (oldBounds == null)
243 oldBounds = new Rectangle2D.Double();
244 bounds = calculateBounds(oldBounds);
247 private Rectangle2D calculateBounds(Rectangle2D rect) {
248 return calculatePath(edge, null, false).getBounds2D();
251 public void setDNEdge(DistrictNetworkEdge edge) {
256 public void setColor(Color color) {
260 public Color getColor() {
264 @PropertySetter(value = "stroke")
265 public void setStroke(Double stroke) {
266 this.stroke = stroke;
269 @PropertySetter(value = "dynamicColor")
270 public void setDynamicColor(Color color) {
271 this.dynamicColor = color;
274 @PropertySetter(value = "arrowLength")
275 public void setArroLength(Double length) {
276 arrowLength = length;
279 @PropertySetter(value = "SVG")
280 public void setSVG(String value) {
281 for (INode nn : this.getNodes())
282 if (nn instanceof SVGNode)
283 ((SVGNode)nn).setData(value);