]> gerrit.simantics Code Review - simantics/district.git/blob
9229932400a8ff12b964621a3643c2102bb0b634
[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.Rectangle2D;
11
12 import org.simantics.district.network.ModelledCRS;
13 import org.simantics.district.network.ui.DistrictNetworkEdge;
14 import org.simantics.scenegraph.g2d.G2DNode;
15 import org.simantics.scenegraph.utils.GeometryUtils;
16 import org.simantics.scenegraph.utils.NodeUtil;
17
18 public class DistrictNetworkEdgeNode extends G2DNode {
19
20     private static final long serialVersionUID = 8049769475036519806L;
21     
22     private static final Stroke     SELECTION_STROKE = new BasicStroke(1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);
23     private static final Color      SELECTION_COLOR  = new Color(255, 0, 255, 96);
24     
25     private DistrictNetworkEdge edge;
26     private Rectangle2D bounds;
27     
28     private static final Stroke STROKE = new BasicStroke(4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
29     private boolean scaleStroke = true;
30
31     private Color color;
32
33     private Double stroke;
34
35     @Override
36     public void init() {
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                 double str;
66                 if (stroke != null)
67                     str = Math.abs(stroke);
68                 else
69                     str = 1.0;
70                 BasicStroke bs = GeometryUtils.scaleStroke(STROKE, (float) (str / GeometryUtils.getScale(g2d.getTransform())));
71                 g2d.setStroke(bs);
72             } else {
73                 g2d.setStroke(STROKE);
74             }
75         }
76
77         g2d.draw(calculatePath(edge));
78         
79         // Reset
80         g2d.setStroke(oldStroke);
81         g2d.setColor(oldColor);
82         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
83         
84         if (ot != null)
85             g2d.setTransform(ot);
86     }
87     
88     public static Path2D calculatePath(DistrictNetworkEdge edge) {
89         // Convert to screen coordinates
90         double startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX());
91         double startY = ModelledCRS.latitudeToY(-edge.getStartPoint().getY()); // Invert for Simantics
92         double endX = ModelledCRS.longitudeToX(edge.getEndPoint().getX());
93         double endY = ModelledCRS.latitudeToY(-edge.getEndPoint().getY());// Invert for Simantics
94         
95         // render
96         Path2D path = new Path2D.Double();
97         path.moveTo(startX, startY);
98         path.lineTo(endX, endY);
99         return path;
100     }
101
102     private boolean isSelected() {
103         return NodeUtil.isSelected(this, 1);
104     }
105
106     @Override
107     public Rectangle2D getBoundsInLocal() {
108         return bounds;
109     }
110     
111     private void updateBounds() {
112         Rectangle2D oldBounds = bounds;
113         if (oldBounds == null)
114             oldBounds = new Rectangle2D.Double();
115         bounds = calculateBounds(oldBounds);
116     }
117
118     private Rectangle2D calculateBounds(Rectangle2D rect) {
119         return calculatePath(edge).getBounds2D();
120     }
121
122     public void setDNEdge(DistrictNetworkEdge edge) {
123         this.edge = edge;
124         updateBounds();
125     }
126
127     public void setColor(Color color) {
128         this.color = color;
129     }
130     
131     public Color getColor() {
132         return color;
133     }
134
135     @PropertySetter(value = "stroke")
136     public void setStroke(Double stroke) {
137         this.stroke = stroke;
138     }
139
140 }