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.List;
11 import java.util.Optional;
13 import org.simantics.district.network.ui.adapters.DistrictNetworkVertex;
14 import org.simantics.maps.MapScalingTransform;
15 import org.simantics.scenegraph.INode;
16 import org.simantics.scenegraph.ISelectionPainterNode;
17 import org.simantics.scenegraph.g2d.G2DNode;
18 import org.simantics.scenegraph.g2d.G2DParentNode;
19 import org.simantics.scenegraph.g2d.G2DRenderingHints;
20 import org.simantics.scenegraph.g2d.IG2DNode;
21 import org.simantics.scenegraph.g2d.nodes.SVGNode;
22 import org.simantics.scenegraph.utils.GeometryUtils;
23 import org.simantics.scenegraph.utils.NodeUtil;
25 public class DistrictNetworkVertexNode extends G2DParentNode implements ISelectionPainterNode, HoverSensitiveNode {
27 //private static final Logger LOGGER = LoggerFactory.getLogger(DistrictNetworkVertexNode.class);
29 private static final long serialVersionUID = -2641639101400236719L;
31 private static final double left = -15;
32 private static final double top = left;
33 public static final double width = 30;
34 private static final double height = width;
36 private static final BasicStroke STROKE = new BasicStroke((float)width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
37 private static final Color SELECTION_COLOR = new Color(255, 0, 255, 96);
39 private static final Rectangle2D NORMAL = new Rectangle2D.Double(left, top, width, height);
40 private static final Rectangle2D HOVERED = new Rectangle2D.Double(left * 2, top * 2, width * 2, height * 2);
42 private DistrictNetworkVertex vertex;
44 private static boolean scaleStroke = true;
45 private boolean hover;
48 private transient Color dynamicColor;
49 private transient Color eventColor;
51 private Rectangle2D bounds;
52 private transient Point2D point;
53 private transient Rectangle2D rect;
54 private transient AffineTransform symbolTransform;
56 private double nodeSize = 1.0;
58 private boolean hidden = false;
66 public void render(Graphics2D g2d) {
67 AffineTransform ot = null;
68 AffineTransform t = getTransform();
69 double viewScaleRecip = scaleStroke ? (Double) g2d.getRenderingHint(DistrictRenderingHints.KEY_VIEW_SCALE_RECIPROCAL_UNDER_SPATIAL_ROOT) : 1.0;
70 if (t != null && !t.isIdentity()) {
71 //ot = g2d.getTransform();
72 ot = (AffineTransform) g2d.getRenderingHint(G2DRenderingHints.KEY_TRANSFORM_UNDER_SPATIAL_ROOT);
74 ot = g2d.getTransform();
78 AffineTransform work = DistrictNetworkNodeUtils.sharedTransform.get();
79 work.setTransform(ot);
81 viewScaleRecip = DistrictNetworkNodeUtils.calculateScaleRecip(work);
85 // Translate lat and lon to X and Y
86 Point2D p = point = DistrictNetworkNodeUtils.calculatePoint2D(vertex.getPoint(), point);
88 if (!hidden && nodeSize > 0.0) {
89 Object oaaHint = null;
90 Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
91 if (aaHint != RenderingHints.VALUE_ANTIALIAS_OFF) {
93 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
96 Color oldColor = g2d.getColor();
97 Color newColor = eventColor != null ? eventColor : dynamicColor != null ? dynamicColor : color;
98 boolean changeColor = !oldColor.equals(newColor);
100 double scaleRecip = viewScaleRecip * nodeSize;
102 Rectangle2D toDraw = rect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, hover ? HOVERED : NORMAL, rect, scaleRecip);
104 if (NodeUtil.isSelected(this, 1)) {
106 g2d.setColor(SELECTION_COLOR);
107 BasicStroke ss = GeometryUtils.scaleStroke(STROKE, (float)viewScaleRecip);
114 g2d.setColor(newColor);
119 g2d.setColor(oldColor);
121 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
125 for (INode nn : getNodes()) {
126 G2DNode g2dNode = (G2DNode)nn;
127 if (nn instanceof SVGNode) {
128 Rectangle2D toDraw = rect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, hover ? HOVERED : NORMAL, rect, viewScaleRecip);
129 symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(toDraw, symbolTransform);
130 g2dNode.setTransform(symbolTransform);
137 g2d.setTransform(ot);
141 public Rectangle2D getBounds() {
142 return super.getBounds();
146 public Rectangle2D getBoundsInLocal() {
151 private void updateBounds() {
152 Rectangle2D oldBounds = bounds;
153 if (oldBounds == null)
154 oldBounds = new Rectangle2D.Double();
155 bounds = calculateBounds(oldBounds);
159 public void setTransform(AffineTransform transform) {
160 super.setTransform(transform);
165 private Rectangle2D calculateBounds(Rectangle2D rect) {
166 Point2D calcPoint = DistrictNetworkNodeUtils.calculatePoint2D(vertex.getPoint(), point);
167 AffineTransform at = NodeUtil.getLocalToGlobalTransform(this);
168 at.concatenate(MapScalingTransform.INSTANCE);
169 double x = calcPoint.getX();
170 double y = calcPoint.getY();
171 double scaleRecip = DistrictNetworkNodeUtils.calculateScaleRecip(at);
172 double widthh = width * scaleRecip * nodeSize;
173 double heighth = height * scaleRecip * nodeSize;
175 rect = new Rectangle2D.Double();
176 rect.setRect(x - widthh/2, y - heighth/2, widthh, heighth);
180 public void setVertex(DistrictNetworkVertex vertex) {
181 this.vertex = vertex;
185 public DistrictNetworkVertex getVertex() {
190 public boolean hover(boolean hover, boolean isConnectionTool) {
191 // Only react to hover when the connection tool is active
192 boolean doHover = hover && isConnectionTool;
193 boolean changed = this.hover != doHover;
194 this.hover = doHover;
196 for (IG2DNode child : getNodes()) {
197 if (child instanceof HoverSensitiveNode)
198 changed = ((HoverSensitiveNode)child).hover(hover, isConnectionTool) || changed;
205 public void setMousePosition(Point2D p) {
206 for (IG2DNode child : getNodes()) {
207 if (child instanceof HoverSensitiveNode)
208 ((HoverSensitiveNode) child).setMousePosition(p);
212 public void setColor(Color color) {
216 public Color getColor() {
220 @PropertySetter(value = "SVG")
221 public void setSVG(String value) {
222 for (INode nn : this.getNodes())
223 if (nn instanceof SVGNode)
224 ((SVGNode)nn).setData(value);
228 @PropertySetter(value = "size")
229 public void setSize(Double size) {
230 boolean changed = false;
232 changed = size != this.nodeSize;
233 this.nodeSize = size;
235 changed = this.nodeSize != 1.0;
242 @PropertySetter(value = "dynamicColor")
243 public void setDynamicColor(Color color) {
244 this.dynamicColor = color;
247 @PropertySetter(value = "eventColor")
248 public void setEventColor(Color colorr) {
249 this.eventColor = colorr;
252 @PropertySetter(value = "hidden")
253 public void setHidden(Boolean value) {
257 public void setStaticInformation(Optional<String> staticInformation) {
258 DistrictNetworkStaticInfoNode child = getOrCreateNode(DistrictNetworkStaticInfoNode.NODE_KEY, DistrictNetworkStaticInfoNode.class);
259 Point2D calculatePoint2D = DistrictNetworkNodeUtils.calculatePoint2D(vertex.getPoint(), null);
260 child.setLocation(calculatePoint2D, new Point2D.Double(1.0, 0.0));
261 if (staticInformation.isPresent()) {
262 child.setInfo(staticInformation.get());
268 public void setInSimulation(Optional<Boolean> isInSimulation) {
269 if (!isInSimulation.isPresent()) {
270 removeNode(NotInSimulationNode.NODE_NAME);
272 NotInSimulationNode child = getOrCreateNode(NotInSimulationNode.NODE_NAME, NotInSimulationNode.class);
273 child.setZIndex(1000);
274 child.setIsInSimulation(isInSimulation.get());
278 public void setConnectionLinePoints(List<Point2D> points) {
279 if (points == null) {
280 removeNode(ConnectionLineNode.NODE_NAME);
282 ConnectionLineNode child = getOrCreateNode(ConnectionLineNode.NODE_NAME, ConnectionLineNode.class);
284 child.setStrokeWidth(2.f);
285 child.setPoints(points);