]> gerrit.simantics Code Review - simantics/district.git/blob
c566981e05261b5f735ae18c77538a18a97a2dc2
[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.5;
28     private static final double top = -0.5;
29     private static final double width = 1;
30     private static final double height = 1;
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     @Override
44     public void init() {
45         setZIndex(2);
46     }
47     
48     @Override
49     public void render(Graphics2D g2d) {
50         AffineTransform ot = null;
51         AffineTransform t = getTransform();
52         if (t != null && !t.isIdentity()) {
53             ot = g2d.getTransform();
54             g2d.transform(getTransform());
55         }
56         
57         Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
58         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
59         
60         Color oldColor = g2d.getColor();
61         Stroke oldStroke = g2d.getStroke();
62         
63         g2d.setColor(color);
64         
65         double scaleRecip = 1;
66         if (scaleStroke) {
67             double scale = GeometryUtils.getScale(g2d.getTransform());
68             
69             //System.out.println("scale: " + scale);
70             scaleRecip = 1.0 / scale;
71         }
72         scaleRecip = 8.0 * scaleRecip;
73         
74         // Translate lat and lon to X and Y
75         Point2D res = calculatePoint2D(vertex);
76         
77         Rectangle2D toDraw;
78         if (hover) {
79             toDraw = new Rectangle2D.Double(res.getX() - (HOVERED.getWidth() / 2 * scaleRecip), res.getY() - (HOVERED.getHeight() / 2 * scaleRecip), HOVERED.getWidth() * scaleRecip, HOVERED.getHeight() * scaleRecip);
80         } else {
81             toDraw = new Rectangle2D.Double(res.getX() - (NORMAL.getWidth() / 2 * scaleRecip), res.getY() - (NORMAL.getHeight() / 2 * scaleRecip), NORMAL.getWidth() * scaleRecip, NORMAL.getHeight() * scaleRecip);
82         }
83         // render
84         g2d.fill(toDraw);
85         
86         // Reset stats
87         g2d.setColor(oldColor);
88         g2d.setStroke(oldStroke);
89         
90         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
91         
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(), 1 / at.getScaleX(), 1 / 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());
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 }