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