1 package org.simantics.district.network.ui.nodes;
3 import java.awt.BasicStroke;
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;
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;
20 public class DistrictNetworkVertexNode extends G2DNode {
22 private static final Logger LOGGER = LoggerFactory.getLogger(DistrictNetworkVertexNode.class);
24 private static final long serialVersionUID = -2641639101400236719L;
25 private DistrictNetworkVertex vertex;
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;
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);
35 private Stroke stroke = new BasicStroke(2);
36 private boolean scaleStroke = true;
37 private boolean hover;
41 private Rectangle2D bounds;
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());
57 Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
58 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
60 Color oldColor = g2d.getColor();
61 Stroke oldStroke = g2d.getStroke();
65 double scaleRecip = 1;
67 double scale = GeometryUtils.getScale(g2d.getTransform());
69 //System.out.println("scale: " + scale);
70 scaleRecip = 1.0 / scale;
72 scaleRecip = 8.0 * scaleRecip;
74 // Translate lat and lon to X and Y
75 Point2D res = calculatePoint2D(vertex);
79 toDraw = new Rectangle2D.Double(res.getX() - (HOVERED.getWidth() / 2 * scaleRecip), res.getY() - (HOVERED.getHeight() / 2 * scaleRecip), HOVERED.getWidth() * scaleRecip, HOVERED.getHeight() * scaleRecip);
81 toDraw = new Rectangle2D.Double(res.getX() - (NORMAL.getWidth() / 2 * scaleRecip), res.getY() - (NORMAL.getHeight() / 2 * scaleRecip), NORMAL.getWidth() * scaleRecip, NORMAL.getHeight() * scaleRecip);
87 g2d.setColor(oldColor);
88 g2d.setStroke(oldStroke);
90 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
97 public Rectangle2D getBounds() {
98 return super.getBounds();
102 public Rectangle2D getBoundsInLocal() {
106 private void updateBounds() {
107 Rectangle2D oldBounds = bounds;
108 if (oldBounds == null)
109 oldBounds = new Rectangle2D.Double();
110 bounds = calculateBounds(oldBounds);
114 public void setTransform(AffineTransform transform) {
115 super.setTransform(transform);
121 public AffineTransform getTransform() {
122 return super.getTransform();
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();
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());
137 Point2D res = new Point2D.Double(x, y);
141 public void setVertex(DistrictNetworkVertex vertex) {
142 this.vertex = vertex;
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) {
157 public void setColor(Color color) {
161 public Color getColor() {