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;
50 private Rectangle2D bounds;
51 private transient Point2D point;
52 private transient Rectangle2D rect;
53 private transient AffineTransform symbolTransform;
55 private double nodeSize = 1.0;
57 private boolean hidden = false;
65 public void render(Graphics2D g2d) {
66 AffineTransform ot = null;
67 AffineTransform t = getTransform();
68 double viewScaleRecip = scaleStroke ? (Double) g2d.getRenderingHint(DistrictRenderingHints.KEY_VIEW_SCALE_RECIPROCAL_UNDER_SPATIAL_ROOT) : 1.0;
69 if (t != null && !t.isIdentity()) {
70 //ot = g2d.getTransform();
71 ot = (AffineTransform) g2d.getRenderingHint(G2DRenderingHints.KEY_TRANSFORM_UNDER_SPATIAL_ROOT);
73 ot = g2d.getTransform();
77 AffineTransform work = DistrictNetworkNodeUtils.sharedTransform.get();
78 work.setTransform(ot);
80 viewScaleRecip = DistrictNetworkNodeUtils.calculateScaleRecip(work);
84 // Translate lat and lon to X and Y
85 Point2D p = point = DistrictNetworkNodeUtils.calculatePoint2D(vertex.getPoint(), point);
87 if (!hidden && nodeSize > 0.0) {
88 Object oaaHint = null;
89 Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
90 if (aaHint != RenderingHints.VALUE_ANTIALIAS_OFF) {
92 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
95 Color oldColor = g2d.getColor();
96 Color newColor = dynamicColor != null ? dynamicColor : color;
97 boolean changeColor = !oldColor.equals(newColor);
99 double scaleRecip = viewScaleRecip * nodeSize;
101 Rectangle2D toDraw = rect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, hover ? HOVERED : NORMAL, rect, scaleRecip);
103 if (NodeUtil.isSelected(this, 1)) {
105 g2d.setColor(SELECTION_COLOR);
106 BasicStroke ss = GeometryUtils.scaleStroke(STROKE, (float)viewScaleRecip);
113 g2d.setColor(newColor);
118 g2d.setColor(oldColor);
120 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
124 for (INode nn : getNodes()) {
125 G2DNode g2dNode = (G2DNode)nn;
126 if (nn instanceof SVGNode) {
127 Rectangle2D toDraw = rect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, hover ? HOVERED : NORMAL, rect, viewScaleRecip);
128 symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(toDraw, symbolTransform);
129 g2dNode.setTransform(symbolTransform);
136 g2d.setTransform(ot);
140 public Rectangle2D getBounds() {
141 return super.getBounds();
145 public Rectangle2D getBoundsInLocal() {
150 private void updateBounds() {
151 Rectangle2D oldBounds = bounds;
152 if (oldBounds == null)
153 oldBounds = new Rectangle2D.Double();
154 bounds = calculateBounds(oldBounds);
158 public void setTransform(AffineTransform transform) {
159 super.setTransform(transform);
164 private Rectangle2D calculateBounds(Rectangle2D rect) {
165 Point2D calcPoint = DistrictNetworkNodeUtils.calculatePoint2D(vertex.getPoint(), point);
166 AffineTransform at = NodeUtil.getLocalToGlobalTransform(this);
167 at.concatenate(MapScalingTransform.INSTANCE);
168 double x = calcPoint.getX();
169 double y = calcPoint.getY();
170 double scaleRecip = DistrictNetworkNodeUtils.calculateScaleRecip(at);
171 double widthh = width * scaleRecip * nodeSize;
172 double heighth = height * scaleRecip * nodeSize;
174 rect = new Rectangle2D.Double();
175 rect.setRect(x - widthh/2, y - heighth/2, widthh, heighth);
179 public void setVertex(DistrictNetworkVertex vertex) {
180 this.vertex = vertex;
184 public DistrictNetworkVertex getVertex() {
189 public boolean hover(boolean hover, boolean isConnectionTool) {
190 // Only react to hover when the connection tool is active
191 boolean doHover = hover && isConnectionTool;
192 boolean changed = this.hover != doHover;
193 this.hover = doHover;
195 for (IG2DNode child : getNodes()) {
196 if (child instanceof HoverSensitiveNode)
197 changed = ((HoverSensitiveNode)child).hover(hover, isConnectionTool) || changed;
204 public void setMousePosition(Point2D p) {
205 for (IG2DNode child : getNodes()) {
206 if (child instanceof HoverSensitiveNode)
207 ((HoverSensitiveNode) child).setMousePosition(p);
211 public void setColor(Color color) {
215 public Color getColor() {
219 @PropertySetter(value = "SVG")
220 public void setSVG(String value) {
221 for (INode nn : this.getNodes())
222 if (nn instanceof SVGNode)
223 ((SVGNode)nn).setData(value);
227 @PropertySetter(value = "size")
228 public void setSize(Double size) {
229 boolean changed = false;
231 changed = size != this.nodeSize;
232 this.nodeSize = size;
234 changed = this.nodeSize != 1.0;
241 @PropertySetter(value = "dynamicColor")
242 public void setDynamicColor(Color color) {
243 this.dynamicColor = color;
246 @PropertySetter(value = "hidden")
247 public void setHidden(Boolean value) {
251 public void setStaticInformation(Optional<String> staticInformation) {
252 DistrictNetworkStaticInfoNode child = getOrCreateNode(DistrictNetworkStaticInfoNode.NODE_KEY, DistrictNetworkStaticInfoNode.class);
253 Point2D calculatePoint2D = DistrictNetworkNodeUtils.calculatePoint2D(vertex.getPoint(), null);
254 child.setLocation(calculatePoint2D, new Point2D.Double(1.0, 0.0));
255 if (staticInformation.isPresent()) {
256 child.setInfo(staticInformation.get());
262 public void setInSimulation(Optional<Boolean> isInSimulation) {
263 if (!isInSimulation.isPresent()) {
264 removeNode(NotInSimulationNode.NODE_NAME);
266 NotInSimulationNode child = getOrCreateNode(NotInSimulationNode.NODE_NAME, NotInSimulationNode.class);
267 child.setZIndex(1000);
268 child.setIsInSimulation(isInSimulation.get());
272 public void setConnectionLinePoints(List<Point2D> points) {
273 if (points == null) {
274 removeNode(ConnectionLineNode.NODE_NAME);
276 ConnectionLineNode child = getOrCreateNode(ConnectionLineNode.NODE_NAME, ConnectionLineNode.class);
278 child.setStrokeWidth(2.f);
279 child.setPoints(points);