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.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.G2DRenderingHints;
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 static 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 boolean hidden = false;
55 private Double arrowLength;
57 private static double startX;
58 private static double startY;
59 private static double endX;
60 private static double endY;
67 public void render(Graphics2D g2d) {
68 AffineTransform ot = null;
69 AffineTransform t = getTransform();
70 double scale = scaleStroke ? (Double) g2d.getRenderingHint(DistrictRenderingHints.KEY_VIEW_SCALE_UNDER_SPATIAL_ROOT) : 1.0;
71 if (t != null && !t.isIdentity()) {
72 //ot = g2d.getTransform();
73 ot = (AffineTransform) g2d.getRenderingHint(G2DRenderingHints.KEY_TRANSFORM_UNDER_SPATIAL_ROOT);
76 AffineTransform work = DistrictNetworkNodeUtils.sharedTransform.get();
77 work.setTransform(ot);
79 scale = DistrictNetworkNodeUtils.getScale(work);
84 Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
85 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
87 Color oldColor = g2d.getColor();
88 BasicStroke oldStroke = (BasicStroke) g2d.getStroke();
90 BasicStroke bs = null;
92 bs = GeometryUtils.scaleStroke(STROKE, getStrokeWidth(scale));
97 int zoomLevel = (Integer) g2d.getRenderingHint(DistrictRenderingHints.KEY_VIEW_ZOOM_LEVEL);
98 path = calculatePath(edge, path, zoomLevel > 15);
101 g2d.setColor(SELECTION_COLOR);
102 g2d.setStroke(GeometryUtils.scaleAndOffsetStrokeWidth(bs, 1.f, (float)(2 * STROKE.getLineWidth() / scale)));
106 g2d.setColor(dynamicColor != null ? dynamicColor : color);
111 if (arrowLength != null) {
112 g2d.setColor(Color.BLACK);
113 float lw = STROKE.getLineWidth() / (float)scale;
114 g2d.setStroke(new BasicStroke(lw, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
116 double l = arrowLength;
117 double w = 2 * (double) lw * Math.signum(l);
118 if (Math.abs(w) > Math.abs(l)) w = l;
119 double offset = 2 * (double) lw;
121 double centerX = (startX + endX) / 2, centerY = (startY + endY) / 2;
122 double deltaX = endX - startX, deltaY = endY - startY;
123 double length = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
127 double x0 = centerX - l/2 * deltaX + offset * deltaY;
128 double y0 = centerY - l/2 * deltaY - offset * deltaX;
129 double x1 = centerX + (l/2 - w) * deltaX + offset * deltaY;
130 double y1 = centerY + (l/2 - w) * deltaY - offset * deltaX;
132 g2d.draw(new Line2D.Double(x0, y0, x1, y1));
134 Path2D path = new Path2D.Double();
135 path.moveTo(x1 + w * deltaX, y1 + w * deltaY);
136 path.lineTo(x1 + w * deltaY, y1 - w * deltaX);
137 path.lineTo(x1 - w * deltaY, y1 + w * deltaX);
143 g2d.setStroke(oldStroke);
144 g2d.setColor(oldColor);
145 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
148 for (INode nn : getNodes()) {
149 G2DNode g2dNode = (G2DNode)nn;
150 if (g2dNode instanceof SVGNode) {
152 double viewScaleRecip = 10;
154 viewScaleRecip /= scale;
157 // If the node is hidden from the start, then the center point cannot be calculated yet.
158 Point2D p = getCenterPoint();
162 symbolRect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, NORMAL, symbolRect, viewScaleRecip);
163 symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(symbolRect, symbolTransform);
165 g2dNode.setTransform(symbolTransform);
171 g2d.setTransform(ot);
174 public float getStrokeWidth(AffineTransform tr, boolean selection) {
175 double scale = DistrictNetworkNodeUtils.getScale(tr);
176 float width = STROKE.getLineWidth() * getStrokeWidth(scale);
177 if (selection) width = width + (float) (2 * STROKE.getLineWidth() / scale);
181 private float getStrokeWidth(double scale) {
183 double str = stroke != null ? Math.abs(stroke) : 1.0;
184 float strokeWidth = (float) (str / scale);
192 public Path2D getPath() {
196 private Point2D getCenterPoint() {
199 if (centerPoint == null)
200 centerPoint = new Point2D.Double();
201 Rectangle2D bounds = path.getBounds2D();
202 centerPoint.setLocation(bounds.getCenterX(), bounds.getCenterY());
206 // public static Line2D calculateLine(DistrictNetworkEdge edge, Line2D result) {
207 // // Convert to screen coordinates
208 // double startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX());
209 // double startY = ModelledCRS.latitudeToY(-edge.getStartPoint().getY()); // Invert for Simantics
210 // double endX = ModelledCRS.longitudeToX(edge.getEndPoint().getX());
211 // double endY = ModelledCRS.latitudeToY(-edge.getEndPoint().getY());// Invert for Simantics
213 // if (result == null)
214 // result = new Line2D.Double();
215 // result.setLine(startX, startY, endX, endY);
219 public static Path2D calculatePath(DistrictNetworkEdge edge, Path2D result, boolean detailed) {
220 startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX());
221 startY = ModelledCRS.latitudeToY(-edge.getStartPoint().getY());
222 endX = ModelledCRS.longitudeToX(edge.getEndPoint().getX());
223 endY = ModelledCRS.latitudeToY(-edge.getEndPoint().getY());
225 if (result == null) {
226 result = new Path2D.Double();
230 result.moveTo(startX, startY);
232 double[] detailedGeometry = edge.getGeometry();
233 if (detailedGeometry != null && !DistrictNetworkEdgeElementFactory.EMPTY.equals(detailedGeometry)) {
236 for (int i = 0; i < detailedGeometry.length; i += 2) {
237 double x = ModelledCRS.longitudeToX(detailedGeometry[i]);
238 double y = ModelledCRS.latitudeToY(-detailedGeometry[i+1]);// Invert for Simantics
243 result.lineTo(endX, endY);
247 private boolean isSelected() {
248 return NodeUtil.isSelected(this, 1);
252 public Rectangle2D getBoundsInLocal() {
256 private void updateBounds() {
257 Rectangle2D oldBounds = bounds;
258 if (oldBounds == null)
259 oldBounds = new Rectangle2D.Double();
260 bounds = calculateBounds(oldBounds);
263 private Rectangle2D calculateBounds(Rectangle2D rect) {
264 return calculatePath(edge, null, false).getBounds2D();
267 public void setDNEdge(DistrictNetworkEdge edge) {
272 public void setColor(Color color) {
276 public Color getColor() {
280 @PropertySetter(value = "stroke")
281 public void setStroke(Double stroke) {
282 this.stroke = stroke;
285 @PropertySetter(value = "dynamicColor")
286 public void setDynamicColor(Color color) {
287 this.dynamicColor = color;
290 @PropertySetter(value = "arrowLength")
291 public void setArrowLength(Double length) {
292 arrowLength = length;
295 @PropertySetter(value = "SVG")
296 public void setSVG(String value) {
297 for (INode nn : this.getNodes())
298 if (nn instanceof SVGNode)
299 ((SVGNode)nn).setData(value);
303 @PropertySetter(value = "hidden")
304 public void setHidden(Boolean value) {