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.scenegraph.INode;
16 import org.simantics.scenegraph.ISelectionPainterNode;
17 import org.simantics.scenegraph.g2d.G2DNode;
18 import org.simantics.scenegraph.g2d.G2DParentNode;
19 import org.simantics.scenegraph.g2d.nodes.SVGNode;
20 import org.simantics.scenegraph.utils.GeometryUtils;
21 import org.simantics.scenegraph.utils.NodeUtil;
23 public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelectionPainterNode {
25 private static final long serialVersionUID = 8049769475036519806L;
27 private static final BasicStroke STROKE = new BasicStroke(4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
28 private static final Color SELECTION_COLOR = new Color(255, 0, 255, 96);
30 private DistrictNetworkEdge edge;
31 private Rectangle2D bounds;
32 private transient Line2D path;
34 private boolean scaleStroke = true;
36 private Double stroke;
37 private transient Color dynamicColor = null;
39 // Dimensions for shut-off valve symbol
40 private static final double left = -0.25;
41 private static final double top = -0.25;
42 private static final double width = 0.5;
43 private static final double height = 0.5;
45 private static final Rectangle2D NORMAL = new Rectangle2D.Double(left, top, width, height);
47 private transient Point2D centerPoint;
48 private transient Rectangle2D symbolRect;
49 private transient AffineTransform symbolTransform;
56 public void render(Graphics2D g2d) {
57 AffineTransform ot = null;
58 AffineTransform t = getTransform();
59 if (t != null && !t.isIdentity()) {
60 ot = g2d.getTransform();
61 g2d.transform(getTransform());
64 Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
65 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
67 Color oldColor = g2d.getColor();
68 BasicStroke oldStroke = (BasicStroke) g2d.getStroke();
70 BasicStroke bs = null;
72 double scale = GeometryUtils.getScale(g2d.getTransform());
73 scale = Math.max(10000, Math.min(scale, 50000));
74 double str = stroke != null ? Math.abs(stroke) : 1.0;
75 bs = GeometryUtils.scaleStroke(STROKE, (float) (str / scale));
80 path = calculateLine(edge, path);
83 g2d.setColor(SELECTION_COLOR);
84 g2d.setStroke(GeometryUtils.scaleStroke(bs, 4f));
88 g2d.setColor(dynamicColor != null ? dynamicColor : color);
93 g2d.setStroke(oldStroke);
94 g2d.setColor(oldColor);
97 double viewScaleRecip = 10;
99 double scale = GeometryUtils.getScale(g2d.getTransform());
100 scale = Math.max(10000, Math.min(scale, 50000));
101 viewScaleRecip /= scale;
104 Point2D p = getCenterPoint();
105 symbolRect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, NORMAL, symbolRect, viewScaleRecip);
106 symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(symbolRect, symbolTransform);
108 for (INode nn : getNodes()) {
109 G2DNode g2dNode = (G2DNode)nn;
110 g2dNode.setTransform(symbolTransform);
114 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
117 g2d.setTransform(ot);
120 private Point2D getCenterPoint() {
121 if (centerPoint == null)
122 centerPoint = new Point2D.Double();
124 Rectangle2D bounds = path.getBounds2D();
125 centerPoint.setLocation(bounds.getCenterX(), bounds.getCenterY());
129 public static Line2D calculateLine(DistrictNetworkEdge edge, Line2D result) {
130 // Convert to screen coordinates
131 double startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX());
132 double startY = ModelledCRS.latitudeToY(-edge.getStartPoint().getY()); // Invert for Simantics
133 double endX = ModelledCRS.longitudeToX(edge.getEndPoint().getX());
134 double endY = ModelledCRS.latitudeToY(-edge.getEndPoint().getY());// Invert for Simantics
137 result = new Line2D.Double();
138 result.setLine(startX, startY, endX, endY);
142 public static Path2D calculatePath(DistrictNetworkEdge edge, Path2D result) {
143 // Convert to screen coordinates
144 double startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX());
145 double startY = ModelledCRS.latitudeToY(-edge.getStartPoint().getY()); // Invert for Simantics
146 double endX = ModelledCRS.longitudeToX(edge.getEndPoint().getX());
147 double endY = ModelledCRS.latitudeToY(-edge.getEndPoint().getY());// Invert for Simantics
149 if (result == null) {
150 result = new Path2D.Double();
154 result.moveTo(startX, startY);
155 result.lineTo(endX, endY);
159 private boolean isSelected() {
160 return NodeUtil.isSelected(this, 1);
164 public Rectangle2D getBoundsInLocal() {
168 private void updateBounds() {
169 Rectangle2D oldBounds = bounds;
170 if (oldBounds == null)
171 oldBounds = new Rectangle2D.Double();
172 bounds = calculateBounds(oldBounds);
175 private Rectangle2D calculateBounds(Rectangle2D rect) {
176 return calculatePath(edge, null).getBounds2D();
179 public void setDNEdge(DistrictNetworkEdge edge) {
184 public void setColor(Color color) {
188 public Color getColor() {
192 @PropertySetter(value = "stroke")
193 public void setStroke(Double stroke) {
194 this.stroke = stroke;
197 @PropertySetter(value = "dynamicColor")
198 public void setDynamicColor(Color color) {
199 this.dynamicColor = color;
202 @PropertySetter(value = "SVG")
203 public void setSVG(String value) {
204 for (INode nn : this.getNodes())
205 if (nn instanceof SVGNode)
206 ((SVGNode)nn).setData(value);