]> gerrit.simantics Code Review - simantics/district.git/blob
a126ee5942a841f50a2108684a364a78052a4554
[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.adapters.DistrictNetworkVertex;
12 import org.simantics.scenegraph.g2d.G2DNode;
13 import org.simantics.scenegraph.utils.GeometryUtils;
14
15 public class DistrictNetworkVertexNode extends G2DNode {
16
17     private static final long serialVersionUID = -2641639101400236719L;
18     private DistrictNetworkVertex vertex;
19     
20     private static final double scale = 1;
21     
22     private double left = -0.5;
23     private double top = -0.5;
24     private double width = 1;
25     private double height = 1;
26     
27     private Stroke stroke = new BasicStroke(2);
28     private boolean scaleStroke = true;
29
30     @Override
31     public void init() {
32         setZIndex(2);
33     }
34     
35     @Override
36     public void render(Graphics2D g2d) {
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_OFF);
46         
47         Color oldColor = g2d.getColor();
48         Stroke oldStroke = g2d.getStroke();
49         
50         g2d.setColor(Color.RED);
51         if (stroke != null) {
52             if (scaleStroke && stroke instanceof BasicStroke) {
53                 BasicStroke bs = GeometryUtils.scaleStroke(stroke, (float) (1.0 / GeometryUtils.getScale(g2d.getTransform())));
54                 g2d.setStroke(bs);
55             } else {
56                 g2d.setStroke(stroke);
57             }
58         }
59         // render
60         
61         Rectangle2D.Double rect = new Rectangle2D.Double(left / scale, top / scale, width / scale, height / scale);
62         g2d.draw(rect);
63         
64         // Reset stats
65         g2d.setColor(oldColor);
66         g2d.setStroke(oldStroke);
67         
68         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
69         
70         if (ot != null)
71             g2d.setTransform(ot);
72     }
73
74     @Override
75     public Rectangle2D getBoundsInLocal() {
76         return new Rectangle2D.Double(left / scale, top / scale, width / scale, height / scale);
77     }
78
79     public void setVertex(DistrictNetworkVertex vertex) {
80         this.vertex = vertex;
81     }
82
83 }