1 package org.simantics.district.network.ui.nodes;
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;
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;
21 public class DistrictNetworkStaticInfoNode extends G2DNode implements DeferredNode {
23 private static final Font FONT = new Font("Tahoma", Font.PLAIN, (int)(DPIUtil.upscale(9) * MapScalingTransform.getScaleY() + 0.5));
25 private static final long serialVersionUID = 1L;
27 private static final Point2D UNIT_X = new Point2D.Double(1.0, 0.0);
29 public static final String NODE_KEY = "DISTRICT_NETWORK_STATIC_INFO";
32 Point2D origin = new Point2D.Double();
33 Point2D direction = UNIT_X;
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))
50 AffineTransform oldTransform = g.getTransform();
52 // There has to be a better way to calculate the zoom level in this context...
53 AffineTransform baseTransform;
55 baseTransform = ((G2DParentNode)getParent()).getTransform().createInverse();
56 } catch (NoninvertibleTransformException e) {
57 baseTransform = new AffineTransform();
60 baseTransform.preConcatenate(oldTransform);
61 int zoomLevel = MapScalingTransform.zoomLevel(baseTransform);
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);
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.5 / g.getTransform().getScaleX();
81 g.scale(scale, scale);
84 int width1 = g.getFontMetrics().stringWidth(info);
85 // int height = g.getFontMetrics().getHeight();
87 g.transform(AffineTransform.getRotateInstance(direction.getX(), direction.getY()));
90 // g.setColor(Color.WHITE);
91 // g.fillRect(-width1/2 - 5, -height-2, width1+10, height+4);
93 g.setColor(Color.BLACK);
95 g.drawString(info, -width1/2, 0);
99 public Rectangle2D getBoundsInLocal() {
103 public void setLocation(Point2D origin, Point2D direction) {
104 this.origin = origin;
105 this.direction = direction;
108 public void setInfo(String info) {