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.Path2D;
9 import java.awt.geom.Point2D;
10 import java.awt.geom.Rectangle2D;
11 import java.util.Optional;
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 public 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 static final boolean scaleStroke = true;
41 private Double stroke;
42 private transient Color dynamicColor = null;
43 private transient Color eventColor = null;
45 // Dimensions for shut-off valve symbol
46 private static final double left = -0.25;
47 private static final double top = -0.25;
48 private static final double width = 0.5;
49 private static final double height = 0.5;
51 private static final Rectangle2D NORMAL = new Rectangle2D.Double(left, top, width, height);
53 private transient Point2D centerPoint;
54 private transient Point2D detailedCenterPoint;
55 private transient Point2D direction;
56 private transient Point2D detailedDirection;
58 private transient Rectangle2D symbolRect;
59 private transient AffineTransform symbolTransform;
61 private boolean hidden = false;
63 private static double startX;
64 private static double startY;
65 private static double endX;
66 private static double endY;
73 public void render(Graphics2D g2d) {
74 AffineTransform ot = null;
75 AffineTransform t = getTransform();
76 double scale = scaleStroke ? (Double) g2d.getRenderingHint(DistrictRenderingHints.KEY_VIEW_SCALE_UNDER_SPATIAL_ROOT) : 1.0;
77 if (t != null && !t.isIdentity()) {
78 //ot = g2d.getTransform();
79 ot = (AffineTransform) g2d.getRenderingHint(G2DRenderingHints.KEY_TRANSFORM_UNDER_SPATIAL_ROOT);
81 ot = g2d.getTransform();
84 AffineTransform work = DistrictNetworkNodeUtils.sharedTransform.get();
85 work.setTransform(ot);
87 scale = DistrictNetworkNodeUtils.getScale(work);
91 zoomLevel = (Integer) g2d.getRenderingHint(DistrictRenderingHints.KEY_VIEW_ZOOM_LEVEL);
94 Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
95 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
97 Color oldColor = g2d.getColor();
98 BasicStroke oldStroke = (BasicStroke) g2d.getStroke();
100 BasicStroke bs = null;
102 bs = GeometryUtils.scaleStroke(STROKE, getStrokeWidth(scale));
107 Path2D path = renderDetailed(zoomLevel) ? this.detailedPath : this.path;
110 g2d.setColor(SELECTION_COLOR);
111 g2d.setStroke(GeometryUtils.scaleAndOffsetStrokeWidth(bs, 1.f, (float)(2 * STROKE.getLineWidth() / scale)));
115 g2d.setColor(dynamicColor != null ? dynamicColor : color);
116 g2d.setColor(eventColor != null ? eventColor : g2d.getColor());
121 g2d.setStroke(oldStroke);
122 g2d.setColor(oldColor);
123 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
126 Point2D centerP = getCenterPoint(zoomLevel);
127 for (INode nn : getNodes()) {
128 G2DNode g2dNode = (G2DNode) nn;
129 if (g2dNode instanceof SVGNode) {
130 // If the node is hidden from the start, then the center point cannot be calculated yet.
131 if (!isDefined(centerP))
135 double viewScaleRecip = scaleStroke ? 10 / scale : 10;
136 symbolRect = DistrictNetworkNodeUtils.calculateDrawnGeometry(centerP, NORMAL, symbolRect, viewScaleRecip);
137 symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(symbolRect, symbolTransform);
138 g2dNode.setTransform(symbolTransform);
144 g2d.setTransform(ot);
147 static boolean isDefined(Point2D p) {
148 return p != null && !(Double.isNaN(p.getX()) || Double.isNaN(p.getY()));
151 boolean renderDetailed(int zoomLevel) {
152 return zoomLevel > 13;
155 public float getStrokeWidth(AffineTransform tr, boolean selection) {
156 double scale = DistrictNetworkNodeUtils.getScale(tr);
157 float width = STROKE.getLineWidth() * getStrokeWidth(scale);
158 if (selection) width = width + (float) (2 * STROKE.getLineWidth() / scale);
162 private float getStrokeWidth(double scale) {
164 double str = stroke != null ? Math.abs(stroke) : 1.0;
165 float strokeWidth = (float) (str / scale);
173 public Path2D getPath() {
174 return renderDetailed(zoomLevel) ? detailedPath : path;
177 public Point2D getCenterPoint(int zoomLevel) {
178 return renderDetailed(zoomLevel) ? detailedCenterPoint : centerPoint;
181 public Point2D getDirection(int zoomLevel) {
182 return renderDetailed(zoomLevel) ? detailedDirection : direction;
185 public static Path2D calculatePath(DistrictNetworkEdge edge, Path2D result, boolean detailed) {
186 startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX());
187 startY = ModelledCRS.latitudeToY(-edge.getStartPoint().getY());
188 endX = ModelledCRS.longitudeToX(edge.getEndPoint().getX());
189 endY = ModelledCRS.latitudeToY(-edge.getEndPoint().getY());
191 if (result == null) {
192 result = new Path2D.Double();
196 result.moveTo(startX, startY);
198 double[] detailedGeometry = edge.getGeometry();
199 if (detailedGeometry != null && !DistrictNetworkEdgeElementFactory.EMPTY.equals(detailedGeometry)) {
201 for (int i = 0; i < detailedGeometry.length; i += 2) {
202 double x = ModelledCRS.longitudeToX(detailedGeometry[i]);
203 double y = ModelledCRS.latitudeToY(-detailedGeometry[i+1]);// Invert for Simantics
208 result.lineTo(endX, endY);
212 private boolean isSelected() {
213 return NodeUtil.isSelected(this, 1);
217 public Rectangle2D getBoundsInLocal() {
221 private void updateBounds() {
222 Rectangle2D oldBounds = bounds;
223 if (oldBounds == null)
224 oldBounds = new Rectangle2D.Double();
225 bounds = calculateBounds(oldBounds);
229 * Calculates conservative bounds of the node provided by
230 * <code>detailedPath</code>. These might be larger than the simplified bounds
231 * of <code>path</code> but the conservative bounds ensure that spatial
232 * optimizations work properly.
234 private Rectangle2D calculateBounds(Rectangle2D rect) {
235 if (detailedPath == null)
236 detailedPath = calculatePath(edge, detailedPath, true);
237 return detailedPath.getBounds2D();
240 public void setDNEdge(DistrictNetworkEdge edge) {
242 path = calculatePath(edge, path, false);
243 detailedPath = calculatePath(edge, detailedPath, true);
245 centerPoint = new Point2D.Double();
246 detailedCenterPoint = new Point2D.Double();
247 direction = new Point2D.Double();
248 detailedDirection = new Point2D.Double();
250 DistrictNetworkNodeUtils.calculateCenterPointAndDirection(path, centerPoint, direction);
251 DistrictNetworkNodeUtils.calculateCenterPointAndDirection(detailedPath, detailedCenterPoint, detailedDirection);
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 = "eventColor")
275 public void setEventColor(Color color) {
276 this.eventColor = color;
280 @PropertySetter(value = "arrowLength")
281 public void setArrowLength(Double length) {
282 // find if there is a child deferred arrow node
283 DistrictNetworkEdgeArrayNode child = getOrCreateNode(DistrictNetworkEdgeArrayNode.NODE_KEY, DistrictNetworkEdgeArrayNode.class);
284 child.setEdgeNode(this);
285 child.setArrowLength(length);
286 //arrowLength = length;
289 @PropertySetter(value = "SVG")
290 public void setSVG(String value) {
291 for (INode nn : this.getNodes())
292 if (nn instanceof SVGNode)
293 ((SVGNode)nn).setData(value);
296 @PropertySetter(value = "hidden")
297 public void setHidden(Boolean value) {
301 public void setStaticInformation(Optional<String> staticInformation) {
302 DistrictNetworkStaticInfoNode child = getOrCreateNode(DistrictNetworkStaticInfoNode.NODE_KEY, DistrictNetworkStaticInfoNode.class);
303 child.setEdgeNode(this);
304 if (staticInformation.isPresent()) {
305 child.setInfo(staticInformation.get());
311 public void setInSimulation(Optional<Boolean> isInSimulation) {
312 if (!isInSimulation.isPresent()) {
313 removeNode(NotInSimulationNode.NODE_NAME);
315 NotInSimulationNode child = getOrCreateNode(NotInSimulationNode.NODE_NAME, NotInSimulationNode.class);
316 child.setZIndex(1000);
317 child.setIsInSimulation(isInSimulation.get());