]> gerrit.simantics Code Review - simantics/district.git/blob
cd35b2f011329ac926fa0e6d51f8b7a0c5046fe5
[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     
39     @Override
40     public void render(Graphics2D g2d) {
41         
42         AffineTransform ot = null;
43         AffineTransform t = getTransform();
44         if (t != null && !t.isIdentity()) {
45             ot = g2d.getTransform();
46             g2d.transform(getTransform());
47         }
48         
49         Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
50         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
51
52         Color oldColor = g2d.getColor();
53         BasicStroke oldStroke = (BasicStroke) g2d.getStroke();
54 //        boolean selected = isSelected();
55 //        if (selected) {
56 //            Path2D selectionPath = edge.getPath();
57 //            Shape selectionShape = SELECTION_STROKE.createStrokedShape(selectionPath);
58 //            g2d.setColor(SELECTION_COLOR);
59 //            g2d.fill(selectionShape);
60 //        }
61 //        
62         g2d.setColor(color);
63         if (STROKE != null) {
64             if (scaleStroke && STROKE instanceof BasicStroke) {
65                 BasicStroke bs = GeometryUtils.scaleStroke(STROKE, (float) (1.0 / GeometryUtils.getScale(g2d.getTransform())));
66                 g2d.setStroke(bs);
67             } else {
68                 g2d.setStroke(STROKE);
69             }
70         }
71
72         g2d.draw(calculatePath(edge));
73         
74         // Reset
75         g2d.setStroke(oldStroke);
76         g2d.setColor(oldColor);
77         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
78         
79         if (ot != null)
80             g2d.setTransform(ot);
81     }
82     
83     public static Path2D calculatePath(DistrictNetworkEdge edge) {
84         // Convert to screen coordinates
85         double startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX());
86         double startY = ModelledCRS.latitudeToY(edge.getStartPoint().getY());
87         double endX = ModelledCRS.longitudeToX(edge.getEndPoint().getX());
88         double endY = ModelledCRS.latitudeToY(edge.getEndPoint().getY());
89         
90         // render
91         Path2D path = new Path2D.Double();
92         path.moveTo(startX, startY);
93         path.lineTo(endX, endY);
94         return path;
95     }
96
97     private boolean isSelected() {
98         return NodeUtil.isSelected(this, 1);
99     }
100
101     @Override
102     public Rectangle2D getBoundsInLocal() {
103         return bounds;
104     }
105     
106     private void updateBounds() {
107         Rectangle2D oldBounds = bounds;
108         if (oldBounds == null)
109             oldBounds = new Rectangle2D.Double();
110         bounds = calculateBounds(oldBounds);
111     }
112
113     private Rectangle2D calculateBounds(Rectangle2D rect) {
114         return calculatePath(edge).getBounds2D();
115     }
116
117     public void setDNEdge(DistrictNetworkEdge edge) {
118         this.edge = edge;
119         updateBounds();
120     }
121
122     public void setColor(Color color) {
123         this.color = color;
124     }
125     
126     public Color getColor() {
127         return color;
128     }
129
130 }