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.IG2DNode;
18 import org.simantics.scenegraph.g2d.nodes.SVGNode;
19 import org.simantics.scenegraph.utils.GeometryUtils;
20 import org.simantics.scenegraph.utils.NodeUtil;
22 public class DistrictNetworkVertexNode extends G2DParentNode implements ISelectionPainterNode, HoverSensitiveNode {
24 //private static final Logger LOGGER = LoggerFactory.getLogger(DistrictNetworkVertexNode.class);
26 private static final long serialVersionUID = -2641639101400236719L;
28 private static final double left = -15;
29 private static final double top = left;
30 public static final double width = 30;
31 private static final double height = width;
33 private static final BasicStroke STROKE = new BasicStroke((float)width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
34 private static final Color SELECTION_COLOR = new Color(255, 0, 255, 96);
36 private static final Rectangle2D NORMAL = new Rectangle2D.Double(left, top, width, height);
37 private static final Rectangle2D HOVERED = new Rectangle2D.Double(left * 2, top * 2, width * 2, height * 2);
39 private DistrictNetworkVertex vertex;
41 private boolean scaleStroke = true;
42 private boolean hover;
45 private transient Color dynamicColor;
47 private Rectangle2D bounds;
48 private transient Point2D point;
49 private transient Rectangle2D rect;
50 private transient AffineTransform symbolTransform;
52 private double nodeSize = 1.0;
54 private boolean hidden = false;
62 public void render(Graphics2D g2d) {
63 AffineTransform ot = null;
64 AffineTransform t = getTransform();
65 if (t != null && !t.isIdentity()) {
66 ot = g2d.getTransform();
70 // Translate lat and lon to X and Y
71 Point2D p = point = DistrictNetworkNodeUtils.calculatePoint2D(vertex.getPoint(), point);
73 double viewScaleRecip = 1;
75 viewScaleRecip *= DistrictNetworkNodeUtils.calculateScaleRecip(g2d.getTransform());
78 if (!hidden && nodeSize > 0.0) {
79 Object oaaHint = null;
80 Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
81 if (aaHint != RenderingHints.VALUE_ANTIALIAS_OFF) {
83 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
86 Color oldColor = g2d.getColor();
87 Color newColor = dynamicColor != null ? dynamicColor : color;
88 boolean changeColor = !oldColor.equals(newColor);
90 double scaleRecip = viewScaleRecip * nodeSize;
92 Rectangle2D toDraw = rect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, hover ? HOVERED : NORMAL, rect, scaleRecip);
94 if (NodeUtil.isSelected(this, 1)) {
96 g2d.setColor(SELECTION_COLOR);
97 BasicStroke ss = GeometryUtils.scaleStroke(STROKE, (float)viewScaleRecip);
104 g2d.setColor(newColor);
109 g2d.setColor(oldColor);
111 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
115 for (INode nn : getNodes()) {
116 G2DNode g2dNode = (G2DNode)nn;
117 if (nn instanceof SVGNode) {
118 Rectangle2D toDraw = rect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, hover ? HOVERED : NORMAL, rect, viewScaleRecip);
119 symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(toDraw, symbolTransform);
120 g2dNode.setTransform(symbolTransform);
127 g2d.setTransform(ot);
131 public Rectangle2D getBounds() {
132 return super.getBounds();
136 public Rectangle2D getBoundsInLocal() {
141 private void updateBounds() {
142 Rectangle2D oldBounds = bounds;
143 if (oldBounds == null)
144 oldBounds = new Rectangle2D.Double();
145 bounds = calculateBounds(oldBounds);
149 public void setTransform(AffineTransform transform) {
150 super.setTransform(transform);
156 public AffineTransform getTransform() {
157 return super.getTransform();
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) {