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.Rectangle2D;
11 import org.simantics.district.network.ui.DistrictNetworkEdge;
12 import org.simantics.scenegraph.g2d.G2DNode;
13 import org.simantics.scenegraph.utils.GeometryUtils;
14 import org.simantics.scenegraph.utils.NodeUtil;
16 public class DistrictNetworkEdgeNode extends G2DNode {
18 private static final long serialVersionUID = 8049769475036519806L;
20 private static final Stroke SELECTION_STROKE = new BasicStroke(1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);
21 private static final Color SELECTION_COLOR = new Color(255, 0, 255, 96);
23 private DistrictNetworkEdge edge;
24 private Rectangle2D bounds;
26 private Stroke stroke = new BasicStroke(2);
27 private boolean scaleStroke = true;
35 public void render(Graphics2D g2d) {
37 AffineTransform ot = null;
38 AffineTransform t = getTransform();
39 if (t != null && !t.isIdentity()) {
40 ot = g2d.getTransform();
41 g2d.transform(getTransform());
44 Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
45 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
47 Color oldColor = g2d.getColor();
48 BasicStroke oldStroke = (BasicStroke) g2d.getStroke();
49 // boolean selected = isSelected();
51 // Path2D selectionPath = edge.getPath();
52 // Shape selectionShape = SELECTION_STROKE.createStrokedShape(selectionPath);
53 // g2d.setColor(SELECTION_COLOR);
54 // g2d.fill(selectionShape);
57 g2d.setColor(Color.BLUE);
59 if (scaleStroke && stroke instanceof BasicStroke) {
60 BasicStroke bs = GeometryUtils.scaleStroke(stroke, (float) (1.0 / GeometryUtils.getScale(g2d.getTransform())));
63 g2d.setStroke(stroke);
67 g2d.draw(edge.getPath());
70 g2d.setStroke(oldStroke);
71 g2d.setColor(oldColor);
72 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
78 private boolean isSelected() {
79 return NodeUtil.isSelected(this, 1);
83 public Rectangle2D getBoundsInLocal() {
87 private void updateBounds() {
88 Rectangle2D oldBounds = bounds;
89 if (oldBounds == null)
90 oldBounds = new Rectangle2D.Double();
91 bounds = calculateBounds(oldBounds);
94 private Rectangle2D calculateBounds(Rectangle2D rect) {
95 return edge.getBounds(rect);
98 public void setDNEdge(DistrictNetworkEdge edge) {