]> gerrit.simantics Code Review - simantics/district.git/blob
745f2a80fccb04dbf0e83bbc2bfeff5b252489b0
[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.Stroke;
8 import java.awt.geom.AffineTransform;
9 import java.awt.geom.Path2D;
10 import java.awt.geom.Point2D;
11 import java.awt.geom.Rectangle2D;
12
13 import org.simantics.district.network.ModelledCRS;
14 import org.simantics.district.network.ui.DistrictNetworkEdge;
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 {
20
21     private static final long serialVersionUID = 8049769475036519806L;
22     
23     private static final Stroke     SELECTION_STROKE = new BasicStroke(1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);
24     private static final Color      SELECTION_COLOR  = new Color(255, 0, 255, 96);
25     
26     private DistrictNetworkEdge edge;
27     private Rectangle2D bounds;
28     
29     private static final Stroke STROKE = new BasicStroke(4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
30     private boolean scaleStroke = true;
31
32     private Color color;
33
34     @Override
35     public void init() {
36     }
37     
38     @Override
39     public void render(Graphics2D g2d) {
40         
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 //        boolean selected = isSelected();
54 //        if (selected) {
55 //            Path2D selectionPath = edge.getPath();
56 //            Shape selectionShape = SELECTION_STROKE.createStrokedShape(selectionPath);
57 //            g2d.setColor(SELECTION_COLOR);
58 //            g2d.fill(selectionShape);
59 //        }
60 //        
61         g2d.setColor(color);
62         if (STROKE != null) {
63             if (scaleStroke && STROKE instanceof BasicStroke) {
64                 BasicStroke bs = GeometryUtils.scaleStroke(STROKE, (float) (1.0 / GeometryUtils.getScale(g2d.getTransform())));
65                 g2d.setStroke(bs);
66             } else {
67                 g2d.setStroke(STROKE);
68             }
69         }
70
71         g2d.draw(calculatePath(edge));
72         
73         // Reset
74         g2d.setStroke(oldStroke);
75         g2d.setColor(oldColor);
76         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
77         
78         if (ot != null)
79             g2d.setTransform(ot);
80     }
81     
82     public static Path2D calculatePath(DistrictNetworkEdge edge) {
83         // Convert to screen coordinates
84         double startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX());
85         double startY = ModelledCRS.latitudeToY(edge.getStartPoint().getY());
86         double endX = ModelledCRS.longitudeToX(edge.getEndPoint().getX());
87         double endY = ModelledCRS.latitudeToY(edge.getEndPoint().getY());
88         
89         // render
90         Path2D path = new Path2D.Double();
91         path.moveTo(startX, startY);
92         path.lineTo(endX, endY);
93         return path;
94     }
95
96     private boolean isSelected() {
97         return NodeUtil.isSelected(this, 1);
98     }
99
100     @Override
101     public Rectangle2D getBoundsInLocal() {
102         return bounds;
103     }
104     
105     private void updateBounds() {
106         Rectangle2D oldBounds = bounds;
107         if (oldBounds == null)
108             oldBounds = new Rectangle2D.Double();
109         bounds = calculateBounds(oldBounds);
110     }
111
112     private Rectangle2D calculateBounds(Rectangle2D rect) {
113         return calculatePath(edge).getBounds2D();
114     }
115
116     public void setDNEdge(DistrictNetworkEdge edge) {
117         this.edge = edge;
118         updateBounds();
119     }
120
121     public void setColor(Color color) {
122         this.color = color;
123     }
124     
125     public Color getColor() {
126         return color;
127     }
128
129 }