]> gerrit.simantics Code Review - simantics/district.git/blob
347261c43a9dc462e8e4c56b77947c9eaa26690a
[simantics/district.git] /
1 package org.simantics.district.network.ui.nodes;
2
3 import java.awt.Color;
4 import java.awt.Font;
5 import java.awt.Graphics2D;
6 import java.awt.RenderingHints;
7 import java.awt.geom.AffineTransform;
8 import java.awt.geom.NoninvertibleTransformException;
9 import java.awt.geom.Point2D;
10 import java.awt.geom.Rectangle2D;
11
12 import org.simantics.district.network.DistrictNetworkUtil;
13 import org.simantics.district.network.ui.DistrictNetworkUIUtil;
14 import org.simantics.maps.MapScalingTransform;
15 import org.simantics.scenegraph.g2d.G2DNode;
16 import org.simantics.scenegraph.g2d.G2DParentNode;
17 import org.simantics.scenegraph.utils.DPIUtil;
18
19 public class DistrictNetworkStaticInfoNode extends G2DNode {
20
21         private static final Font FONT = new Font("Tahoma", Font.PLAIN, (int)(DPIUtil.upscale(9) * MapScalingTransform.getScaleY() + 0.5));
22         
23         private static final long serialVersionUID = 1L;
24
25         private static final Point2D UNIT_X = new Point2D.Double(1.0, 0.0);
26
27         public static final String NODE_KEY = "DISTRICT_NETWORK_STATIC_INFO";
28         
29         String info = null;
30         Point2D origin = new Point2D.Double();
31         Point2D direction = UNIT_X;
32         
33         @Override
34         public void render(Graphics2D g) {
35                 if (info == null || "".equals(info))
36                         return;
37                 
38                 AffineTransform oldTransform = g.getTransform();
39                 
40                 // There has to be a better way to calculate the zoom level in this context...
41                 AffineTransform baseTransform;
42                 try {
43                         baseTransform = ((G2DParentNode)getParent()).getTransform().createInverse();
44                 } catch (NoninvertibleTransformException e) {
45                         baseTransform = new AffineTransform();
46                 }
47                 
48                 baseTransform.preConcatenate(oldTransform);
49                 int zoomLevel = MapScalingTransform.zoomLevel(baseTransform);
50                 if (zoomLevel < 15)
51                         return;
52                 
53                 Font oldFont = g.getFont();
54                 Color oldColor = g.getColor();
55                 Object oldAA = g.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
56                 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
57                 
58                 doRender(g);
59
60                 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldAA);
61                 g.setTransform(oldTransform);
62                 g.setColor(oldColor);
63                 g.setFont(oldFont);
64         }
65
66         private void doRender(Graphics2D g) {
67                 g.translate(origin.getX(), origin.getY());
68                 double scale = 1.5 / g.getTransform().getScaleX();
69                 g.scale(scale, scale);
70
71                 int width1 = g.getFontMetrics().stringWidth(info);
72                 int height = g.getFontMetrics().getHeight();
73                 
74                 g.setColor(Color.WHITE);
75                 g.fillRect(-width1/2 - 5, -2, width1, height);
76                 
77                 g.setFont(FONT);
78                 g.setColor(Color.BLACK);
79                 
80                 g.transform(AffineTransform.getRotateInstance(direction.getX(), direction.getY()));
81                 g.translate(0, 10);
82                 
83                 g.drawString(info, -width1/2, 0);
84         }
85
86         @Override
87         public Rectangle2D getBoundsInLocal() {
88                 return null;
89         }
90
91         public void setLocation(Point2D origin, Point2D direction) {
92                 this.origin = origin;
93                 this.direction = direction;
94         }
95
96         public void setInfo(String info) {
97                 this.info = info;
98         }
99 }