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.ModelledCRS;
12 import org.simantics.district.network.ui.adapters.DistrictNetworkVertex;
13 import org.simantics.scenegraph.ISelectionPainterNode;
14 import org.simantics.scenegraph.g2d.G2DNode;
15 import org.simantics.scenegraph.utils.GeometryUtils;
16 import org.simantics.scenegraph.utils.NodeUtil;
18 public class DistrictNetworkVertexNode extends G2DNode implements ISelectionPainterNode {
20 //private static final Logger LOGGER = LoggerFactory.getLogger(DistrictNetworkVertexNode.class);
22 private static final long serialVersionUID = -2641639101400236719L;
24 private static final BasicStroke STROKE = new BasicStroke(4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
25 private static final Color SELECTION_COLOR = new Color(255, 0, 255, 96);
27 private static final double left = -0.25;
28 private static final double top = -0.25;
29 private static final double width = 0.5;
30 private static final double height = 0.5;
32 private static final Rectangle2D NORMAL = new Rectangle2D.Double(left, top, width, height);
33 private static final Rectangle2D HOVERED = new Rectangle2D.Double(left * 3, top * 3, width * 3, height * 3);
35 private DistrictNetworkVertex vertex;
37 private boolean scaleStroke = true;
38 private boolean hover;
41 private transient Color dynamicColor;
43 private Rectangle2D bounds;
45 private double nodeSize = 1;
53 public void render(Graphics2D g2d) {
57 AffineTransform ot = null;
58 AffineTransform t = getTransform();
59 if (t != null && !t.isIdentity()) {
60 ot = g2d.getTransform();
61 g2d.transform(getTransform());
64 Object oaaHint = null;
65 Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
66 if (aaHint != RenderingHints.VALUE_ANTIALIAS_OFF) {
68 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
71 Color oldColor = g2d.getColor();
73 double scaleRecip = 1;
75 double scale = GeometryUtils.getScale(g2d.getTransform());
76 scale = Math.max(10000, Math.min(scale, 50000));
77 scaleRecip = 1.0 / scale;
79 scaleRecip = scaleRecip * nodeSize;
81 // Translate lat and lon to X and Y
82 Point2D res = calculatePoint2D(vertex);
86 toDraw = new Rectangle2D.Double(res.getX() - (HOVERED.getWidth() / 2 * scaleRecip), res.getY() - (HOVERED.getHeight() / 2 * scaleRecip), HOVERED.getWidth() * scaleRecip, HOVERED.getHeight() * scaleRecip);
88 toDraw = new Rectangle2D.Double(res.getX() - (NORMAL.getWidth() / 2 * scaleRecip), res.getY() - (NORMAL.getHeight() / 2 * scaleRecip), NORMAL.getWidth() * scaleRecip, NORMAL.getHeight() * scaleRecip);
91 if (NodeUtil.isSelected(this, 1)) {
92 g2d.setColor(SELECTION_COLOR);
93 BasicStroke ss = GeometryUtils.scaleStroke(STROKE, (float) scaleRecip*2);
99 g2d.setColor(dynamicColor != null ? dynamicColor : color);
103 g2d.setColor(oldColor);
105 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
107 g2d.setTransform(ot);
111 public Rectangle2D getBounds() {
112 return super.getBounds();
116 public Rectangle2D getBoundsInLocal() {
120 private void updateBounds() {
121 Rectangle2D oldBounds = bounds;
122 if (oldBounds == null)
123 oldBounds = new Rectangle2D.Double();
124 bounds = calculateBounds(oldBounds);
128 public void setTransform(AffineTransform transform) {
129 super.setTransform(transform);
135 public AffineTransform getTransform() {
136 return super.getTransform();
139 private Rectangle2D calculateBounds(Rectangle2D rect) {
140 Point2D calcPoint = calculatePoint2D(vertex);
141 AffineTransform at = getTransform();
142 return new Rectangle2D.Double(calcPoint.getX(), calcPoint.getY(), width / at.getScaleX(), height / at.getScaleY()).getBounds2D();
145 private static Point2D calculatePoint2D(DistrictNetworkVertex vertex) {
146 Point2D point= vertex.getPoint();
147 double x = ModelledCRS.longitudeToX(point.getX());
148 double y = ModelledCRS.latitudeToY(-point.getY()); // Inverse because Simantics Diagram is inverted
151 Point2D res = new Point2D.Double(x, y);
155 public void setVertex(DistrictNetworkVertex vertex) {
156 this.vertex = vertex;
160 public boolean hover(boolean hover) {
161 // if (hover && LOGGER.isDebugEnabled())
162 // LOGGER.debug("Hovering " + this);
163 boolean changed = false;
164 if (this.hover != hover) {
171 public void setColor(Color color) {
175 public Color getColor() {
179 @PropertySetter(value = "size")
180 public void setSize(Double size) {
181 boolean changed = false;
183 changed = size != this.nodeSize;
184 this.nodeSize = size;
186 changed = this.nodeSize != 1.0;
193 @PropertySetter(value = "dynamicColor")
194 public void setDynamicColor(Color color) {
195 this.dynamicColor = color;