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