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.Stroke;
8 import java.awt.geom.AffineTransform;
9 import java.awt.geom.Line2D;
10 import java.awt.geom.Path2D;
11 import java.awt.geom.Rectangle2D;
13 import org.simantics.district.network.ModelledCRS;
14 import org.simantics.district.network.ui.DistrictNetworkEdge;
15 import org.simantics.scenegraph.ISelectionPainterNode;
16 import org.simantics.scenegraph.g2d.G2DNode;
17 import org.simantics.scenegraph.utils.GeometryUtils;
18 import org.simantics.scenegraph.utils.NodeUtil;
20 public class DistrictNetworkEdgeNode extends G2DNode implements ISelectionPainterNode {
22 private static final long serialVersionUID = 8049769475036519806L;
24 private static final Stroke SELECTION_STROKE = new BasicStroke(1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);
25 private static final Color SELECTION_COLOR = new Color(255, 0, 255, 96);
27 private DistrictNetworkEdge edge;
28 private Rectangle2D bounds;
31 private static final BasicStroke STROKE = new BasicStroke(4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
32 private boolean scaleStroke = true;
36 private Double stroke;
43 public void render(Graphics2D g2d) {
44 AffineTransform ot = null;
45 AffineTransform t = getTransform();
46 if (t != null && !t.isIdentity()) {
47 ot = g2d.getTransform();
48 g2d.transform(getTransform());
51 Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
52 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
54 Color oldColor = g2d.getColor();
55 BasicStroke oldStroke = (BasicStroke) g2d.getStroke();
58 BasicStroke bs = null;
60 double scale = GeometryUtils.getScale(g2d.getTransform());
61 scale = Math.max(10000, Math.min(scale, 50000));
62 double str = stroke != null ? Math.abs(stroke) : 1.0;
63 bs = GeometryUtils.scaleStroke(STROKE, (float) (str / scale));
68 path = calculateLine(edge, path);
71 g2d.setColor(SELECTION_COLOR);
72 g2d.setStroke(GeometryUtils.scaleStroke(bs, 4f));
80 g2d.setStroke(oldStroke);
81 g2d.setColor(oldColor);
82 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
88 public static Line2D calculateLine(DistrictNetworkEdge edge, Line2D result) {
89 // Convert to screen coordinates
90 double startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX());
91 double startY = ModelledCRS.latitudeToY(-edge.getStartPoint().getY()); // Invert for Simantics
92 double endX = ModelledCRS.longitudeToX(edge.getEndPoint().getX());
93 double endY = ModelledCRS.latitudeToY(-edge.getEndPoint().getY());// Invert for Simantics
96 result = new Line2D.Double();
97 result.setLine(startX, startY, endX, endY);
101 public static Path2D calculatePath(DistrictNetworkEdge edge, Path2D result) {
102 // Convert to screen coordinates
103 double startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX());
104 double startY = ModelledCRS.latitudeToY(-edge.getStartPoint().getY()); // Invert for Simantics
105 double endX = ModelledCRS.longitudeToX(edge.getEndPoint().getX());
106 double endY = ModelledCRS.latitudeToY(-edge.getEndPoint().getY());// Invert for Simantics
108 if (result == null) {
109 result = new Path2D.Double();
113 result.moveTo(startX, startY);
114 result.lineTo(endX, endY);
118 private boolean isSelected() {
119 return NodeUtil.isSelected(this, 1);
123 public Rectangle2D getBoundsInLocal() {
127 private void updateBounds() {
128 Rectangle2D oldBounds = bounds;
129 if (oldBounds == null)
130 oldBounds = new Rectangle2D.Double();
131 bounds = calculateBounds(oldBounds);
134 private Rectangle2D calculateBounds(Rectangle2D rect) {
135 return calculatePath(edge, null).getBounds2D();
138 public void setDNEdge(DistrictNetworkEdge edge) {
143 public void setColor(Color color) {
147 public Color getColor() {
151 @PropertySetter(value = "stroke")
152 public void setStroke(Double stroke) {
153 this.stroke = stroke;