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.g2d.G2DNode;
15 import org.simantics.scenegraph.utils.GeometryUtils;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
19 public class DistrictNetworkVertexNode extends G2DNode {
21 private static final Logger LOGGER = LoggerFactory.getLogger(DistrictNetworkVertexNode.class);
23 private static final long serialVersionUID = -2641639101400236719L;
24 private DistrictNetworkVertex vertex;
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;
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);
34 private Stroke stroke = new BasicStroke(2);
35 private boolean scaleStroke = true;
36 private boolean hover;
40 private Rectangle2D bounds;
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());
56 Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
57 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
59 Color oldColor = g2d.getColor();
60 Stroke oldStroke = g2d.getStroke();
64 double scaleRecip = 1;
66 double scale = GeometryUtils.getScale(g2d.getTransform());
68 //System.out.println("scale: " + scale);
69 scaleRecip = 1.0 / scale;
71 scaleRecip = 8.0 * scaleRecip;
73 // Translate lat and lon to X and Y
74 Point2D res = calculatePoint2D(vertex);
78 toDraw = new Rectangle2D.Double(res.getX() - (HOVERED.getWidth() / 2 * scaleRecip), res.getY() - (HOVERED.getHeight() / 2 * scaleRecip), HOVERED.getWidth() * scaleRecip, HOVERED.getHeight() * scaleRecip);
80 toDraw = new Rectangle2D.Double(res.getX() - (NORMAL.getWidth() / 2 * scaleRecip), res.getY() - (NORMAL.getHeight() / 2 * scaleRecip), NORMAL.getWidth() * scaleRecip, NORMAL.getHeight() * scaleRecip);
86 g2d.setColor(oldColor);
87 g2d.setStroke(oldStroke);
89 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
96 public Rectangle2D getBounds() {
97 return super.getBounds();
101 public Rectangle2D getBoundsInLocal() {
105 private void updateBounds() {
106 Rectangle2D oldBounds = bounds;
107 if (oldBounds == null)
108 oldBounds = new Rectangle2D.Double();
109 bounds = calculateBounds(oldBounds);
113 public void setTransform(AffineTransform transform) {
114 super.setTransform(transform);
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();
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());
131 Point2D res = new Point2D.Double(x, y);
135 public void setVertex(DistrictNetworkVertex vertex) {
136 this.vertex = vertex;
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) {
151 public void setColor(Color color) {
155 public Color getColor() {