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;
10 import java.util.Optional;
12 import org.simantics.district.network.ui.adapters.DistrictNetworkVertex;
13 import org.simantics.maps.MapScalingTransform;
14 import org.simantics.scenegraph.INode;
15 import org.simantics.scenegraph.ISelectionPainterNode;
16 import org.simantics.scenegraph.g2d.G2DNode;
17 import org.simantics.scenegraph.g2d.G2DParentNode;
18 import org.simantics.scenegraph.g2d.G2DRenderingHints;
19 import org.simantics.scenegraph.g2d.IG2DNode;
20 import org.simantics.scenegraph.g2d.nodes.SVGNode;
21 import org.simantics.scenegraph.utils.GeometryUtils;
22 import org.simantics.scenegraph.utils.NodeUtil;
24 public class DistrictNetworkVertexNode extends G2DParentNode implements ISelectionPainterNode, HoverSensitiveNode {
26 //private static final Logger LOGGER = LoggerFactory.getLogger(DistrictNetworkVertexNode.class);
28 private static final long serialVersionUID = -2641639101400236719L;
30 private static final double left = -15;
31 private static final double top = left;
32 public static final double width = 30;
33 private static final double height = width;
35 private static final BasicStroke STROKE = new BasicStroke((float)width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
36 private static final Color SELECTION_COLOR = new Color(255, 0, 255, 96);
38 private static final Rectangle2D NORMAL = new Rectangle2D.Double(left, top, width, height);
39 private static final Rectangle2D HOVERED = new Rectangle2D.Double(left * 2, top * 2, width * 2, height * 2);
41 private DistrictNetworkVertex vertex;
43 private static boolean scaleStroke = true;
44 private boolean hover;
47 private transient Color dynamicColor;
49 private Rectangle2D bounds;
50 private transient Point2D point;
51 private transient Rectangle2D rect;
52 private transient AffineTransform symbolTransform;
54 private double nodeSize = 1.0;
56 private boolean hidden = false;
64 public void render(Graphics2D g2d) {
65 AffineTransform ot = null;
66 AffineTransform t = getTransform();
67 double viewScaleRecip = scaleStroke ? (Double) g2d.getRenderingHint(DistrictRenderingHints.KEY_VIEW_SCALE_RECIPROCAL_UNDER_SPATIAL_ROOT) : 1.0;
68 if (t != null && !t.isIdentity()) {
69 //ot = g2d.getTransform();
70 ot = (AffineTransform) g2d.getRenderingHint(G2DRenderingHints.KEY_TRANSFORM_UNDER_SPATIAL_ROOT);
72 ot = g2d.getTransform();
76 AffineTransform work = DistrictNetworkNodeUtils.sharedTransform.get();
77 work.setTransform(ot);
79 viewScaleRecip = DistrictNetworkNodeUtils.calculateScaleRecip(work);
83 // Translate lat and lon to X and Y
84 Point2D p = point = DistrictNetworkNodeUtils.calculatePoint2D(vertex.getPoint(), point);
86 if (!hidden && nodeSize > 0.0) {
87 Object oaaHint = null;
88 Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
89 if (aaHint != RenderingHints.VALUE_ANTIALIAS_OFF) {
91 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
94 Color oldColor = g2d.getColor();
95 Color newColor = dynamicColor != null ? dynamicColor : color;
96 boolean changeColor = !oldColor.equals(newColor);
98 double scaleRecip = viewScaleRecip * nodeSize;
100 Rectangle2D toDraw = rect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, hover ? HOVERED : NORMAL, rect, scaleRecip);
102 if (NodeUtil.isSelected(this, 1)) {
104 g2d.setColor(SELECTION_COLOR);
105 BasicStroke ss = GeometryUtils.scaleStroke(STROKE, (float)viewScaleRecip);
112 g2d.setColor(newColor);
117 g2d.setColor(oldColor);
119 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
123 for (INode nn : getNodes()) {
124 G2DNode g2dNode = (G2DNode)nn;
125 if (nn instanceof SVGNode) {
126 Rectangle2D toDraw = rect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, hover ? HOVERED : NORMAL, rect, viewScaleRecip);
127 symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(toDraw, symbolTransform);
128 g2dNode.setTransform(symbolTransform);
135 g2d.setTransform(ot);
139 public Rectangle2D getBounds() {
140 return super.getBounds();
144 public Rectangle2D getBoundsInLocal() {
149 private void updateBounds() {
150 Rectangle2D oldBounds = bounds;
151 if (oldBounds == null)
152 oldBounds = new Rectangle2D.Double();
153 bounds = calculateBounds(oldBounds);
157 public void setTransform(AffineTransform transform) {
158 super.setTransform(transform);
163 private Rectangle2D calculateBounds(Rectangle2D rect) {
164 Point2D calcPoint = DistrictNetworkNodeUtils.calculatePoint2D(vertex.getPoint(), point);
165 AffineTransform at = NodeUtil.getLocalToGlobalTransform(this);
166 at.concatenate(MapScalingTransform.INSTANCE);
167 double x = calcPoint.getX();
168 double y = calcPoint.getY();
169 double scaleRecip = DistrictNetworkNodeUtils.calculateScaleRecip(at);
170 double widthh = width * scaleRecip * nodeSize;
171 double heighth = height * scaleRecip * nodeSize;
173 rect = new Rectangle2D.Double();
174 rect.setRect(x - widthh/2, y - heighth/2, widthh, heighth);
178 public void setVertex(DistrictNetworkVertex vertex) {
179 this.vertex = vertex;
183 public DistrictNetworkVertex getVertex() {
188 public boolean hover(boolean hover, boolean isConnectionTool) {
189 // Only react to hover when the connection tool is active
190 boolean doHover = hover && isConnectionTool;
191 boolean changed = this.hover != doHover;
192 this.hover = doHover;
194 for (IG2DNode child : getNodes()) {
195 if (child instanceof HoverSensitiveNode)
196 changed = ((HoverSensitiveNode)child).hover(hover, isConnectionTool) || changed;
203 public void setMousePosition(Point2D p) {
204 for (IG2DNode child : getNodes()) {
205 if (child instanceof HoverSensitiveNode)
206 ((HoverSensitiveNode) child).setMousePosition(p);
210 public void setColor(Color color) {
214 public Color getColor() {
218 @PropertySetter(value = "SVG")
219 public void setSVG(String value) {
220 for (INode nn : this.getNodes())
221 if (nn instanceof SVGNode)
222 ((SVGNode)nn).setData(value);
226 @PropertySetter(value = "size")
227 public void setSize(Double size) {
228 boolean changed = false;
230 changed = size != this.nodeSize;
231 this.nodeSize = size;
233 changed = this.nodeSize != 1.0;
240 @PropertySetter(value = "dynamicColor")
241 public void setDynamicColor(Color color) {
242 this.dynamicColor = color;
245 @PropertySetter(value = "hidden")
246 public void setHidden(Boolean value) {
250 public void setStaticInformation(Optional<String> staticInformation) {
251 DistrictNetworkStaticInfoNode child = getOrCreateNode(DistrictNetworkStaticInfoNode.NODE_KEY, DistrictNetworkStaticInfoNode.class);
252 Point2D calculatePoint2D = DistrictNetworkNodeUtils.calculatePoint2D(vertex.getPoint(), null);
253 child.setLocation(calculatePoint2D, new Point2D.Double(1.0, 0.0));
254 if (staticInformation.isPresent()) {
255 child.setInfo(staticInformation.get());
261 public void setInSimulation(Optional<Boolean> isInSimulation) {
262 if (!isInSimulation.isPresent()) {
263 removeNode(NotInSimulationNode.NODE_NAME);
265 NotInSimulationNode child = getOrCreateNode(NotInSimulationNode.NODE_NAME, NotInSimulationNode.class);
266 child.setZIndex(1000);
267 child.setIsInSimulation(isInSimulation.get());