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.maps.MapScalingTransform;
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.G2DRenderingHints;
18 import org.simantics.scenegraph.g2d.IG2DNode;
19 import org.simantics.scenegraph.g2d.nodes.SVGNode;
20 import org.simantics.scenegraph.utils.GeometryUtils;
21 import org.simantics.scenegraph.utils.NodeUtil;
23 public class DistrictNetworkVertexNode extends G2DParentNode implements ISelectionPainterNode, HoverSensitiveNode {
25 //private static final Logger LOGGER = LoggerFactory.getLogger(DistrictNetworkVertexNode.class);
27 private static final long serialVersionUID = -2641639101400236719L;
29 private static final double left = -15;
30 private static final double top = left;
31 public static final double width = 30;
32 private static final double height = width;
34 private static final BasicStroke STROKE = new BasicStroke((float)width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
35 private static final Color SELECTION_COLOR = new Color(255, 0, 255, 96);
37 private static final Rectangle2D NORMAL = new Rectangle2D.Double(left, top, width, height);
38 private static final Rectangle2D HOVERED = new Rectangle2D.Double(left * 2, top * 2, width * 2, height * 2);
40 private DistrictNetworkVertex vertex;
42 private static boolean scaleStroke = true;
43 private boolean hover;
46 private transient Color dynamicColor;
48 private Rectangle2D bounds;
49 private transient Point2D point;
50 private transient Rectangle2D rect;
51 private transient AffineTransform symbolTransform;
53 private double nodeSize = 1.0;
55 private boolean hidden = false;
63 public void render(Graphics2D g2d) {
64 AffineTransform ot = null;
65 AffineTransform t = getTransform();
66 double viewScaleRecip = scaleStroke ? (Double) g2d.getRenderingHint(DistrictRenderingHints.KEY_VIEW_SCALE_RECIPROCAL_UNDER_SPATIAL_ROOT) : 1.0;
67 if (t != null && !t.isIdentity()) {
68 //ot = g2d.getTransform();
69 ot = (AffineTransform) g2d.getRenderingHint(G2DRenderingHints.KEY_TRANSFORM_UNDER_SPATIAL_ROOT);
71 ot = g2d.getTransform();
75 AffineTransform work = DistrictNetworkNodeUtils.sharedTransform.get();
76 work.setTransform(ot);
78 viewScaleRecip = DistrictNetworkNodeUtils.calculateScaleRecip(work);
82 // Translate lat and lon to X and Y
83 Point2D p = point = DistrictNetworkNodeUtils.calculatePoint2D(vertex.getPoint(), point);
85 if (!hidden && nodeSize > 0.0) {
86 Object oaaHint = null;
87 Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
88 if (aaHint != RenderingHints.VALUE_ANTIALIAS_OFF) {
90 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
93 Color oldColor = g2d.getColor();
94 Color newColor = dynamicColor != null ? dynamicColor : color;
95 boolean changeColor = !oldColor.equals(newColor);
97 double scaleRecip = viewScaleRecip * nodeSize;
99 Rectangle2D toDraw = rect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, hover ? HOVERED : NORMAL, rect, scaleRecip);
101 if (NodeUtil.isSelected(this, 1)) {
103 g2d.setColor(SELECTION_COLOR);
104 BasicStroke ss = GeometryUtils.scaleStroke(STROKE, (float)viewScaleRecip);
111 g2d.setColor(newColor);
116 g2d.setColor(oldColor);
118 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
122 for (INode nn : getNodes()) {
123 G2DNode g2dNode = (G2DNode)nn;
124 if (nn instanceof SVGNode) {
125 Rectangle2D toDraw = rect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, hover ? HOVERED : NORMAL, rect, viewScaleRecip);
126 symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(toDraw, symbolTransform);
127 g2dNode.setTransform(symbolTransform);
134 g2d.setTransform(ot);
138 public Rectangle2D getBounds() {
139 return super.getBounds();
143 public Rectangle2D getBoundsInLocal() {
148 private void updateBounds() {
149 Rectangle2D oldBounds = bounds;
150 if (oldBounds == null)
151 oldBounds = new Rectangle2D.Double();
152 bounds = calculateBounds(oldBounds);
156 public void setTransform(AffineTransform transform) {
157 super.setTransform(transform);
162 private Rectangle2D calculateBounds(Rectangle2D rect) {
163 Point2D calcPoint = DistrictNetworkNodeUtils.calculatePoint2D(vertex.getPoint(), point);
164 AffineTransform at = NodeUtil.getLocalToGlobalTransform(this);
165 at.concatenate(MapScalingTransform.INSTANCE);
166 double x = calcPoint.getX();
167 double y = calcPoint.getY();
168 double scaleRecip = DistrictNetworkNodeUtils.calculateScaleRecip(at);
169 double widthh = width * scaleRecip * nodeSize;
170 double heighth = height * scaleRecip * nodeSize;
172 rect = new Rectangle2D.Double();
173 rect.setRect(x - widthh/2, y - heighth/2, widthh, heighth);
177 public void setVertex(DistrictNetworkVertex vertex) {
178 this.vertex = vertex;
183 public boolean hover(boolean hover, boolean isConnectionTool) {
184 // Only react to hover when the connection tool is active
185 boolean doHover = hover && isConnectionTool;
186 boolean changed = this.hover != doHover;
187 this.hover = doHover;
189 for (IG2DNode child : getNodes()) {
190 if (child instanceof HoverSensitiveNode)
191 changed = ((HoverSensitiveNode)child).hover(hover, isConnectionTool) || changed;
198 public void setMousePosition(Point2D p) {
199 for (IG2DNode child : getNodes()) {
200 if (child instanceof HoverSensitiveNode)
201 ((HoverSensitiveNode) child).setMousePosition(p);
205 public void setColor(Color color) {
209 public Color getColor() {
213 @PropertySetter(value = "SVG")
214 public void setSVG(String value) {
215 for (INode nn : this.getNodes())
216 if (nn instanceof SVGNode)
217 ((SVGNode)nn).setData(value);
221 @PropertySetter(value = "size")
222 public void setSize(Double size) {
223 boolean changed = false;
225 changed = size != this.nodeSize;
226 this.nodeSize = size;
228 changed = this.nodeSize != 1.0;
235 @PropertySetter(value = "dynamicColor")
236 public void setDynamicColor(Color color) {
237 this.dynamicColor = color;
240 @PropertySetter(value = "hidden")
241 public void setHidden(Boolean value) {