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.scenegraph.INode;
13 import org.simantics.scenegraph.ISelectionPainterNode;
14 import org.simantics.scenegraph.g2d.G2DNode;
15 import org.simantics.scenegraph.g2d.G2DParentNode;
16 import org.simantics.scenegraph.g2d.nodes.SVGNode;
17 import org.simantics.scenegraph.utils.GeometryUtils;
18 import org.simantics.scenegraph.utils.NodeUtil;
20 public class DistrictNetworkVertexNode extends G2DParentNode implements ISelectionPainterNode {
22 //private static final Logger LOGGER = LoggerFactory.getLogger(DistrictNetworkVertexNode.class);
24 private static final long serialVersionUID = -2641639101400236719L;
26 private static final double left = -0.00005;
27 private static final double top = left;
28 public static final double width = 0.0001;
29 private static final double height = width;
31 private static final BasicStroke STROKE = new BasicStroke((float)width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
32 private static final Color SELECTION_COLOR = new Color(255, 0, 255, 96);
34 private static final Rectangle2D NORMAL = new Rectangle2D.Double(left, top, width, height);
35 private static final Rectangle2D HOVERED = new Rectangle2D.Double(left * 3, top * 3, width * 3, height * 3);
37 private DistrictNetworkVertex vertex;
39 private boolean scaleStroke = true;
40 private boolean hover;
43 private transient Color dynamicColor;
45 private Rectangle2D bounds;
46 private transient Point2D point;
47 private transient Rectangle2D rect;
48 private transient AffineTransform symbolTransform;
50 private double nodeSize = 1;
58 public void render(Graphics2D g2d) {
62 AffineTransform ot = null;
63 AffineTransform t = getTransform();
64 if (t != null && !t.isIdentity()) {
65 ot = g2d.getTransform();
69 Object oaaHint = null;
70 Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
71 if (aaHint != RenderingHints.VALUE_ANTIALIAS_OFF) {
73 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
76 Color oldColor = g2d.getColor();
77 Color newColor = dynamicColor != null ? dynamicColor : color;
78 boolean changeColor = !oldColor.equals(newColor);
80 double viewScaleRecip = 1;
82 viewScaleRecip *= DistrictNetworkNodeUtils.calculateScaleRecip(g2d.getTransform());
84 double scaleRecip = viewScaleRecip * nodeSize;
86 // Translate lat and lon to X and Y
87 Point2D p = point = DistrictNetworkNodeUtils.calculatePoint2D(vertex.getPoint(), point);
88 Rectangle2D toDraw = rect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, hover ? HOVERED : NORMAL, rect, scaleRecip);
90 if (NodeUtil.isSelected(this, 1)) {
92 g2d.setColor(SELECTION_COLOR);
93 BasicStroke ss = GeometryUtils.scaleStroke(STROKE, (float) (viewScaleRecip*0.5));
100 g2d.setColor(newColor);
105 g2d.setColor(oldColor);
107 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
110 for (INode nn : getNodes()) {
111 G2DNode g2dNode = (G2DNode)nn;
112 symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(toDraw, symbolTransform);
113 g2dNode.setTransform(symbolTransform);
118 g2d.setTransform(ot);
122 public Rectangle2D getBounds() {
123 return super.getBounds();
127 public Rectangle2D getBoundsInLocal() {
131 private void updateBounds() {
132 Rectangle2D oldBounds = bounds;
133 if (oldBounds == null)
134 oldBounds = new Rectangle2D.Double();
135 bounds = calculateBounds(oldBounds);
139 public void setTransform(AffineTransform transform) {
140 super.setTransform(transform);
146 public AffineTransform getTransform() {
147 return super.getTransform();
150 private Rectangle2D calculateBounds(Rectangle2D rect) {
151 Point2D calcPoint = DistrictNetworkNodeUtils.calculatePoint2D(vertex.getPoint(), point);
152 AffineTransform at = getTransform();
153 double x = calcPoint.getX();
154 double y = calcPoint.getY();
155 double widthh = width / at.getScaleX();
156 double heighth = height / at.getScaleY();
157 return new Rectangle2D.Double(x - widthh, y - heighth, widthh * 2, heighth * 2).getBounds2D();
160 public void setVertex(DistrictNetworkVertex vertex) {
161 this.vertex = vertex;
165 public boolean hover(boolean hover) {
166 // if (hover && LOGGER.isDebugEnabled())
167 // LOGGER.debug("Hovering " + this);
168 boolean changed = false;
169 if (this.hover != hover) {
176 public void setColor(Color color) {
180 public Color getColor() {
184 @PropertySetter(value = "SVG")
185 public void setSVG(String value) {
186 for (INode nn : this.getNodes())
187 if (nn instanceof SVGNode)
188 ((SVGNode)nn).setData(value);
192 @PropertySetter(value = "size")
193 public void setSize(Double size) {
194 boolean changed = false;
196 changed = size != this.nodeSize;
197 this.nodeSize = size;
199 changed = this.nodeSize != 1.0;
206 @PropertySetter(value = "dynamicColor")
207 public void setDynamicColor(Color color) {
208 this.dynamicColor = color;