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.Rectangle2D;
12 import org.simantics.district.network.ModelledCRS;
13 import org.simantics.district.network.ui.DistrictNetworkEdge;
14 import org.simantics.scenegraph.ISelectionPainterNode;
15 import org.simantics.scenegraph.g2d.G2DNode;
16 import org.simantics.scenegraph.utils.GeometryUtils;
17 import org.simantics.scenegraph.utils.NodeUtil;
19 public class DistrictNetworkEdgeNode extends G2DNode implements ISelectionPainterNode {
21 private static final long serialVersionUID = 8049769475036519806L;
23 private static final BasicStroke STROKE = new BasicStroke(4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
24 private static final Color SELECTION_COLOR = new Color(255, 0, 255, 96);
26 private DistrictNetworkEdge edge;
27 private Rectangle2D bounds;
28 private transient Line2D path;
30 private boolean scaleStroke = true;
32 private Double stroke;
33 private transient Color dynamicColor = null;
40 public void render(Graphics2D g2d) {
41 AffineTransform ot = null;
42 AffineTransform t = getTransform();
43 if (t != null && !t.isIdentity()) {
44 ot = g2d.getTransform();
45 g2d.transform(getTransform());
48 Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
49 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
51 Color oldColor = g2d.getColor();
52 BasicStroke oldStroke = (BasicStroke) g2d.getStroke();
54 BasicStroke bs = null;
56 double scale = GeometryUtils.getScale(g2d.getTransform());
57 scale = Math.max(10000, Math.min(scale, 50000));
58 double str = stroke != null ? Math.abs(stroke) : 1.0;
59 bs = GeometryUtils.scaleStroke(STROKE, (float) (str / scale));
64 path = calculateLine(edge, path);
67 g2d.setColor(SELECTION_COLOR);
68 g2d.setStroke(GeometryUtils.scaleStroke(bs, 4f));
72 g2d.setColor(dynamicColor != null ? dynamicColor : color);
77 g2d.setStroke(oldStroke);
78 g2d.setColor(oldColor);
79 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
85 public static Line2D calculateLine(DistrictNetworkEdge edge, Line2D result) {
86 // Convert to screen coordinates
87 double startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX());
88 double startY = ModelledCRS.latitudeToY(-edge.getStartPoint().getY()); // Invert for Simantics
89 double endX = ModelledCRS.longitudeToX(edge.getEndPoint().getX());
90 double endY = ModelledCRS.latitudeToY(-edge.getEndPoint().getY());// Invert for Simantics
93 result = new Line2D.Double();
94 result.setLine(startX, startY, endX, endY);
98 public static Path2D calculatePath(DistrictNetworkEdge edge, Path2D result) {
99 // Convert to screen coordinates
100 double startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX());
101 double startY = ModelledCRS.latitudeToY(-edge.getStartPoint().getY()); // Invert for Simantics
102 double endX = ModelledCRS.longitudeToX(edge.getEndPoint().getX());
103 double endY = ModelledCRS.latitudeToY(-edge.getEndPoint().getY());// Invert for Simantics
105 if (result == null) {
106 result = new Path2D.Double();
110 result.moveTo(startX, startY);
111 result.lineTo(endX, endY);
115 private boolean isSelected() {
116 return NodeUtil.isSelected(this, 1);
120 public Rectangle2D getBoundsInLocal() {
124 private void updateBounds() {
125 Rectangle2D oldBounds = bounds;
126 if (oldBounds == null)
127 oldBounds = new Rectangle2D.Double();
128 bounds = calculateBounds(oldBounds);
131 private Rectangle2D calculateBounds(Rectangle2D rect) {
132 return calculatePath(edge, null).getBounds2D();
135 public void setDNEdge(DistrictNetworkEdge edge) {
140 public void setColor(Color color) {
144 public Color getColor() {
148 @PropertySetter(value = "stroke")
149 public void setStroke(Double stroke) {
150 this.stroke = stroke;
153 @PropertySetter(value = "dynamicColor")
154 public void setDynamicColor(Color color) {
155 this.dynamicColor = color;