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 // Ensure the line is always rendered on top of the edge
139 // to prevent overlap with static info rendered below it.
140 double odx = offset * deltaY;
141 double ody = offset * deltaX;
147 double x0 = centerX - l/2 * deltaX + ody;
148 double y0 = centerY - l/2 * deltaY - odx;
149 double x1 = centerX + (l/2 - w) * deltaX + ody;
150 double y1 = centerY + (l/2 - w) * deltaY - odx;
152 g2d.draw(new Line2D.Double(x0, y0, x1, y1));
154 arrowPath = new Path2D.Double();
155 arrowPath.moveTo(x1 + w * deltaX, y1 + w * deltaY);
156 arrowPath.lineTo(x1 + w * deltaY, y1 - w * deltaX);
157 arrowPath.lineTo(x1 - w * deltaY, y1 + w * deltaX);
158 arrowPath.closePath();
163 g2d.setStroke(oldStroke);
164 g2d.setColor(oldColor);
165 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
168 Point2D centerP = getCenterPoint(zoomLevel);
169 for (INode nn : getNodes()) {
170 G2DNode g2dNode = (G2DNode) nn;
171 if (g2dNode instanceof SVGNode) {
172 // If the node is hidden from the start, then the center point cannot be calculated yet.
173 if (!isDefined(centerP))
177 double viewScaleRecip = scaleStroke ? 10 / scale : 10;
178 symbolRect = DistrictNetworkNodeUtils.calculateDrawnGeometry(centerP, NORMAL, symbolRect, viewScaleRecip);
179 symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(symbolRect, symbolTransform);
180 g2dNode.setTransform(symbolTransform);
186 g2d.setTransform(ot);
189 static boolean isDefined(Point2D p) {
190 return p != null && !(Double.isNaN(p.getX()) || Double.isNaN(p.getY()));
193 boolean renderDetailed(int zoomLevel) {
194 return zoomLevel > 13;
197 public float getStrokeWidth(AffineTransform tr, boolean selection) {
198 double scale = DistrictNetworkNodeUtils.getScale(tr);
199 float width = STROKE.getLineWidth() * getStrokeWidth(scale);
200 if (selection) width = width + (float) (2 * STROKE.getLineWidth() / scale);
204 private float getStrokeWidth(double scale) {
206 double str = stroke != null ? Math.abs(stroke) : 1.0;
207 float strokeWidth = (float) (str / scale);
215 public Path2D getPath() {
216 return renderDetailed(zoomLevel) ? detailedPath : path;
219 public Point2D getCenterPoint(int zoomLevel) {
220 return renderDetailed(zoomLevel) ? detailedCenterPoint : centerPoint;
223 public Point2D getDirection(int zoomLevel) {
224 return renderDetailed(zoomLevel) ? detailedDirection : direction;
227 public static Path2D calculatePath(DistrictNetworkEdge edge, Path2D result, boolean detailed) {
228 startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX());
229 startY = ModelledCRS.latitudeToY(-edge.getStartPoint().getY());
230 endX = ModelledCRS.longitudeToX(edge.getEndPoint().getX());
231 endY = ModelledCRS.latitudeToY(-edge.getEndPoint().getY());
233 if (result == null) {
234 result = new Path2D.Double();
238 result.moveTo(startX, startY);
240 double[] detailedGeometry = edge.getGeometry();
241 if (detailedGeometry != null && !DistrictNetworkEdgeElementFactory.EMPTY.equals(detailedGeometry)) {
243 for (int i = 0; i < detailedGeometry.length; i += 2) {
244 double x = ModelledCRS.longitudeToX(detailedGeometry[i]);
245 double y = ModelledCRS.latitudeToY(-detailedGeometry[i+1]);// Invert for Simantics
250 result.lineTo(endX, endY);
254 private boolean isSelected() {
255 return NodeUtil.isSelected(this, 1);
259 public Rectangle2D getBoundsInLocal() {
263 private void updateBounds() {
264 Rectangle2D oldBounds = bounds;
265 if (oldBounds == null)
266 oldBounds = new Rectangle2D.Double();
267 bounds = calculateBounds(oldBounds);
271 * Calculates conservative bounds of the node provided by
272 * <code>detailedPath</code>. These might be larger than the simplified bounds
273 * of <code>path</code> but the conservative bounds ensure that spatial
274 * optimizations work properly.
276 private Rectangle2D calculateBounds(Rectangle2D rect) {
277 if (detailedPath == null)
278 detailedPath = calculatePath(edge, detailedPath, true);
279 return detailedPath.getBounds2D();
282 public void setDNEdge(DistrictNetworkEdge edge) {
284 path = calculatePath(edge, path, false);
285 detailedPath = calculatePath(edge, detailedPath, true);
287 centerPoint = new Point2D.Double();
288 detailedCenterPoint = new Point2D.Double();
289 direction = new Point2D.Double();
290 detailedDirection = new Point2D.Double();
292 DistrictNetworkNodeUtils.calculateCenterPointAndDirection(path, centerPoint, direction);
293 DistrictNetworkNodeUtils.calculateCenterPointAndDirection(detailedPath, detailedCenterPoint, detailedDirection);
298 public void setColor(Color color) {
302 public Color getColor() {
306 @PropertySetter(value = "stroke")
307 public void setStroke(Double stroke) {
308 this.stroke = stroke;
311 @PropertySetter(value = "dynamicColor")
312 public void setDynamicColor(Color color) {
313 this.dynamicColor = color;
316 @PropertySetter(value = "arrowLength")
317 public void setArrowLength(Double length) {
318 arrowLength = length;
321 @PropertySetter(value = "SVG")
322 public void setSVG(String value) {
323 for (INode nn : this.getNodes())
324 if (nn instanceof SVGNode)
325 ((SVGNode)nn).setData(value);
328 @PropertySetter(value = "hidden")
329 public void setHidden(Boolean value) {