]> gerrit.simantics Code Review - simantics/district.git/blob
9f8c9a23ed192245813cdccd1ddbf2a0a61ddc16
[simantics/district.git] /
1 package org.simantics.district.network.ui.nodes;
2
3 import java.awt.BasicStroke;
4 import java.awt.Color;
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;
11
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;
18
19 public class DistrictNetworkEdgeNode extends G2DNode implements ISelectionPainterNode {
20
21     private static final long serialVersionUID = 8049769475036519806L;
22
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);
25
26     private DistrictNetworkEdge edge;
27     private Rectangle2D bounds;
28     private transient Line2D path;
29
30     private boolean scaleStroke = true;
31     private Color color;
32     private Double stroke;
33     private transient Color dynamicColor = null;
34
35     @Override
36     public void init() {
37     }
38
39     @Override
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());
46         }
47
48         Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
49         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
50
51         Color oldColor = g2d.getColor();
52         BasicStroke oldStroke = (BasicStroke) g2d.getStroke();
53
54         BasicStroke bs = null;
55         if (scaleStroke) {
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));
60         } else {
61             bs = STROKE;
62         }
63
64         path = calculateLine(edge, path);
65
66         if (isSelected()) {
67             g2d.setColor(SELECTION_COLOR);
68             g2d.setStroke(GeometryUtils.scaleStroke(bs, 4f));
69             g2d.draw(path);
70         }
71
72         g2d.setColor(dynamicColor != null ? dynamicColor : color);
73         g2d.setStroke(bs);
74         g2d.draw(path);
75
76         // Reset
77         g2d.setStroke(oldStroke);
78         g2d.setColor(oldColor);
79         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
80
81         if (ot != null)
82             g2d.setTransform(ot);
83     }
84
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
91
92         if (result == null)
93             result = new Line2D.Double();
94         result.setLine(startX, startY, endX, endY);
95         return result;
96     }
97
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
104
105         if (result == null) {
106              result = new Path2D.Double();
107         } else {
108              result.reset();
109         }
110         result.moveTo(startX, startY);
111         result.lineTo(endX, endY);
112         return result;
113     }
114
115     private boolean isSelected() {
116         return NodeUtil.isSelected(this, 1);
117     }
118
119     @Override
120     public Rectangle2D getBoundsInLocal() {
121         return bounds;
122     }
123
124     private void updateBounds() {
125         Rectangle2D oldBounds = bounds;
126         if (oldBounds == null)
127             oldBounds = new Rectangle2D.Double();
128         bounds = calculateBounds(oldBounds);
129     }
130
131     private Rectangle2D calculateBounds(Rectangle2D rect) {
132         return calculatePath(edge, null).getBounds2D();
133     }
134
135     public void setDNEdge(DistrictNetworkEdge edge) {
136         this.edge = edge;
137         updateBounds();
138     }
139
140     public void setColor(Color color) {
141         this.color = color;
142     }
143
144     public Color getColor() {
145         return color;
146     }
147
148     @PropertySetter(value = "stroke")
149     public void setStroke(Double stroke) {
150         this.stroke = stroke;
151     }
152
153     @PropertySetter(value = "dynamicColor")
154     public void setDynamicColor(Color color) {
155         this.dynamicColor = color;
156     }
157
158 }