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.INode;
14 import org.simantics.scenegraph.ISelectionPainterNode;
15 import org.simantics.scenegraph.g2d.G2DNode;
16 import org.simantics.scenegraph.g2d.G2DParentNode;
17 import org.simantics.scenegraph.g2d.nodes.SVGNode;
18 import org.simantics.scenegraph.utils.GeometryUtils;
19 import org.simantics.scenegraph.utils.NodeUtil;
21 public class DistrictNetworkVertexNode extends G2DParentNode implements ISelectionPainterNode {
23 //private static final Logger LOGGER = LoggerFactory.getLogger(DistrictNetworkVertexNode.class);
25 private static final long serialVersionUID = -2641639101400236719L;
27 private static final BasicStroke STROKE = new BasicStroke(4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
28 private static final Color SELECTION_COLOR = new Color(255, 0, 255, 96);
30 private static final double left = -0.25;
31 private static final double top = -0.25;
32 private static final double width = 0.5;
33 private static final double height = 0.5;
35 private static final Rectangle2D NORMAL = new Rectangle2D.Double(left, top, width, height);
36 private static final Rectangle2D HOVERED = new Rectangle2D.Double(left * 3, top * 3, width * 3, height * 3);
38 private DistrictNetworkVertex vertex;
40 private boolean scaleStroke = true;
41 private boolean hover;
44 private transient Color dynamicColor;
46 private Rectangle2D bounds;
47 private transient Point2D point;
48 private transient Rectangle2D rect;
50 private double nodeSize = 1;
58 public void render(Graphics2D g2d) {
62 AffineTransform ot = null;
63 AffineTransform t = getTransform();
64 if (t != null && !t.isIdentity()) {
65 ot = g2d.getTransform();
66 g2d.transform(getTransform());
69 Object oaaHint = null;
70 Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
71 if (aaHint != RenderingHints.VALUE_ANTIALIAS_OFF) {
73 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
76 Color oldColor = g2d.getColor();
77 Color newColor = dynamicColor != null ? dynamicColor : color;
78 boolean changeColor = !oldColor.equals(newColor);
80 double viewScaleRecip = 1;
82 double scale = GeometryUtils.getScale(g2d.getTransform());
83 scale = Math.max(10000, Math.min(scale, 50000));
84 viewScaleRecip = 1.0 / scale;
86 double scaleRecip = viewScaleRecip * nodeSize;
88 // Translate lat and lon to X and Y
89 Point2D p = point = calculatePoint2D(vertex, point);
90 Rectangle2D toDraw = calculateDrawnGeometry(p, hover ? HOVERED : NORMAL, rect, scaleRecip);
92 if (NodeUtil.isSelected(this, 1)) {
94 g2d.setColor(SELECTION_COLOR);
95 BasicStroke ss = GeometryUtils.scaleStroke(STROKE, (float) viewScaleRecip*5);
102 g2d.setColor(newColor);
107 g2d.setColor(oldColor);
109 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
112 for (INode nn : getNodes()) {
113 ((G2DNode)nn).setTransform(new AffineTransform(toDraw.getWidth(), 0.0, 0.0, toDraw.getHeight(), toDraw.getCenterX(), toDraw.getCenterY()));
114 ((G2DNode)nn).render(g2d);
118 g2d.setTransform(ot);
121 private Rectangle2D calculateDrawnGeometry(Point2D p, Rectangle2D margin, Rectangle2D result, double scaleRecip) {
123 result = new Rectangle2D.Double();
124 double mw = margin.getWidth();
125 double mh = margin.getHeight();
126 result.setFrame(p.getX() - (mw / 2 * scaleRecip), p.getY() - (mh / 2 * scaleRecip), mw * scaleRecip, mh * scaleRecip);
131 public Rectangle2D getBounds() {
132 return super.getBounds();
136 public Rectangle2D getBoundsInLocal() {
140 private void updateBounds() {
141 Rectangle2D oldBounds = bounds;
142 if (oldBounds == null)
143 oldBounds = new Rectangle2D.Double();
144 bounds = calculateBounds(oldBounds);
148 public void setTransform(AffineTransform transform) {
149 super.setTransform(transform);
155 public AffineTransform getTransform() {
156 return super.getTransform();
159 private Rectangle2D calculateBounds(Rectangle2D rect) {
160 Point2D calcPoint = calculatePoint2D(vertex, point);
161 AffineTransform at = getTransform();
162 return new Rectangle2D.Double(calcPoint.getX(), calcPoint.getY(), width / at.getScaleX(), height / at.getScaleY()).getBounds2D();
165 private static Point2D calculatePoint2D(DistrictNetworkVertex vertex, Point2D result) {
166 Point2D point = vertex.getPoint();
167 double x = ModelledCRS.longitudeToX(point.getX());
168 double y = ModelledCRS.latitudeToY(-point.getY()); // Inverse because Simantics Diagram is inverted
170 result = new Point2D.Double(x, y);
172 result.setLocation(x, y);
176 public void setVertex(DistrictNetworkVertex vertex) {
177 this.vertex = vertex;
181 public boolean hover(boolean hover) {
182 // if (hover && LOGGER.isDebugEnabled())
183 // LOGGER.debug("Hovering " + this);
184 boolean changed = false;
185 if (this.hover != hover) {
192 public void setColor(Color color) {
196 public Color getColor() {
200 @PropertySetter(value = "SVG")
201 public void setSVG(String value) {
202 for (INode nn : this.getNodes())
203 if (nn instanceof SVGNode)
204 ((SVGNode)nn).setData(value);
208 @PropertySetter(value = "size")
209 public void setSize(Double size) {
210 boolean changed = false;
212 changed = size != this.nodeSize;
213 this.nodeSize = size;
215 changed = this.nodeSize != 1.0;
222 @PropertySetter(value = "dynamicColor")
223 public void setDynamicColor(Color color) {
224 this.dynamicColor = color;