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.Path2D;
10 import java.awt.geom.Rectangle2D;
12 import org.simantics.district.network.ModelledCRS;
13 import org.simantics.district.network.ui.DistrictNetworkEdge;
14 import org.simantics.scenegraph.g2d.G2DNode;
15 import org.simantics.scenegraph.utils.GeometryUtils;
16 import org.simantics.scenegraph.utils.NodeUtil;
18 public class DistrictNetworkEdgeNode extends G2DNode {
20 private static final long serialVersionUID = 8049769475036519806L;
22 private static final Stroke SELECTION_STROKE = new BasicStroke(1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);
23 private static final Color SELECTION_COLOR = new Color(255, 0, 255, 96);
25 private DistrictNetworkEdge edge;
26 private Rectangle2D bounds;
28 private static final Stroke STROKE = new BasicStroke(4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
29 private boolean scaleStroke = true;
33 private Double stroke;
40 public void render(Graphics2D g2d) {
42 AffineTransform ot = null;
43 AffineTransform t = getTransform();
44 if (t != null && !t.isIdentity()) {
45 ot = g2d.getTransform();
46 g2d.transform(getTransform());
49 Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
50 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
52 Color oldColor = g2d.getColor();
53 BasicStroke oldStroke = (BasicStroke) g2d.getStroke();
54 // boolean selected = isSelected();
56 // Path2D selectionPath = edge.getPath();
57 // Shape selectionShape = SELECTION_STROKE.createStrokedShape(selectionPath);
58 // g2d.setColor(SELECTION_COLOR);
59 // g2d.fill(selectionShape);
64 if (scaleStroke && STROKE instanceof BasicStroke) {
67 str = Math.abs(stroke);
70 BasicStroke bs = GeometryUtils.scaleStroke(STROKE, (float) (str / GeometryUtils.getScale(g2d.getTransform())));
73 g2d.setStroke(STROKE);
77 g2d.draw(calculatePath(edge));
80 g2d.setStroke(oldStroke);
81 g2d.setColor(oldColor);
82 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
88 public static Path2D calculatePath(DistrictNetworkEdge edge) {
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 Path2D path = new Path2D.Double();
97 path.moveTo(startX, startY);
98 path.lineTo(endX, endY);
102 private boolean isSelected() {
103 return NodeUtil.isSelected(this, 1);
107 public Rectangle2D getBoundsInLocal() {
111 private void updateBounds() {
112 Rectangle2D oldBounds = bounds;
113 if (oldBounds == null)
114 oldBounds = new Rectangle2D.Double();
115 bounds = calculateBounds(oldBounds);
118 private Rectangle2D calculateBounds(Rectangle2D rect) {
119 return calculatePath(edge).getBounds2D();
122 public void setDNEdge(DistrictNetworkEdge edge) {
127 public void setColor(Color color) {
131 public Color getColor() {
135 @PropertySetter(value = "stroke")
136 public void setStroke(Double stroke) {
137 this.stroke = stroke;