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.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;
18 public class DistrictNetworkStaticInfoNode extends G2DNode implements DeferredNode {
20 private static final long serialVersionUID = -1723122278582600873L;
22 private static final Font FONT = new Font(Font.SANS_SERIF, Font.PLAIN, DPIUtil.upscale(10));
24 public static final String NODE_KEY = "DISTRICT_NETWORK_STATIC_INFO";
26 public static final String STATIC_INFO_DEFERRED = "staticInfo";
29 Point2D origin = new Point2D.Double();
30 Point2D direction = new Point2D.Double();
32 private DistrictNetworkEdgeNode edgeNode = null;
34 private int prevZoomLevel = -1;
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;
41 deferred.deferNode(g.getTransform(), this);
47 public void renderDeferred(Graphics2D g) {
48 if (info == null || "".equals(info))
50 int zoomLevel = (Integer) g.getRenderingHint(DistrictRenderingHints.KEY_VIEW_ZOOM_LEVEL);
54 AffineTransform oldTransform = g.getTransform();
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);
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());
67 prevZoomLevel = zoomLevel;
71 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldAA);
72 g.setTransform(oldTransform);
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);
83 FontMetrics fm = g.getFontMetrics();
84 int width = fm.stringWidth(info);
85 int ascent = fm.getMaxAscent();
87 g.transform(AffineTransform.getRotateInstance(direction.getX(), direction.getY()));
88 g.translate(0, ascent+2);
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);
96 g.drawString(info, -width/2, 0);
100 public Rectangle2D getBoundsInLocal() {
104 public void setLocation(Point2D origin, Point2D direction) {
105 this.origin.setLocation(origin);
106 this.direction.setLocation(direction);
109 public void setInfo(String info) {
113 public void setEdgeNode(DistrictNetworkEdgeNode n) {
115 // Ensure that origin/location are recalculated