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);
73 AffineTransform work = DistrictNetworkNodeUtils.sharedTransform.get();
74 work.setTransform(ot);
76 viewScaleRecip = DistrictNetworkNodeUtils.calculateScaleRecip(work);
80 // Translate lat and lon to X and Y
81 Point2D p = point = DistrictNetworkNodeUtils.calculatePoint2D(vertex.getPoint(), point);
83 if (!hidden && nodeSize > 0.0) {
84 Object oaaHint = null;
85 Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
86 if (aaHint != RenderingHints.VALUE_ANTIALIAS_OFF) {
88 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
91 Color oldColor = g2d.getColor();
92 Color newColor = dynamicColor != null ? dynamicColor : color;
93 boolean changeColor = !oldColor.equals(newColor);
95 double scaleRecip = viewScaleRecip * nodeSize;
97 Rectangle2D toDraw = rect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, hover ? HOVERED : NORMAL, rect, scaleRecip);
99 if (NodeUtil.isSelected(this, 1)) {
101 g2d.setColor(SELECTION_COLOR);
102 BasicStroke ss = GeometryUtils.scaleStroke(STROKE, (float)viewScaleRecip);
109 g2d.setColor(newColor);
114 g2d.setColor(oldColor);
116 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
120 for (INode nn : getNodes()) {
121 G2DNode g2dNode = (G2DNode)nn;
122 if (nn instanceof SVGNode) {
123 Rectangle2D toDraw = rect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, hover ? HOVERED : NORMAL, rect, viewScaleRecip);
124 symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(toDraw, symbolTransform);
125 g2dNode.setTransform(symbolTransform);
132 g2d.setTransform(ot);
136 public Rectangle2D getBounds() {
137 return super.getBounds();
141 public Rectangle2D getBoundsInLocal() {
146 private void updateBounds() {
147 Rectangle2D oldBounds = bounds;
148 if (oldBounds == null)
149 oldBounds = new Rectangle2D.Double();
150 bounds = calculateBounds(oldBounds);
154 public void setTransform(AffineTransform transform) {
155 super.setTransform(transform);
160 private Rectangle2D calculateBounds(Rectangle2D rect) {
161 Point2D calcPoint = DistrictNetworkNodeUtils.calculatePoint2D(vertex.getPoint(), point);
162 AffineTransform at = NodeUtil.getLocalToGlobalTransform(this);
163 at.concatenate(MapScalingTransform.INSTANCE);
164 double x = calcPoint.getX();
165 double y = calcPoint.getY();
166 double scaleRecip = DistrictNetworkNodeUtils.calculateScaleRecip(at);
167 double widthh = width * scaleRecip * nodeSize;
168 double heighth = height * scaleRecip * nodeSize;
170 rect = new Rectangle2D.Double();
171 rect.setRect(x - widthh/2, y - heighth/2, widthh, heighth);
175 public void setVertex(DistrictNetworkVertex vertex) {
176 this.vertex = vertex;
181 public boolean hover(boolean hover, boolean isConnectionTool) {
182 // Only react to hover when the connection tool is active
183 boolean doHover = hover && isConnectionTool;
184 boolean changed = this.hover != doHover;
185 this.hover = doHover;
187 for (IG2DNode child : getNodes()) {
188 if (child instanceof HoverSensitiveNode)
189 changed = ((HoverSensitiveNode)child).hover(hover, isConnectionTool) || changed;
196 public void setMousePosition(Point2D p) {
197 for (IG2DNode child : getNodes()) {
198 if (child instanceof HoverSensitiveNode)
199 ((HoverSensitiveNode) child).setMousePosition(p);
203 public void setColor(Color color) {
207 public Color getColor() {
211 @PropertySetter(value = "SVG")
212 public void setSVG(String value) {
213 for (INode nn : this.getNodes())
214 if (nn instanceof SVGNode)
215 ((SVGNode)nn).setData(value);
219 @PropertySetter(value = "size")
220 public void setSize(Double size) {
221 boolean changed = false;
223 changed = size != this.nodeSize;
224 this.nodeSize = size;
226 changed = this.nodeSize != 1.0;
233 @PropertySetter(value = "dynamicColor")
234 public void setDynamicColor(Color color) {
235 this.dynamicColor = color;
238 @PropertySetter(value = "hidden")
239 public void setHidden(Boolean value) {