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;
60 public void render(Graphics2D g2d) {
64 AffineTransform ot = null;
65 AffineTransform t = getTransform();
66 if (t != null && !t.isIdentity()) {
67 ot = g2d.getTransform();
71 Object oaaHint = null;
72 Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
73 if (aaHint != RenderingHints.VALUE_ANTIALIAS_OFF) {
75 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
78 Color oldColor = g2d.getColor();
79 Color newColor = dynamicColor != null ? dynamicColor : color;
80 boolean changeColor = !oldColor.equals(newColor);
82 double viewScaleRecip = 1;
84 viewScaleRecip *= DistrictNetworkNodeUtils.calculateScaleRecip(g2d.getTransform());
86 double scaleRecip = viewScaleRecip * nodeSize;
88 // Translate lat and lon to X and Y
89 Point2D p = point = DistrictNetworkNodeUtils.calculatePoint2D(vertex.getPoint(), point);
90 Rectangle2D toDraw = rect = DistrictNetworkNodeUtils.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);
102 g2d.setColor(newColor);
107 g2d.setColor(oldColor);
109 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
112 for (INode nn : getNodes()) {
113 G2DNode g2dNode = (G2DNode)nn;
114 if (nn instanceof SVGNode) {
115 symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(toDraw, symbolTransform);
116 g2dNode.setTransform(symbolTransform);
123 g2d.setTransform(ot);
127 public Rectangle2D getBounds() {
128 return super.getBounds();
132 public Rectangle2D getBoundsInLocal() {
137 private void updateBounds() {
138 Rectangle2D oldBounds = bounds;
139 if (oldBounds == null)
140 oldBounds = new Rectangle2D.Double();
141 bounds = calculateBounds(oldBounds);
145 public void setTransform(AffineTransform transform) {
146 super.setTransform(transform);
152 public AffineTransform getTransform() {
153 return super.getTransform();
156 private Rectangle2D calculateBounds(Rectangle2D rect) {
157 Point2D calcPoint = DistrictNetworkNodeUtils.calculatePoint2D(vertex.getPoint(), point);
158 AffineTransform at = NodeUtil.getLocalToGlobalTransform(this);
159 at.concatenate(MapScalingTransform.INSTANCE);
160 double x = calcPoint.getX();
161 double y = calcPoint.getY();
162 double scaleRecip = DistrictNetworkNodeUtils.calculateScaleRecip(at);
163 double widthh = width * scaleRecip * nodeSize;
164 double heighth = height * scaleRecip * nodeSize;
166 rect = new Rectangle2D.Double();
167 rect.setRect(x - widthh/2, y - heighth/2, widthh, heighth);
171 public void setVertex(DistrictNetworkVertex vertex) {
172 this.vertex = vertex;
177 public boolean hover(boolean hover, boolean isConnectionTool) {
178 // Only react to hover when the connection tool is active
179 boolean doHover = hover && isConnectionTool;
180 boolean changed = this.hover != doHover;
181 this.hover = doHover;
183 for (IG2DNode child : getNodes()) {
184 if (child instanceof HoverSensitiveNode)
185 changed = ((HoverSensitiveNode)child).hover(hover, isConnectionTool) || changed;
192 public void setMousePosition(Point2D p) {
193 for (IG2DNode child : getNodes()) {
194 if (child instanceof HoverSensitiveNode)
195 ((HoverSensitiveNode) child).setMousePosition(p);
199 public void setColor(Color color) {
203 public Color getColor() {
207 @PropertySetter(value = "SVG")
208 public void setSVG(String value) {
209 for (INode nn : this.getNodes())
210 if (nn instanceof SVGNode)
211 ((SVGNode)nn).setData(value);
215 @PropertySetter(value = "size")
216 public void setSize(Double size) {
217 boolean changed = false;
219 changed = size != this.nodeSize;
220 this.nodeSize = size;
222 changed = this.nodeSize != 1.0;
229 @PropertySetter(value = "dynamicColor")
230 public void setDynamicColor(Color color) {
231 this.dynamicColor = color;