]> gerrit.simantics Code Review - simantics/district.git/blob
408625f5aeeaa0bcafc139c636291d921860124d
[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     @Override
30     public void init() {
31
32     }
33     
34     @Override
35     public void render(Graphics2D g2d) {
36         
37         AffineTransform ot = null;
38         AffineTransform t = getTransform();
39         if (t != null && !t.isIdentity()) {
40             ot = g2d.getTransform();
41             g2d.transform(getTransform());
42         }
43         
44         Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
45         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
46
47         Color oldColor = g2d.getColor();
48         BasicStroke oldStroke = (BasicStroke) g2d.getStroke();
49 //        boolean selected = isSelected();
50 //        if (selected) {
51 //            Path2D selectionPath = edge.getPath();
52 //            Shape selectionShape = SELECTION_STROKE.createStrokedShape(selectionPath);
53 //            g2d.setColor(SELECTION_COLOR);
54 //            g2d.fill(selectionShape);
55 //        }
56 //        
57         g2d.setColor(Color.BLUE);
58         if (STROKE != null) {
59             if (scaleStroke && STROKE instanceof BasicStroke) {
60                 BasicStroke bs = GeometryUtils.scaleStroke(STROKE, (float) (1.0 / GeometryUtils.getScale(g2d.getTransform())));
61                 g2d.setStroke(bs);
62             } else {
63                 g2d.setStroke(STROKE);
64             }
65         }
66         // render
67         g2d.draw(edge.getPath());
68         
69         // Reset
70         g2d.setStroke(oldStroke);
71         g2d.setColor(oldColor);
72         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
73         
74         if (ot != null)
75             g2d.setTransform(ot);
76     }
77
78     private boolean isSelected() {
79         return NodeUtil.isSelected(this, 1);
80     }
81
82     @Override
83     public Rectangle2D getBoundsInLocal() {
84         return bounds;
85     }
86     
87     private void updateBounds() {
88         Rectangle2D oldBounds = bounds;
89         if (oldBounds == null)
90             oldBounds = new Rectangle2D.Double();
91         bounds = calculateBounds(oldBounds);
92     }
93
94     private Rectangle2D calculateBounds(Rectangle2D rect) {
95         return edge.getBounds(rect);
96     }
97
98     public void setDNEdge(DistrictNetworkEdge edge) {
99         this.edge = edge;
100         updateBounds();
101     }
102
103 }