]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/ConnectionLineNode.java
Add connected components to visualisation from profiles
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / nodes / ConnectionLineNode.java
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.geom.Line2D;
7 import java.awt.geom.Point2D;
8 import java.awt.geom.Rectangle2D;
9 import java.util.List;
10
11 import org.simantics.g2d.utils.GeometryUtils;
12 import org.simantics.scenegraph.g2d.G2DNode;
13
14 public class ConnectionLineNode extends G2DNode {
15     
16     public static final String NODE_NAME = "districtNetworkConnection";
17     
18         private static final BasicStroke STROKE = new BasicStroke(1.f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1.f, new float[] {4.f, 2.f}, 0.f);
19         private static final Color[] colors = { Color.RED, Color.GREEN, Color.BLUE, Color.ORANGE, Color.CYAN, Color.PINK };
20         
21         private float strokeWidth;
22         private Line2D[] lines;
23
24         public ConnectionLineNode() {
25                 super();
26         }
27         
28         private static final long serialVersionUID = 1L;
29
30         @Override
31         public Rectangle2D getBoundsInLocal() {
32                 return null;
33         }
34
35         @Override
36         public Rectangle2D getBoundsInLocal(boolean b) {
37                 return null;
38         }
39
40         @Override
41         public Rectangle2D getBounds() {
42                 return null;
43         }
44         
45         public void setStrokeWidth(float w) {
46                 strokeWidth = w;
47         }
48         
49         public void setPoints(List<Point2D> result) {
50                 Point2D p0 = DistrictNetworkNodeUtils.calculatePoint2D(result.get(0), null);
51                 lines = new Line2D[result.size() - 1];
52                 for (int i = 1; i < result.size(); i++) 
53                 {
54                         Point2D p = result.get(i);
55                         lines[i-1] = p != null ? new Line2D.Double(p0, DistrictNetworkNodeUtils.calculatePoint2D(p, null)) : null;
56                 }
57         }
58         
59         @Override
60         public void render(Graphics2D g2d) {
61                 if (lines == null || lines.length == 0)
62                         return;
63                 
64                 // Keep fixed line width on screen
65                 float scaleRecip = (float) GeometryUtils.getScale(g2d.getTransform());
66                 g2d.setStroke(GeometryUtils.scaleStroke(STROKE, strokeWidth / scaleRecip));
67                 
68                 for (int i = 0; i < lines.length; i++) {
69                         if (lines[i] != null) {
70                                 g2d.setColor(colors[i % colors.length]);
71                                 g2d.draw(lines[i]);
72                         }
73                 }
74         }
75 }