]> gerrit.simantics Code Review - simantics/district.git/blob
c26c175907635f583b469a18ac465cebac443141
[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.Stroke;
7 import java.awt.geom.Point2D;
8 import java.awt.geom.Rectangle2D;
9 import java.util.Collection;
10 import java.util.Collections;
11
12 import org.simantics.scenegraph.g2d.G2DNode;
13 import org.simantics.scenegraph.utils.GeometryUtils;
14
15 public class ElevationServerNode extends G2DNode {
16
17     private static final long serialVersionUID = 3832908017166017921L;
18
19     private static final Stroke DASHED_STROKE = new BasicStroke(2.0f,
20             BasicStroke.CAP_ROUND,
21             BasicStroke.JOIN_ROUND,
22             4.0f, new float[]{4.0f}, 0.0f);
23
24     private static final Color BLUE_ALPHA = new Color(0, 0, 255, 100);
25
26     public static final String ID = "elevationServerNode";
27
28     private Collection<Rectangle2D> rectangles;
29     
30     @Override
31     public void render(Graphics2D g2d) {
32
33         Color old = g2d.getColor();
34         Stroke oldStroke = g2d.getStroke();
35         
36         g2d.setColor(BLUE_ALPHA);
37         BasicStroke stroke = GeometryUtils.scaleStroke(DASHED_STROKE, (float) (1.0 / GeometryUtils.getScale(g2d.getTransform())));
38         g2d.setStroke(stroke);
39         
40         double scale = getTransform().getScaleY();
41         
42         for (Rectangle2D rect : getRectangles()) {
43             
44             Point2D point = new Point2D.Double(rect.getY(), rect.getX());
45             Point2D result = DistrictNetworkNodeUtils.calculatePoint2D(point, null);
46
47             Point2D point2 = new Point2D.Double(rect.getY() + rect.getHeight(), rect.getX() + rect.getWidth());
48             Point2D result2 = DistrictNetworkNodeUtils.calculatePoint2D(point2, null);
49             
50             double x = result.getX() * scale;
51             double y = result.getY() * scale;
52             double width = result2.getX() * scale - result.getX() * scale;
53             double height = result2.getY() * scale - result.getY() * scale;
54             
55             Rectangle2D translated = new Rectangle2D.Double(x, y, width, Math.abs(height));
56             g2d.draw(translated);
57         }
58         
59         g2d.setStroke(oldStroke);
60         g2d.setColor(old);
61     }
62
63     @Override
64     public Rectangle2D getBoundsInLocal() {
65         Rectangle2D bounds = new Rectangle2D.Double();
66         for (Rectangle2D rect : getRectangles()) {
67             bounds.add(rect);
68         }
69         return bounds;
70     }
71
72     public void setRectangles(Collection<Rectangle2D> rectangles) {
73         this.rectangles = rectangles;
74     }
75
76     public Collection<Rectangle2D> getRectangles() {
77         if (rectangles == null)
78             return Collections.emptyList();
79         return rectangles;
80     }
81 }