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.maps.MapScalingTransform;
17 import org.simantics.scenegraph.INode;
18 import org.simantics.scenegraph.ISelectionPainterNode;
19 import org.simantics.scenegraph.INode.PropertySetter;
20 import org.simantics.scenegraph.g2d.G2DNode;
21 import org.simantics.scenegraph.g2d.G2DParentNode;
22 import org.simantics.scenegraph.g2d.nodes.SVGNode;
23 import org.simantics.scenegraph.utils.GeometryUtils;
24 import org.simantics.scenegraph.utils.NodeUtil;
26 public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelectionPainterNode {
28 private static final long serialVersionUID = 8049769475036519806L;
30 private static final BasicStroke STROKE = new BasicStroke(4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
31 private static final Color SELECTION_COLOR = new Color(255, 0, 255, 96);
33 private DistrictNetworkEdge edge;
34 private Rectangle2D bounds;
35 private transient Path2D path;
37 private boolean scaleStroke = true;
39 private Double stroke;
40 private transient Color dynamicColor = null;
42 // Dimensions for shut-off valve symbol
43 private static final double left = -0.25;
44 private static final double top = -0.25;
45 private static final double width = 0.5;
46 private static final double height = 0.5;
48 private static final Rectangle2D NORMAL = new Rectangle2D.Double(left, top, width, height);
50 private transient Point2D centerPoint;
51 private transient Rectangle2D symbolRect;
52 private transient AffineTransform symbolTransform;
54 private boolean hidden = false;
56 private Double arrowLength;
58 private static double startX;
59 private static double startY;
60 private static double endX;
61 private static double endY;
68 public void render(Graphics2D g2d) {
69 AffineTransform ot = null;
70 AffineTransform t = getTransform();
71 if (t != null && !t.isIdentity()) {
72 ot = g2d.getTransform();
73 g2d.transform(getTransform());
78 AffineTransform tr = g2d.getTransform();
79 scale = DistrictNetworkNodeUtils.getScale(tr);
83 Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
84 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
86 Color oldColor = g2d.getColor();
87 BasicStroke oldStroke = (BasicStroke) g2d.getStroke();
89 BasicStroke bs = null;
91 bs = GeometryUtils.scaleStroke(STROKE, getStrokeWidth(scale));
96 int zoomLevel = MapScalingTransform.zoomLevel(ot);
97 path = calculatePath(edge, path, zoomLevel > 15);
100 g2d.setColor(SELECTION_COLOR);
101 g2d.setStroke(GeometryUtils.scaleAndOffsetStrokeWidth(bs, 1.f, (float)(2 * STROKE.getLineWidth() / scale)));
105 g2d.setColor(dynamicColor != null ? dynamicColor : color);
110 if (arrowLength != null) {
111 g2d.setColor(Color.BLACK);
112 float lw = STROKE.getLineWidth() / (float)scale;
113 g2d.setStroke(new BasicStroke(lw, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
115 double l = arrowLength;
116 double w = 2 * (double) lw * Math.signum(l);
117 if (Math.abs(w) > Math.abs(l)) w = l;
118 double offset = 2 * (double) lw;
120 double centerX = (startX + endX) / 2, centerY = (startY + endY) / 2;
121 double deltaX = endX - startX, deltaY = endY - startY;
122 double length = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
126 double x0 = centerX - l/2 * deltaX + offset * deltaY;
127 double y0 = centerY - l/2 * deltaY - offset * deltaX;
128 double x1 = centerX + (l/2 - w) * deltaX + offset * deltaY;
129 double y1 = centerY + (l/2 - w) * deltaY - offset * deltaX;
131 g2d.draw(new Line2D.Double(x0, y0, x1, y1));
133 Path2D path = new Path2D.Double();
134 path.moveTo(x1 + w * deltaX, y1 + w * deltaY);
135 path.lineTo(x1 + w * deltaY, y1 - w * deltaX);
136 path.lineTo(x1 - w * deltaY, y1 + w * deltaX);
142 g2d.setStroke(oldStroke);
143 g2d.setColor(oldColor);
144 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
147 for (INode nn : getNodes()) {
148 G2DNode g2dNode = (G2DNode)nn;
149 if (g2dNode instanceof SVGNode) {
151 double viewScaleRecip = 10;
153 viewScaleRecip /= scale;
156 Point2D p = getCenterPoint();
157 symbolRect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, NORMAL, symbolRect, viewScaleRecip);
158 symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(symbolRect, symbolTransform);
160 g2dNode.setTransform(symbolTransform);
166 g2d.setTransform(ot);
169 public float getStrokeWidth(AffineTransform tr, boolean selection) {
170 double scale = DistrictNetworkNodeUtils.getScale(tr);
171 float width = STROKE.getLineWidth() * getStrokeWidth(scale);
172 if (selection) width = width + (float) (2 * STROKE.getLineWidth() / scale);
176 private float getStrokeWidth(double scale) {
178 double str = stroke != null ? Math.abs(stroke) : 1.0;
179 float strokeWidth = (float) (str / scale);
187 public Path2D getPath() {
191 private Point2D getCenterPoint() {
192 if (centerPoint == null)
193 centerPoint = new Point2D.Double();
195 Rectangle2D bounds = path.getBounds2D();
196 centerPoint.setLocation(bounds.getCenterX(), bounds.getCenterY());
200 // public static Line2D calculateLine(DistrictNetworkEdge edge, Line2D result) {
201 // // Convert to screen coordinates
202 // double startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX());
203 // double startY = ModelledCRS.latitudeToY(-edge.getStartPoint().getY()); // Invert for Simantics
204 // double endX = ModelledCRS.longitudeToX(edge.getEndPoint().getX());
205 // double endY = ModelledCRS.latitudeToY(-edge.getEndPoint().getY());// Invert for Simantics
207 // if (result == null)
208 // result = new Line2D.Double();
209 // result.setLine(startX, startY, endX, endY);
213 public static Path2D calculatePath(DistrictNetworkEdge edge, Path2D result, boolean detailed) {
214 startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX());
215 startY = ModelledCRS.latitudeToY(-edge.getStartPoint().getY());
216 endX = ModelledCRS.longitudeToX(edge.getEndPoint().getX());
217 endY = ModelledCRS.latitudeToY(-edge.getEndPoint().getY());
219 if (result == null) {
220 result = new Path2D.Double();
224 result.moveTo(startX, startY);
226 double[] detailedGeometry = edge.getGeometry();
227 if (detailedGeometry != null && !DistrictNetworkEdgeElementFactory.EMPTY.equals(detailedGeometry)) {
230 for (int i = 0; i < detailedGeometry.length; i += 2) {
231 double x = ModelledCRS.longitudeToX(detailedGeometry[i]);
232 double y = ModelledCRS.latitudeToY(-detailedGeometry[i+1]);// Invert for Simantics
237 result.lineTo(endX, endY);
241 private boolean isSelected() {
242 return NodeUtil.isSelected(this, 1);
246 public Rectangle2D getBoundsInLocal() {
250 private void updateBounds() {
251 Rectangle2D oldBounds = bounds;
252 if (oldBounds == null)
253 oldBounds = new Rectangle2D.Double();
254 bounds = calculateBounds(oldBounds);
257 private Rectangle2D calculateBounds(Rectangle2D rect) {
258 return calculatePath(edge, null, false).getBounds2D();
261 public void setDNEdge(DistrictNetworkEdge edge) {
266 public void setColor(Color color) {
270 public Color getColor() {
274 @PropertySetter(value = "stroke")
275 public void setStroke(Double stroke) {
276 this.stroke = stroke;
279 @PropertySetter(value = "dynamicColor")
280 public void setDynamicColor(Color color) {
281 this.dynamicColor = color;
284 @PropertySetter(value = "arrowLength")
285 public void setArroLength(Double 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);
297 @PropertySetter(value = "hidden")
298 public void setHidden(Boolean value) {