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.25;
28 private static final double top = -0.25;
29 private static final double width = 0.5;
30 private static final double height = 0.5;
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;
43 private Double strokee;
51 public void render(Graphics2D g2d) {
52 AffineTransform ot = null;
53 AffineTransform t = getTransform();
54 if (t != null && !t.isIdentity()) {
55 ot = g2d.getTransform();
56 g2d.transform(getTransform());
59 Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
60 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
62 Color oldColor = g2d.getColor();
63 Stroke oldStroke = g2d.getStroke();
67 double scaleRecip = 1;
69 double scale = GeometryUtils.getScale(g2d.getTransform());
75 //System.out.println("scale: " + scale);
76 scaleRecip = 1.0 / scale;
78 scaleRecip = 8.0 * scaleRecip;
80 // Translate lat and lon to X and Y
81 Point2D res = calculatePoint2D(vertex);
85 toDraw = new Rectangle2D.Double(res.getX() - (HOVERED.getWidth() / 2 * scaleRecip), res.getY() - (HOVERED.getHeight() / 2 * scaleRecip), HOVERED.getWidth() * scaleRecip, HOVERED.getHeight() * scaleRecip);
87 toDraw = new Rectangle2D.Double(res.getX() - (NORMAL.getWidth() / 2 * scaleRecip), res.getY() - (NORMAL.getHeight() / 2 * scaleRecip), NORMAL.getWidth() * scaleRecip, NORMAL.getHeight() * scaleRecip);
93 g2d.setColor(oldColor);
94 g2d.setStroke(oldStroke);
96 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
103 public Rectangle2D getBounds() {
104 return super.getBounds();
108 public Rectangle2D getBoundsInLocal() {
112 private void updateBounds() {
113 Rectangle2D oldBounds = bounds;
114 if (oldBounds == null)
115 oldBounds = new Rectangle2D.Double();
116 bounds = calculateBounds(oldBounds);
120 public void setTransform(AffineTransform transform) {
121 super.setTransform(transform);
127 public AffineTransform getTransform() {
128 return super.getTransform();
131 private Rectangle2D calculateBounds(Rectangle2D rect) {
132 Point2D calcPoint = calculatePoint2D(vertex);
133 AffineTransform at = getTransform();
134 return new Rectangle2D.Double(calcPoint.getX(), calcPoint.getY(), 1 / at.getScaleX(), 1 / at.getScaleY()).getBounds2D();
137 private static Point2D calculatePoint2D(DistrictNetworkVertex vertex) {
138 Point2D point= vertex.getPoint();
139 double x = ModelledCRS.longitudeToX(point.getX());
140 double y = ModelledCRS.latitudeToY(-point.getY()); // Inverse because Simantics Diagram is inverted
143 Point2D res = new Point2D.Double(x, y);
147 public void setVertex(DistrictNetworkVertex vertex) {
148 this.vertex = vertex;
152 public boolean hover(boolean hover) {
153 // if (hover && LOGGER.isDebugEnabled())
154 // LOGGER.debug("Hovering " + this);
155 boolean changed = false;
156 if (this.hover != hover) {
163 public void setColor(Color color) {
167 public Color getColor() {
171 @PropertySetter(value = "stroke")
172 public void setStroke(Double stroke) {
173 this.strokee = stroke / 10;