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.geom.AffineTransform;
8 import java.awt.geom.Point2D;
9 import java.awt.geom.Rectangle2D;
11 import org.simantics.district.network.ModelledCRS;
12 import org.simantics.district.network.ui.adapters.DistrictNetworkVertex;
13 import org.simantics.scenegraph.ISelectionPainterNode;
14 import org.simantics.scenegraph.g2d.G2DNode;
15 import org.simantics.scenegraph.utils.GeometryUtils;
16 import org.simantics.scenegraph.utils.NodeUtil;
18 public class DistrictNetworkVertexNode extends G2DNode implements ISelectionPainterNode {
20 //private static final Logger LOGGER = LoggerFactory.getLogger(DistrictNetworkVertexNode.class);
22 private static final long serialVersionUID = -2641639101400236719L;
24 private static final BasicStroke STROKE = new BasicStroke(4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
25 private static final Color SELECTION_COLOR = new Color(255, 0, 255, 96);
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 DistrictNetworkVertex vertex;
37 private boolean scaleStroke = true;
38 private boolean hover;
41 private transient Color dynamicColor;
43 private Rectangle2D bounds;
44 private transient Point2D point;
45 private transient Rectangle2D rect;
47 private double nodeSize = 1;
55 public void render(Graphics2D g2d) {
59 AffineTransform ot = null;
60 AffineTransform t = getTransform();
61 if (t != null && !t.isIdentity()) {
62 ot = g2d.getTransform();
63 g2d.transform(getTransform());
66 Object oaaHint = null;
67 Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
68 if (aaHint != RenderingHints.VALUE_ANTIALIAS_OFF) {
70 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
73 Color oldColor = g2d.getColor();
74 Color newColor = dynamicColor != null ? dynamicColor : color;
75 boolean changeColor = !oldColor.equals(newColor);
77 double viewScaleRecip = 1;
79 double scale = GeometryUtils.getScale(g2d.getTransform());
80 scale = Math.max(10000, Math.min(scale, 50000));
81 viewScaleRecip = 1.0 / scale;
83 double scaleRecip = viewScaleRecip * nodeSize;
85 // Translate lat and lon to X and Y
86 Point2D p = point = calculatePoint2D(vertex, point);
87 Rectangle2D toDraw = calculateDrawnGeometry(p, hover ? HOVERED : NORMAL, rect, scaleRecip);
89 if (NodeUtil.isSelected(this, 1)) {
91 g2d.setColor(SELECTION_COLOR);
92 BasicStroke ss = GeometryUtils.scaleStroke(STROKE, (float) viewScaleRecip*5);
99 g2d.setColor(newColor);
104 g2d.setColor(oldColor);
106 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
108 g2d.setTransform(ot);
111 private Rectangle2D calculateDrawnGeometry(Point2D p, Rectangle2D margin, Rectangle2D result, double scaleRecip) {
113 result = new Rectangle2D.Double();
114 double mw = margin.getWidth();
115 double mh = margin.getHeight();
116 result.setFrame(p.getX() - (mw / 2 * scaleRecip), p.getY() - (mh / 2 * scaleRecip), mw * scaleRecip, mh * scaleRecip);
121 public Rectangle2D getBounds() {
122 return super.getBounds();
126 public Rectangle2D getBoundsInLocal() {
130 private void updateBounds() {
131 Rectangle2D oldBounds = bounds;
132 if (oldBounds == null)
133 oldBounds = new Rectangle2D.Double();
134 bounds = calculateBounds(oldBounds);
138 public void setTransform(AffineTransform transform) {
139 super.setTransform(transform);
145 public AffineTransform getTransform() {
146 return super.getTransform();
149 private Rectangle2D calculateBounds(Rectangle2D rect) {
150 Point2D calcPoint = calculatePoint2D(vertex, point);
151 AffineTransform at = getTransform();
152 return new Rectangle2D.Double(calcPoint.getX(), calcPoint.getY(), width / at.getScaleX(), height / at.getScaleY()).getBounds2D();
155 private static Point2D calculatePoint2D(DistrictNetworkVertex vertex, Point2D result) {
156 Point2D point = vertex.getPoint();
157 double x = ModelledCRS.longitudeToX(point.getX());
158 double y = ModelledCRS.latitudeToY(-point.getY()); // Inverse because Simantics Diagram is inverted
160 result = new Point2D.Double(x, y);
162 result.setLocation(x, y);
166 public void setVertex(DistrictNetworkVertex vertex) {
167 this.vertex = vertex;
171 public boolean hover(boolean hover) {
172 // if (hover && LOGGER.isDebugEnabled())
173 // LOGGER.debug("Hovering " + this);
174 boolean changed = false;
175 if (this.hover != hover) {
182 public void setColor(Color color) {
186 public Color getColor() {
190 @PropertySetter(value = "size")
191 public void setSize(Double size) {
192 boolean changed = false;
194 changed = size != this.nodeSize;
195 this.nodeSize = size;
197 changed = this.nodeSize != 1.0;
204 @PropertySetter(value = "dynamicColor")
205 public void setDynamicColor(Color color) {
206 this.dynamicColor = color;