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.ui.adapters.DistrictNetworkVertex;
12 import org.simantics.scenegraph.INode;
13 import org.simantics.scenegraph.ISelectionPainterNode;
14 import org.simantics.scenegraph.g2d.G2DNode;
15 import org.simantics.scenegraph.g2d.G2DParentNode;
16 import org.simantics.scenegraph.g2d.nodes.SVGNode;
17 import org.simantics.scenegraph.utils.GeometryUtils;
18 import org.simantics.scenegraph.utils.NodeUtil;
20 public class DistrictNetworkVertexNode extends G2DParentNode implements ISelectionPainterNode {
22 //private static final Logger LOGGER = LoggerFactory.getLogger(DistrictNetworkVertexNode.class);
24 private static final long serialVersionUID = -2641639101400236719L;
26 private static final double left = -0.00005;
27 private static final double top = left;
28 public static final double width = 0.0001;
29 private static final double height = width;
31 private static final BasicStroke STROKE = new BasicStroke((float)width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
32 private static final Color SELECTION_COLOR = new Color(255, 0, 255, 96);
34 private static final Rectangle2D NORMAL = new Rectangle2D.Double(left, top, width, height);
35 private static final Rectangle2D HOVERED = new Rectangle2D.Double(left * 3, top * 3, width * 3, height * 3);
37 private DistrictNetworkVertex vertex;
39 private boolean scaleStroke = true;
40 private boolean hover;
43 private transient Color dynamicColor;
45 private Rectangle2D bounds;
46 private transient Point2D point;
47 private transient Rectangle2D rect;
48 private transient AffineTransform symbolTransform;
50 private double nodeSize = 3;
58 public void render(Graphics2D g2d) {
62 AffineTransform ot = null;
63 AffineTransform t = getTransform();
64 if (t != null && !t.isIdentity()) {
65 ot = g2d.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 viewScaleRecip *= DistrictNetworkNodeUtils.calculateScaleRecip(g2d.getTransform());
84 double scaleRecip = viewScaleRecip * nodeSize;
86 // Translate lat and lon to X and Y
87 Point2D p = point = DistrictNetworkNodeUtils.calculatePoint2D(vertex.getPoint(), point);
88 Rectangle2D toDraw = rect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, hover ? HOVERED : NORMAL, rect, scaleRecip);
90 if (NodeUtil.isSelected(this, 1)) {
92 g2d.setColor(SELECTION_COLOR);
93 BasicStroke ss = GeometryUtils.scaleStroke(STROKE, (float) (viewScaleRecip * nodeSize));
100 g2d.setColor(newColor);
105 g2d.setColor(oldColor);
107 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
110 for (INode nn : getNodes()) {
111 G2DNode g2dNode = (G2DNode)nn;
112 if (nn instanceof SVGNode) {
113 symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(toDraw, symbolTransform);
114 g2dNode.setTransform(symbolTransform);
120 g2d.setTransform(ot);
124 public Rectangle2D getBounds() {
125 return super.getBounds();
129 public Rectangle2D getBoundsInLocal() {
133 private void updateBounds() {
134 Rectangle2D oldBounds = bounds;
135 if (oldBounds == null)
136 oldBounds = new Rectangle2D.Double();
137 bounds = calculateBounds(oldBounds);
141 public void setTransform(AffineTransform transform) {
142 super.setTransform(transform);
148 public AffineTransform getTransform() {
149 return super.getTransform();
152 private Rectangle2D calculateBounds(Rectangle2D rect) {
153 Point2D calcPoint = DistrictNetworkNodeUtils.calculatePoint2D(vertex.getPoint(), point);
154 AffineTransform at = getTransform();
155 double x = calcPoint.getX();
156 double y = calcPoint.getY();
157 double widthh = width / at.getScaleX();
158 double heighth = height / at.getScaleY();
159 return new Rectangle2D.Double(x - widthh, y - heighth, widthh * 2, heighth * 2).getBounds2D();
162 public void setVertex(DistrictNetworkVertex vertex) {
163 this.vertex = vertex;
167 public boolean hover(boolean hover) {
168 // if (hover && LOGGER.isDebugEnabled())
169 // LOGGER.debug("Hovering " + this);
170 boolean changed = false;
171 if (this.hover != hover) {
178 public void setColor(Color color) {
182 public Color getColor() {
186 @PropertySetter(value = "SVG")
187 public void setSVG(String value) {
188 for (INode nn : this.getNodes())
189 if (nn instanceof SVGNode)
190 ((SVGNode)nn).setData(value);
194 @PropertySetter(value = "size")
195 public void setSize(Double size) {
196 boolean changed = false;
198 changed = size != this.nodeSize;
199 this.nodeSize = size;
201 changed = this.nodeSize != 1.0;
208 @PropertySetter(value = "dynamicColor")
209 public void setDynamicColor(Color color) {
210 this.dynamicColor = color;