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;
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 static double startX;
63 private static double startY;
64 private static double endX;
65 private static double endY;
72 public void render(Graphics2D g2d) {
73 AffineTransform ot = null;
74 AffineTransform t = getTransform();
75 double scale = scaleStroke ? (Double) g2d.getRenderingHint(DistrictRenderingHints.KEY_VIEW_SCALE_UNDER_SPATIAL_ROOT) : 1.0;
76 if (t != null && !t.isIdentity()) {
77 //ot = g2d.getTransform();
78 ot = (AffineTransform) g2d.getRenderingHint(G2DRenderingHints.KEY_TRANSFORM_UNDER_SPATIAL_ROOT);
80 ot = g2d.getTransform();
83 AffineTransform work = DistrictNetworkNodeUtils.sharedTransform.get();
84 work.setTransform(ot);
86 scale = DistrictNetworkNodeUtils.getScale(work);
90 zoomLevel = (Integer) g2d.getRenderingHint(DistrictRenderingHints.KEY_VIEW_ZOOM_LEVEL);
93 Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
94 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
96 Color oldColor = g2d.getColor();
97 BasicStroke oldStroke = (BasicStroke) g2d.getStroke();
99 BasicStroke bs = null;
101 bs = GeometryUtils.scaleStroke(STROKE, getStrokeWidth(scale));
106 Path2D path = renderDetailed(zoomLevel) ? this.detailedPath : this.path;
109 g2d.setColor(SELECTION_COLOR);
110 g2d.setStroke(GeometryUtils.scaleAndOffsetStrokeWidth(bs, 1.f, (float)(2 * STROKE.getLineWidth() / scale)));
114 g2d.setColor(dynamicColor != null ? dynamicColor : color);
119 g2d.setStroke(oldStroke);
120 g2d.setColor(oldColor);
121 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
124 Point2D centerP = getCenterPoint(zoomLevel);
125 for (INode nn : getNodes()) {
126 G2DNode g2dNode = (G2DNode) nn;
127 if (g2dNode instanceof SVGNode) {
128 // If the node is hidden from the start, then the center point cannot be calculated yet.
129 if (!isDefined(centerP))
133 double viewScaleRecip = scaleStroke ? 10 / scale : 10;
134 symbolRect = DistrictNetworkNodeUtils.calculateDrawnGeometry(centerP, NORMAL, symbolRect, viewScaleRecip);
135 symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(symbolRect, symbolTransform);
136 g2dNode.setTransform(symbolTransform);
142 g2d.setTransform(ot);
145 static boolean isDefined(Point2D p) {
146 return p != null && !(Double.isNaN(p.getX()) || Double.isNaN(p.getY()));
149 boolean renderDetailed(int zoomLevel) {
150 return zoomLevel > 13;
153 public float getStrokeWidth(AffineTransform tr, boolean selection) {
154 double scale = DistrictNetworkNodeUtils.getScale(tr);
155 float width = STROKE.getLineWidth() * getStrokeWidth(scale);
156 if (selection) width = width + (float) (2 * STROKE.getLineWidth() / scale);
160 private float getStrokeWidth(double scale) {
162 double str = stroke != null ? Math.abs(stroke) : 1.0;
163 float strokeWidth = (float) (str / scale);
171 public Path2D getPath() {
172 return renderDetailed(zoomLevel) ? detailedPath : path;
175 public Point2D getCenterPoint(int zoomLevel) {
176 return renderDetailed(zoomLevel) ? detailedCenterPoint : centerPoint;
179 public Point2D getDirection(int zoomLevel) {
180 return renderDetailed(zoomLevel) ? detailedDirection : direction;
183 public static Path2D calculatePath(DistrictNetworkEdge edge, Path2D result, boolean detailed) {
184 startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX());
185 startY = ModelledCRS.latitudeToY(-edge.getStartPoint().getY());
186 endX = ModelledCRS.longitudeToX(edge.getEndPoint().getX());
187 endY = ModelledCRS.latitudeToY(-edge.getEndPoint().getY());
189 if (result == null) {
190 result = new Path2D.Double();
194 result.moveTo(startX, startY);
196 double[] detailedGeometry = edge.getGeometry();
197 if (detailedGeometry != null && !DistrictNetworkEdgeElementFactory.EMPTY.equals(detailedGeometry)) {
199 for (int i = 0; i < detailedGeometry.length; i += 2) {
200 double x = ModelledCRS.longitudeToX(detailedGeometry[i]);
201 double y = ModelledCRS.latitudeToY(-detailedGeometry[i+1]);// Invert for Simantics
206 result.lineTo(endX, endY);
210 private boolean isSelected() {
211 return NodeUtil.isSelected(this, 1);
215 public Rectangle2D getBoundsInLocal() {
219 private void updateBounds() {
220 Rectangle2D oldBounds = bounds;
221 if (oldBounds == null)
222 oldBounds = new Rectangle2D.Double();
223 bounds = calculateBounds(oldBounds);
227 * Calculates conservative bounds of the node provided by
228 * <code>detailedPath</code>. These might be larger than the simplified bounds
229 * of <code>path</code> but the conservative bounds ensure that spatial
230 * optimizations work properly.
232 private Rectangle2D calculateBounds(Rectangle2D rect) {
233 if (detailedPath == null)
234 detailedPath = calculatePath(edge, detailedPath, true);
235 return detailedPath.getBounds2D();
238 public void setDNEdge(DistrictNetworkEdge edge) {
240 path = calculatePath(edge, path, false);
241 detailedPath = calculatePath(edge, detailedPath, true);
243 centerPoint = new Point2D.Double();
244 detailedCenterPoint = new Point2D.Double();
245 direction = new Point2D.Double();
246 detailedDirection = new Point2D.Double();
248 DistrictNetworkNodeUtils.calculateCenterPointAndDirection(path, centerPoint, direction);
249 DistrictNetworkNodeUtils.calculateCenterPointAndDirection(detailedPath, detailedCenterPoint, detailedDirection);
254 public void setColor(Color color) {
258 public Color getColor() {
262 @PropertySetter(value = "stroke")
263 public void setStroke(Double stroke) {
264 this.stroke = stroke;
267 @PropertySetter(value = "dynamicColor")
268 public void setDynamicColor(Color color) {
269 this.dynamicColor = color;
272 @PropertySetter(value = "arrowLength")
273 public void setArrowLength(Double length) {
274 // find if there is a child deferred arrow node
275 DistrictNetworkEdgeArrayNode child = getOrCreateNode(DistrictNetworkEdgeArrayNode.NODE_KEY, DistrictNetworkEdgeArrayNode.class);
276 child.setEdgeNode(this);
277 child.setArrowLength(length);
278 //arrowLength = length;
281 @PropertySetter(value = "SVG")
282 public void setSVG(String value) {
283 for (INode nn : this.getNodes())
284 if (nn instanceof SVGNode)
285 ((SVGNode)nn).setData(value);
288 @PropertySetter(value = "hidden")
289 public void setHidden(Boolean value) {
293 public void setStaticInformation(Optional<String> staticInformation) {
294 DistrictNetworkStaticInfoNode child = getOrCreateNode(DistrictNetworkStaticInfoNode.NODE_KEY, DistrictNetworkStaticInfoNode.class);
295 child.setEdgeNode(this);
296 if (staticInformation.isPresent()) {
297 child.setInfo(staticInformation.get());
303 public void setInSimulation(Optional<Boolean> isInSimulation) {
304 if (!isInSimulation.isPresent()) {
305 removeNode(NotInSimulationNode.NODE_NAME);
307 NotInSimulationNode child = getOrCreateNode(NotInSimulationNode.NODE_NAME, NotInSimulationNode.class);
308 child.setZIndex(1000);
309 child.setIsInSimulation(isInSimulation.get());