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;
35 private transient Path2D detailedPath;
37 private transient int zoomLevel = 0;
39 private static boolean scaleStroke = true;
41 private Double stroke;
42 private transient Color dynamicColor = null;
44 // Dimensions for shut-off valve symbol
45 private static final double left = -0.25;
46 private static final double top = -0.25;
47 private static final double width = 0.5;
48 private static final double height = 0.5;
50 private static final Rectangle2D NORMAL = new Rectangle2D.Double(left, top, width, height);
52 private transient Point2D centerPoint;
53 private transient Point2D detailedCenterPoint;
54 private transient Point2D direction;
55 private transient Point2D detailedDirection;
57 private transient Rectangle2D symbolRect;
58 private transient AffineTransform symbolTransform;
60 private boolean hidden = false;
62 private Double arrowLength;
64 private static double startX;
65 private static double startY;
66 private static double endX;
67 private static double endY;
69 private Path2D arrowPath;
76 public void render(Graphics2D g2d) {
77 AffineTransform ot = null;
78 AffineTransform t = getTransform();
79 double scale = scaleStroke ? (Double) g2d.getRenderingHint(DistrictRenderingHints.KEY_VIEW_SCALE_UNDER_SPATIAL_ROOT) : 1.0;
80 if (t != null && !t.isIdentity()) {
81 //ot = g2d.getTransform();
82 ot = (AffineTransform) g2d.getRenderingHint(G2DRenderingHints.KEY_TRANSFORM_UNDER_SPATIAL_ROOT);
84 ot = g2d.getTransform();
87 AffineTransform work = DistrictNetworkNodeUtils.sharedTransform.get();
88 work.setTransform(ot);
90 scale = DistrictNetworkNodeUtils.getScale(work);
94 zoomLevel = (Integer) g2d.getRenderingHint(DistrictRenderingHints.KEY_VIEW_ZOOM_LEVEL);
97 Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
98 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
100 Color oldColor = g2d.getColor();
101 BasicStroke oldStroke = (BasicStroke) g2d.getStroke();
103 BasicStroke bs = null;
105 bs = GeometryUtils.scaleStroke(STROKE, getStrokeWidth(scale));
110 Path2D path = renderDetailed(zoomLevel) ? this.detailedPath : this.path;
113 g2d.setColor(SELECTION_COLOR);
114 g2d.setStroke(GeometryUtils.scaleAndOffsetStrokeWidth(bs, 1.f, (float)(2 * STROKE.getLineWidth() / scale)));
118 g2d.setColor(dynamicColor != null ? dynamicColor : color);
123 if (arrowLength != null) {
124 g2d.setColor(Color.BLACK);
125 float lw = STROKE.getLineWidth() / (float)scale;
126 g2d.setStroke(new BasicStroke(lw, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
128 double l = arrowLength;
129 double w = 2 * (double) lw * Math.signum(l);
130 if (Math.abs(w) > Math.abs(l)) w = l;
131 double offset = 2 * (double) lw;
133 Point2D centerPoint = getCenterPoint(zoomLevel);
134 Point2D direction = getDirection(zoomLevel);
135 double centerX = centerPoint.getX(), centerY = centerPoint.getY();
136 double deltaX = direction.getX(), deltaY = direction.getY();
138 double x0 = centerX - l/2 * deltaX + offset * deltaY;
139 double y0 = centerY - l/2 * deltaY - offset * deltaX;
140 double x1 = centerX + (l/2 - w) * deltaX + offset * deltaY;
141 double y1 = centerY + (l/2 - w) * deltaY - offset * deltaX;
143 g2d.draw(new Line2D.Double(x0, y0, x1, y1));
145 arrowPath = new Path2D.Double();
146 arrowPath.moveTo(x1 + w * deltaX, y1 + w * deltaY);
147 arrowPath.lineTo(x1 + w * deltaY, y1 - w * deltaX);
148 arrowPath.lineTo(x1 - w * deltaY, y1 + w * deltaX);
149 arrowPath.closePath();
154 g2d.setStroke(oldStroke);
155 g2d.setColor(oldColor);
156 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
159 Point2D centerP = getCenterPoint(zoomLevel);
160 for (INode nn : getNodes()) {
161 G2DNode g2dNode = (G2DNode) nn;
162 if (g2dNode instanceof SVGNode) {
163 // If the node is hidden from the start, then the center point cannot be calculated yet.
164 if (!isDefined(centerP))
168 double viewScaleRecip = scaleStroke ? 10 / scale : 10;
169 symbolRect = DistrictNetworkNodeUtils.calculateDrawnGeometry(centerP, NORMAL, symbolRect, viewScaleRecip);
170 symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(symbolRect, symbolTransform);
171 g2dNode.setTransform(symbolTransform);
177 g2d.setTransform(ot);
180 static boolean isDefined(Point2D p) {
181 return p != null && !(Double.isNaN(p.getX()) || Double.isNaN(p.getY()));
184 boolean renderDetailed(int zoomLevel) {
185 return zoomLevel > 13;
188 public float getStrokeWidth(AffineTransform tr, boolean selection) {
189 double scale = DistrictNetworkNodeUtils.getScale(tr);
190 float width = STROKE.getLineWidth() * getStrokeWidth(scale);
191 if (selection) width = width + (float) (2 * STROKE.getLineWidth() / scale);
195 private float getStrokeWidth(double scale) {
197 double str = stroke != null ? Math.abs(stroke) : 1.0;
198 float strokeWidth = (float) (str / scale);
206 public Path2D getPath() {
207 return renderDetailed(zoomLevel) ? detailedPath : path;
210 public Point2D getCenterPoint(int zoomLevel) {
211 return renderDetailed(zoomLevel) ? detailedCenterPoint : centerPoint;
214 public Point2D getDirection(int zoomLevel) {
215 return renderDetailed(zoomLevel) ? detailedDirection : direction;
218 public static Path2D calculatePath(DistrictNetworkEdge edge, Path2D result, boolean detailed) {
219 startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX());
220 startY = ModelledCRS.latitudeToY(-edge.getStartPoint().getY());
221 endX = ModelledCRS.longitudeToX(edge.getEndPoint().getX());
222 endY = ModelledCRS.latitudeToY(-edge.getEndPoint().getY());
224 if (result == null) {
225 result = new Path2D.Double();
229 result.moveTo(startX, startY);
231 double[] detailedGeometry = edge.getGeometry();
232 if (detailedGeometry != null && !DistrictNetworkEdgeElementFactory.EMPTY.equals(detailedGeometry)) {
234 for (int i = 0; i < detailedGeometry.length; i += 2) {
235 double x = ModelledCRS.longitudeToX(detailedGeometry[i]);
236 double y = ModelledCRS.latitudeToY(-detailedGeometry[i+1]);// Invert for Simantics
241 result.lineTo(endX, endY);
245 private boolean isSelected() {
246 return NodeUtil.isSelected(this, 1);
250 public Rectangle2D getBoundsInLocal() {
254 private void updateBounds() {
255 Rectangle2D oldBounds = bounds;
256 if (oldBounds == null)
257 oldBounds = new Rectangle2D.Double();
258 bounds = calculateBounds(oldBounds);
262 * Calculates conservative bounds of the node provided by
263 * <code>detailedPath</code>. These might be larger than the simplified bounds
264 * of <code>path</code> but the conservative bounds ensure that spatial
265 * optimizations work properly.
267 private Rectangle2D calculateBounds(Rectangle2D rect) {
268 if (detailedPath == null)
269 detailedPath = calculatePath(edge, detailedPath, true);
270 return detailedPath.getBounds2D();
273 public void setDNEdge(DistrictNetworkEdge edge) {
275 path = calculatePath(edge, path, false);
276 detailedPath = calculatePath(edge, detailedPath, true);
278 centerPoint = new Point2D.Double();
279 detailedCenterPoint = new Point2D.Double();
280 direction = new Point2D.Double();
281 detailedDirection = new Point2D.Double();
283 DistrictNetworkNodeUtils.calculateCenterPointAndDirection(path, centerPoint, direction);
284 DistrictNetworkNodeUtils.calculateCenterPointAndDirection(detailedPath, detailedCenterPoint, detailedDirection);
289 public void setColor(Color color) {
293 public Color getColor() {
297 @PropertySetter(value = "stroke")
298 public void setStroke(Double stroke) {
299 this.stroke = stroke;
302 @PropertySetter(value = "dynamicColor")
303 public void setDynamicColor(Color color) {
304 this.dynamicColor = color;
307 @PropertySetter(value = "arrowLength")
308 public void setArrowLength(Double length) {
309 arrowLength = length;
312 @PropertySetter(value = "SVG")
313 public void setSVG(String value) {
314 for (INode nn : this.getNodes())
315 if (nn instanceof SVGNode)
316 ((SVGNode)nn).setData(value);
319 @PropertySetter(value = "hidden")
320 public void setHidden(Boolean value) {