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