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