1 package org.simantics.district.network.ui.nodes;
5 import java.awt.Graphics2D;
6 import java.awt.geom.AffineTransform;
7 import java.awt.geom.Point2D;
8 import java.awt.geom.Rectangle2D;
10 import java.util.concurrent.atomic.AtomicReference;
12 import org.simantics.maps.MapScalingTransform;
13 import org.simantics.scenegraph.g2d.G2DNode;
14 import org.simantics.scenegraph.utils.DPIUtil;
15 import org.simantics.scl.runtime.Lists;
16 import org.simantics.scl.runtime.tuple.Tuple3;
18 public class DistrictNetworkHoverInfoNode extends G2DNode implements HoverSensitiveNode {
20 private static final Font FONT = new Font("Tahoma", Font.PLAIN, (int)(DPIUtil.upscale(9) * MapScalingTransform.getScaleY() + 0.5));
22 private static final long serialVersionUID = 1L;
24 public static final String NODE_KEY = "DISTRICT_NETWORK_HOVER_INFO";
26 private static final int W1 = 50;
27 private static final int W2 = 30;
28 private static final int PAD = 5;
30 private List<Tuple3> labels;
32 private Point2D origin;
34 private boolean hover = false;
36 private static AtomicReference<DistrictNetworkHoverInfoNode> activeNode = new AtomicReference<>();
39 public void render(Graphics2D g) {
40 if (!hover || activeNode.get() != this)
43 AffineTransform ot = g.getTransform();
44 Font of = g.getFont();
50 private void doRender(Graphics2D g) {
51 g.translate(origin.getX(), origin.getY());
52 double scale = 1.5 / g.getTransform().getScaleX();
53 g.scale(scale, scale);
56 double rowHeight = g.getFontMetrics().getHeight() * 1.1;
58 g.setColor(Color.BLACK);
60 for (Tuple3 t : labels) {
61 g.translate(0.f, -rowHeight);
64 g.drawString((String) t.c0, -(W1 + PAD + W2), 0.f);
68 int width1 = g.getFontMetrics().stringWidth((String) t.c1);
69 g.drawString((String) t.c1, - width1, 0.f);
73 g.drawString((String) t.c2, PAD, 0.f);
79 public Rectangle2D getBoundsInLocal() {
83 @SuppressWarnings("unchecked")
84 public void setLabels(List<Tuple3> list) {
85 this.labels = Lists.reverse(list);
88 public void setOrigin(Point2D origin) {
93 public boolean hover(boolean hover, boolean isConnectionTool) {
94 hover = hover && activeNode.updateAndGet(current -> current == null ? this : current) == this;
95 boolean changed = hover != this.hover;
99 if (!hover) activeNode.updateAndGet(current -> current == this ? null : current);
107 public void delete() {
109 activeNode.getAndUpdate(current -> current == this ? null : current);