]> gerrit.simantics Code Review - simantics/district.git/blob
99eb3cc117b7fb91cbb8352016a62da7e9836249
[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.Point2D;
10 import java.awt.geom.Rectangle2D;
11
12 import org.simantics.district.network.ModelledCRS;
13 import org.simantics.district.network.ui.adapters.DistrictNetworkVertex;
14 import org.simantics.scenegraph.ParentNode;
15 import org.simantics.scenegraph.g2d.G2DNode;
16 import org.simantics.scenegraph.utils.GeometryUtils;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 public class DistrictNetworkVertexNode extends G2DNode {
21
22     private static final Logger LOGGER = LoggerFactory.getLogger(DistrictNetworkVertexNode.class);
23     
24     private static final long serialVersionUID = -2641639101400236719L;
25     private DistrictNetworkVertex vertex;
26
27     private static final double left = -0.25;
28     private static final double top = -0.25;
29     private static final double width = 0.5;
30     private static final double height = 0.5;
31     
32     private static final Rectangle2D NORMAL = new Rectangle2D.Double(left, top, width, height);
33     private static final Rectangle2D HOVERED = new Rectangle2D.Double(left * 3, top * 3, width * 3, height * 3);
34     
35     private Stroke stroke = new BasicStroke(2);
36     private boolean scaleStroke = true;
37     private boolean hover;
38
39     private Color color;
40
41     private Rectangle2D bounds;
42
43     private Double strokee;
44
45     @Override
46     public void init() {
47         setZIndex(2);
48     }
49     
50     @Override
51     public void render(Graphics2D g2d) {
52         AffineTransform ot = null;
53         AffineTransform t = getTransform();
54         if (t != null && !t.isIdentity()) {
55             ot = g2d.getTransform();
56             g2d.transform(getTransform());
57         }
58         
59         Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
60         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
61         
62         Color oldColor = g2d.getColor();
63         Stroke oldStroke = g2d.getStroke();
64         
65         g2d.setColor(color);
66         
67         double scaleRecip = 1;
68         if (scaleStroke) {
69             double scale = GeometryUtils.getScale(g2d.getTransform());
70             double str;
71             if (strokee != null)
72                 str = strokee;
73             else
74                 str = 1.0;
75             //System.out.println("scale: " + scale);
76             scaleRecip = 1.0 / scale;
77         }
78         scaleRecip = 8.0 * scaleRecip;
79         
80         // Translate lat and lon to X and Y
81         Point2D res = calculatePoint2D(vertex);
82         
83         Rectangle2D toDraw;
84         if (hover) {
85             toDraw = new Rectangle2D.Double(res.getX() - (HOVERED.getWidth() / 2 * scaleRecip), res.getY() - (HOVERED.getHeight() / 2 * scaleRecip), HOVERED.getWidth() * scaleRecip, HOVERED.getHeight() * scaleRecip);
86         } else {
87             toDraw = new Rectangle2D.Double(res.getX() - (NORMAL.getWidth() / 2 * scaleRecip), res.getY() - (NORMAL.getHeight() / 2 * scaleRecip), NORMAL.getWidth() * scaleRecip, NORMAL.getHeight() * scaleRecip);
88         }
89         // render
90         g2d.fill(toDraw);
91         
92         // Reset stats
93         g2d.setColor(oldColor);
94         g2d.setStroke(oldStroke);
95         
96         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
97         
98         if (ot != null)
99             g2d.setTransform(ot);
100     }
101
102     @Override
103     public Rectangle2D getBounds() {
104         return super.getBounds();
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     @Override
120     public void setTransform(AffineTransform transform) {
121         super.setTransform(transform);
122         // Update bounds
123         updateBounds();
124     }
125     
126     @Override
127     public AffineTransform getTransform() {
128         return super.getTransform();
129     }
130     
131     private Rectangle2D calculateBounds(Rectangle2D rect) {
132         Point2D calcPoint = calculatePoint2D(vertex);
133         AffineTransform at = getTransform();
134         return new Rectangle2D.Double(calcPoint.getX(), calcPoint.getY(), 1 / at.getScaleX(), 1 / at.getScaleY()).getBounds2D();
135     }
136
137     private static Point2D calculatePoint2D(DistrictNetworkVertex vertex) {
138         Point2D point= vertex.getPoint();
139         double x = ModelledCRS.longitudeToX(point.getX());
140         double y = ModelledCRS.latitudeToY(-point.getY()); // Inverse because Simantics Diagram is inverted
141
142         // Apply the scaling
143         Point2D res = new Point2D.Double(x, y);
144         return res;
145     }
146
147     public void setVertex(DistrictNetworkVertex vertex) {
148         this.vertex = vertex;
149         updateBounds();
150     }
151     
152     public boolean hover(boolean hover) {
153 //        if (hover && LOGGER.isDebugEnabled())
154 //            LOGGER.debug("Hovering " + this);
155         boolean changed = false;
156         if (this.hover != hover) {
157             this.hover = hover;
158             changed = true;
159         }
160         return changed;
161     }
162
163     public void setColor(Color color) {
164         this.color = color;
165     }
166     
167     public Color getColor() {
168         return color;
169     }
170
171     @PropertySetter(value = "stroke")
172     public void setStroke(Double stroke) {
173         this.strokee = stroke / 10;
174     }
175 }