]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkStaticInfoNode.java
Fixed most warnings from district codebase after JavaSE-11 switch
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / nodes / DistrictNetworkStaticInfoNode.java
1 package org.simantics.district.network.ui.nodes;
2
3 import java.awt.Color;
4 import java.awt.Font;
5 import java.awt.FontMetrics;
6 import java.awt.Graphics2D;
7 import java.awt.RenderingHints;
8 import java.awt.geom.AffineTransform;
9 import java.awt.geom.Point2D;
10 import java.awt.geom.Rectangle2D;
11
12 import org.simantics.scenegraph.ParentNode;
13 import org.simantics.scenegraph.g2d.G2DNode;
14 import org.simantics.scenegraph.g2d.nodes.spatial.RTreeNode;
15 import org.simantics.scenegraph.utils.DPIUtil;
16 import org.simantics.scenegraph.utils.NodeUtil;
17
18 public class DistrictNetworkStaticInfoNode extends G2DNode implements DeferredNode {
19
20         private static final long serialVersionUID = -1723122278582600873L;
21
22         private static final Font FONT = new Font(Font.SANS_SERIF, Font.PLAIN, DPIUtil.upscale(10));
23
24         public static final String NODE_KEY = "DISTRICT_NETWORK_STATIC_INFO";
25
26         public static final String STATIC_INFO_DEFERRED = "staticInfo";
27
28         String info = null;
29         Point2D origin = new Point2D.Double();
30         Point2D direction = new Point2D.Double();
31
32         private DistrictNetworkEdgeNode edgeNode = null;
33
34         private int prevZoomLevel = -1;
35
36         @Override
37         public void render(Graphics2D g) {
38                 ParentNode<?> root = (ParentNode<?>) NodeUtil.getNearestParentOfType(this, RTreeNode.class);
39                 DeferredRenderingNode deferred = root != null ? (DeferredRenderingNode) root.getNode(STATIC_INFO_DEFERRED) : null;
40                 if (deferred != null)
41                         deferred.deferNode(g.getTransform(), this);
42                 else
43                         renderDeferred(g);
44         }
45
46         @Override
47         public void renderDeferred(Graphics2D g) {
48                 if (info == null || "".equals(info))
49                         return;
50                 int zoomLevel = (Integer) g.getRenderingHint(DistrictRenderingHints.KEY_VIEW_ZOOM_LEVEL);
51                 if (zoomLevel < 17)
52                         return;
53
54                 AffineTransform oldTransform = g.getTransform();
55
56                 Font oldFont = g.getFont();
57                 Color oldColor = g.getColor();
58                 Object oldAA = g.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
59                 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
60
61                 if (edgeNode != null && zoomLevel != prevZoomLevel) {
62                         origin.setLocation(edgeNode.getCenterPoint(zoomLevel));
63                         Point2D dir = edgeNode.getDirection(zoomLevel);
64                         double s = dir.getX() >= 0.0 ? 1.0 : -1.0;
65                         direction.setLocation(s * dir.getX(), s * dir.getY());
66                 }
67                 prevZoomLevel = zoomLevel;
68
69                 doRender(g);
70
71                 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldAA);
72                 g.setTransform(oldTransform);
73                 g.setColor(oldColor);
74                 g.setFont(oldFont);
75         }
76
77         private void doRender(Graphics2D g) {
78                 g.translate(origin.getX(), origin.getY());
79                 double scale = 1.0 / g.getTransform().getScaleX();
80                 g.scale(scale, scale);
81
82                 g.setFont(FONT);
83                 FontMetrics fm = g.getFontMetrics();
84                 int width = fm.stringWidth(info);
85                 int ascent = fm.getMaxAscent();
86
87                 g.transform(AffineTransform.getRotateInstance(direction.getX(), direction.getY()));
88                 g.translate(0, ascent+2);
89
90 //              int height = fm.getHeight();
91 //              g.setColor(Color.WHITE);
92 //              g.fillRect(-width/2 - 5, -ascent-1, width+10, height+2);
93                 g.setColor(Color.BLACK);
94 //              g.drawRect(-width/2 - 5, -ascent-1, width+10, height+2);
95
96                 g.drawString(info, -width/2, 0);
97         }
98
99         @Override
100         public Rectangle2D getBoundsInLocal() {
101                 return null;
102         }
103
104         public void setLocation(Point2D origin, Point2D direction) {
105                 this.origin.setLocation(origin);
106                 this.direction.setLocation(direction);
107         }
108
109         public void setInfo(String info) {
110                 this.info = info;
111         }
112
113         public void setEdgeNode(DistrictNetworkEdgeNode n) {
114                 this.edgeNode = n;
115                 // Ensure that origin/location are recalculated
116                 prevZoomLevel = -1;
117         }
118 }