1 package org.simantics.district.network.ui.nodes;
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;
12 import org.simantics.district.network.ui.styles.DistrictNetworkStaticInfoStyle;
13 import org.simantics.scenegraph.ParentNode;
14 import org.simantics.scenegraph.g2d.G2DNode;
15 import org.simantics.scenegraph.g2d.nodes.spatial.RTreeNode;
16 import org.simantics.scenegraph.utils.DPIUtil;
17 import org.simantics.scenegraph.utils.NodeUtil;
19 public class DistrictNetworkStaticInfoNode extends G2DNode implements DeferredNode {
21 private static final long serialVersionUID = -1723122278582600873L;
23 private static final Font FONT = new Font(Font.SANS_SERIF, Font.PLAIN, DPIUtil.upscale(10));
25 public static final String NODE_KEY = "DISTRICT_NETWORK_STATIC_INFO";
28 Point2D origin = new Point2D.Double();
29 Point2D direction = new Point2D.Double();
31 private DistrictNetworkEdgeNode edgeNode = null;
33 private int prevZoomLevel = -1;
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;
40 deferred.deferNode(g.getTransform(), this);
46 public void renderDeferred(Graphics2D g) {
47 if (info == null || "".equals(info))
49 int zoomLevel = (Integer) g.getRenderingHint(DistrictRenderingHints.KEY_VIEW_ZOOM_LEVEL);
53 AffineTransform oldTransform = g.getTransform();
55 Font oldFont = g.getFont();
56 Color oldColor = g.getColor();
57 Object oldAA = g.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
58 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
60 if (edgeNode != null && zoomLevel != prevZoomLevel) {
61 origin.setLocation(edgeNode.getCenterPoint(zoomLevel));
62 Point2D dir = edgeNode.getDirection(zoomLevel);
63 double s = dir.getX() >= 0.0 ? 1.0 : -1.0;
64 direction.setLocation(s * dir.getX(), s * dir.getY());
66 prevZoomLevel = zoomLevel;
70 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldAA);
71 g.setTransform(oldTransform);
76 private void doRender(Graphics2D g) {
77 g.translate(origin.getX(), origin.getY());
78 double scale = 1.0 / g.getTransform().getScaleX();
79 g.scale(scale, scale);
82 FontMetrics fm = g.getFontMetrics();
83 int width = fm.stringWidth(info);
84 int ascent = fm.getMaxAscent();
86 g.transform(AffineTransform.getRotateInstance(direction.getX(), direction.getY()));
87 g.translate(0, ascent+2);
89 // int height = fm.getHeight();
90 // g.setColor(Color.WHITE);
91 // g.fillRect(-width/2 - 5, -ascent-1, width+10, height+2);
92 g.setColor(Color.BLACK);
93 // g.drawRect(-width/2 - 5, -ascent-1, width+10, height+2);
95 g.drawString(info, -width/2, 0);
99 public Rectangle2D getBoundsInLocal() {
103 public void setLocation(Point2D origin, Point2D direction) {
104 this.origin.setLocation(origin);
105 this.direction.setLocation(direction);
108 public void setInfo(String info) {
112 public void setEdgeNode(DistrictNetworkEdgeNode n) {
114 // Ensure that origin/location are recalculated