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 static final Stroke STROKE = new BasicStroke(4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
27 private boolean scaleStroke = true;
37 public void render(Graphics2D g2d) {
39 AffineTransform ot = null;
40 AffineTransform t = getTransform();
41 if (t != null && !t.isIdentity()) {
42 ot = g2d.getTransform();
43 g2d.transform(getTransform());
46 Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
47 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
49 Color oldColor = g2d.getColor();
50 BasicStroke oldStroke = (BasicStroke) g2d.getStroke();
51 // boolean selected = isSelected();
53 // Path2D selectionPath = edge.getPath();
54 // Shape selectionShape = SELECTION_STROKE.createStrokedShape(selectionPath);
55 // g2d.setColor(SELECTION_COLOR);
56 // g2d.fill(selectionShape);
61 if (scaleStroke && STROKE instanceof BasicStroke) {
62 BasicStroke bs = GeometryUtils.scaleStroke(STROKE, (float) (1.0 / GeometryUtils.getScale(g2d.getTransform())));
65 g2d.setStroke(STROKE);
69 g2d.draw(edge.getPath());
72 g2d.setStroke(oldStroke);
73 g2d.setColor(oldColor);
74 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
80 private boolean isSelected() {
81 return NodeUtil.isSelected(this, 1);
85 public Rectangle2D getBoundsInLocal() {
89 private void updateBounds() {
90 Rectangle2D oldBounds = bounds;
91 if (oldBounds == null)
92 oldBounds = new Rectangle2D.Double();
93 bounds = calculateBounds(oldBounds);
96 private Rectangle2D calculateBounds(Rectangle2D rect) {
97 return edge.getBounds(rect);
100 public void setDNEdge(DistrictNetworkEdge edge) {
105 public void setColor(Color color) {
109 public Color getColor() {