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 private static final Point2D UNIT_X = new Point2D.Double(1.0, 0.0);
27 public static final String NODE_KEY = "DISTRICT_NETWORK_STATIC_INFO";
30 Point2D origin = new Point2D.Double();
31 Point2D direction = UNIT_X;
33 private DistrictNetworkEdgeNode edgeNode = null;
35 private int prevZoomLevel = -1;
38 public void render(Graphics2D g) {
39 ParentNode<?> root = (ParentNode<?>) NodeUtil.getNearestParentOfType(this, RTreeNode.class);
40 DeferredRenderingNode deferred = root != null ? (DeferredRenderingNode) root.getNode(DistrictNetworkStaticInfoStyle.STATIC_INFO_DEFERRED) : null;
42 deferred.deferNode(g.getTransform(), this);
48 public void renderDeferred(Graphics2D g) {
49 if (info == null || "".equals(info))
51 int zoomLevel = (Integer) g.getRenderingHint(DistrictRenderingHints.KEY_VIEW_ZOOM_LEVEL);
55 AffineTransform oldTransform = g.getTransform();
57 Font oldFont = g.getFont();
58 Color oldColor = g.getColor();
59 Object oldAA = g.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
60 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
62 if (edgeNode != null && zoomLevel != prevZoomLevel) {
63 origin.setLocation(edgeNode.getCenterPoint(zoomLevel));
64 Point2D dir = edgeNode.getDirection(zoomLevel);
65 double s = dir.getX() >= 0.0 ? 1.0 : -1.0;
66 direction.setLocation(s * dir.getX(), s * dir.getY());
68 prevZoomLevel = zoomLevel;
72 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldAA);
73 g.setTransform(oldTransform);
78 private void doRender(Graphics2D g) {
79 g.translate(origin.getX(), origin.getY());
80 double scale = 1.0 / g.getTransform().getScaleX();
81 g.scale(scale, scale);
84 FontMetrics fm = g.getFontMetrics();
85 int width = fm.stringWidth(info);
86 int ascent = fm.getMaxAscent();
88 g.transform(AffineTransform.getRotateInstance(direction.getX(), direction.getY()));
89 g.translate(0, ascent);
91 // int height = fm.getHeight();
92 // g.setColor(Color.WHITE);
93 // g.fillRect(-width/2 - 5, -ascent-1, width+10, height+2);
94 g.setColor(Color.BLACK);
95 // g.drawRect(-width/2 - 5, -ascent-1, width+10, height+2);
97 g.drawString(info, -width/2, 0);
101 public Rectangle2D getBoundsInLocal() {
105 public void setLocation(Point2D origin, Point2D direction) {
106 this.origin = origin;
107 this.direction = direction;
110 public void setInfo(String info) {
114 public void setEdgeNode(DistrictNetworkEdgeNode n) {