]> gerrit.simantics Code Review - simantics/district.git/blob
11d883e91d560c77a4ceb2297ff4d51988aa657e
[simantics/district.git] /
1 package org.simantics.district.network.ui.nodes;
2
3 import java.awt.BasicStroke;
4 import java.awt.Color;
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;
10
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;
15
16 public class DistrictNetworkEdgeNode extends G2DNode {
17
18     private static final long serialVersionUID = 8049769475036519806L;
19     
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);
22     
23     private DistrictNetworkEdge edge;
24     private Rectangle2D bounds;
25     
26     private static final Stroke STROKE = new BasicStroke(4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
27     private boolean scaleStroke = true;
28
29     private Color color;
30
31     @Override
32     public void init() {
33
34     }
35     
36     @Override
37     public void render(Graphics2D g2d) {
38         
39         AffineTransform ot = null;
40         AffineTransform t = getTransform();
41         if (t != null && !t.isIdentity()) {
42             ot = g2d.getTransform();
43             g2d.transform(getTransform());
44         }
45         
46         Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
47         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
48
49         Color oldColor = g2d.getColor();
50         BasicStroke oldStroke = (BasicStroke) g2d.getStroke();
51 //        boolean selected = isSelected();
52 //        if (selected) {
53 //            Path2D selectionPath = edge.getPath();
54 //            Shape selectionShape = SELECTION_STROKE.createStrokedShape(selectionPath);
55 //            g2d.setColor(SELECTION_COLOR);
56 //            g2d.fill(selectionShape);
57 //        }
58 //        
59         g2d.setColor(color);
60         if (STROKE != null) {
61             if (scaleStroke && STROKE instanceof BasicStroke) {
62                 BasicStroke bs = GeometryUtils.scaleStroke(STROKE, (float) (1.0 / GeometryUtils.getScale(g2d.getTransform())));
63                 g2d.setStroke(bs);
64             } else {
65                 g2d.setStroke(STROKE);
66             }
67         }
68         // render
69         g2d.draw(edge.getPath());
70         
71         // Reset
72         g2d.setStroke(oldStroke);
73         g2d.setColor(oldColor);
74         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
75         
76         if (ot != null)
77             g2d.setTransform(ot);
78     }
79
80     private boolean isSelected() {
81         return NodeUtil.isSelected(this, 1);
82     }
83
84     @Override
85     public Rectangle2D getBoundsInLocal() {
86         return bounds;
87     }
88     
89     private void updateBounds() {
90         Rectangle2D oldBounds = bounds;
91         if (oldBounds == null)
92             oldBounds = new Rectangle2D.Double();
93         bounds = calculateBounds(oldBounds);
94     }
95
96     private Rectangle2D calculateBounds(Rectangle2D rect) {
97         return edge.getBounds(rect);
98     }
99
100     public void setDNEdge(DistrictNetworkEdge edge) {
101         this.edge = edge;
102         updateBounds();
103     }
104
105     public void setColor(Color color) {
106         this.color = color;
107     }
108     
109     public Color getColor() {
110         return color;
111     }
112
113 }