]> gerrit.simantics Code Review - simantics/district.git/blob
eb1d26eb7359c9983c1183804b93f758bd5261fd
[simantics/district.git] /
1 package org.simantics.district.network.ui.nodes;\r
2 \r
3 import java.awt.BasicStroke;\r
4 import java.awt.Color;\r
5 import java.awt.Graphics2D;\r
6 import java.awt.RenderingHints;\r
7 import java.awt.Shape;\r
8 import java.awt.Stroke;\r
9 import java.awt.geom.AffineTransform;\r
10 import java.awt.geom.Path2D;\r
11 import java.awt.geom.Rectangle2D;\r
12 \r
13 import org.simantics.district.network.ui.DistrictNetworkEdge;\r
14 import org.simantics.scenegraph.g2d.G2DNode;\r
15 import org.simantics.scenegraph.utils.NodeUtil;\r
16 \r
17 public class DistrictNetworkEdgeNode extends G2DNode {\r
18 \r
19     private static final long serialVersionUID = 8049769475036519806L;\r
20     \r
21     private static final Stroke     SELECTION_STROKE = new BasicStroke(1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);\r
22     private static final Color      SELECTION_COLOR  = new Color(255, 0, 255, 96);\r
23     \r
24     private DistrictNetworkEdge edge;\r
25     private Rectangle2D bounds;\r
26 \r
27     @Override\r
28     public void render(Graphics2D g2d) {\r
29         \r
30         AffineTransform ot = null;\r
31         AffineTransform t = getTransform();\r
32         if (t != null && !t.isIdentity()) {\r
33             ot = g2d.getTransform();\r
34             g2d.transform(getTransform());\r
35         }\r
36         \r
37         Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);\r
38         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);\r
39 \r
40         Color oldColor = g2d.getColor();\r
41 //        boolean selected = isSelected();\r
42 //        if (selected) {\r
43 //            Path2D selectionPath = edge.getPath();\r
44 //            Shape selectionShape = SELECTION_STROKE.createStrokedShape(selectionPath);\r
45 //            g2d.setColor(SELECTION_COLOR);\r
46 //            g2d.fill(selectionShape);\r
47 //        }\r
48 //        \r
49         g2d.setColor(Color.BLUE);\r
50         // render\r
51         g2d.draw(edge.getPath());\r
52         \r
53         // Reset\r
54         g2d.setColor(oldColor);\r
55         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);\r
56         \r
57         if (ot != null)\r
58             g2d.setTransform(ot);\r
59     }\r
60 \r
61     private boolean isSelected() {\r
62         return NodeUtil.isSelected(this, 1);\r
63     }\r
64 \r
65     @Override\r
66     public Rectangle2D getBoundsInLocal() {\r
67         return bounds;\r
68     }\r
69     \r
70     private void updateBounds() {\r
71         Rectangle2D oldBounds = bounds;\r
72         if (oldBounds == null)\r
73             oldBounds = new Rectangle2D.Double();\r
74         bounds = calculateBounds(oldBounds);\r
75     }\r
76 \r
77     private Rectangle2D calculateBounds(Rectangle2D rect) {\r
78         return edge.getBounds(rect);\r
79     }\r
80 \r
81     public void setDNEdge(DistrictNetworkEdge edge) {\r
82         this.edge = edge;\r
83         updateBounds();\r
84     }\r
85 \r
86 }\r