1 package org.simantics.district.network.ui.nodes;
4 import java.awt.Graphics2D;
5 import java.awt.RenderingHints;
6 import java.awt.geom.AffineTransform;
7 import java.awt.geom.Point2D;
8 import java.awt.geom.Rectangle2D;
10 import org.simantics.district.network.ModelledCRS;
11 import org.simantics.district.network.ui.adapters.DistrictNetworkVertex;
12 import org.simantics.scenegraph.g2d.G2DNode;
13 import org.simantics.scenegraph.utils.GeometryUtils;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
17 public class DistrictNetworkVertexNode extends G2DNode {
19 private static final Logger LOGGER = LoggerFactory.getLogger(DistrictNetworkVertexNode.class);
21 private static final long serialVersionUID = -2641639101400236719L;
22 private DistrictNetworkVertex vertex;
24 private static final double left = -0.25;
25 private static final double top = -0.25;
26 private static final double width = 0.5;
27 private static final double height = 0.5;
29 private static final Rectangle2D NORMAL = new Rectangle2D.Double(left, top, width, height);
30 private static final Rectangle2D HOVERED = new Rectangle2D.Double(left * 3, top * 3, width * 3, height * 3);
32 private boolean scaleStroke = true;
33 private boolean hover;
37 private Rectangle2D bounds;
39 private double nodeSize = 1;
47 public void render(Graphics2D g2d) {
51 AffineTransform ot = null;
52 AffineTransform t = getTransform();
53 if (t != null && !t.isIdentity()) {
54 ot = g2d.getTransform();
55 g2d.transform(getTransform());
58 Object oaaHint = null;
59 Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
60 if (aaHint != RenderingHints.VALUE_ANTIALIAS_OFF) {
62 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
65 Color oldColor = g2d.getColor();
68 double scaleRecip = 1;
70 double scale = GeometryUtils.getScale(g2d.getTransform());
71 scale = Math.max(10000, Math.min(scale, 50000));
72 scaleRecip = 1.0 / scale;
74 scaleRecip = scaleRecip * nodeSize;
76 // Translate lat and lon to X and Y
77 Point2D res = calculatePoint2D(vertex);
81 toDraw = new Rectangle2D.Double(res.getX() - (HOVERED.getWidth() / 2 * scaleRecip), res.getY() - (HOVERED.getHeight() / 2 * scaleRecip), HOVERED.getWidth() * scaleRecip, HOVERED.getHeight() * scaleRecip);
83 toDraw = new Rectangle2D.Double(res.getX() - (NORMAL.getWidth() / 2 * scaleRecip), res.getY() - (NORMAL.getHeight() / 2 * scaleRecip), NORMAL.getWidth() * scaleRecip, NORMAL.getHeight() * scaleRecip);
89 g2d.setColor(oldColor);
91 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(), width / at.getScaleX(), height / 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()); // Inverse because Simantics Diagram is inverted
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() {
165 @PropertySetter(value = "size")
166 public void setSize(Double size) {
167 boolean changed = false;
169 changed = size != this.nodeSize;
170 this.nodeSize = size;
172 changed = this.nodeSize != 1.0;