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