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.scenegraph.g2d.G2DNode;
13 import org.simantics.scl.runtime.Lists;
14 import org.simantics.scl.runtime.tuple.Tuple3;
16 public class DistrictNetworkHoverInfoNode extends G2DNode implements HoverSensitiveNode {
18 private static final Font FONT = new Font(Font.DIALOG, Font.PLAIN, 12);
20 private static final long serialVersionUID = 1L;
22 public static final String NODE_KEY = "DISTRICT_NETWORK_HOVER_INFO";
24 private static final int W1 = 50;
25 private static final int W2 = 30;
26 private static final int PAD = 5;
28 private List<Tuple3> labels;
30 private Point2D origin;
32 private boolean hover = false;
34 private static AtomicReference<DistrictNetworkHoverInfoNode> activeNode = new AtomicReference<>();
37 public void render(Graphics2D g) {
38 if (!hover || activeNode.get() != this)
41 AffineTransform ot = g.getTransform();
42 Font of = g.getFont();
48 private void doRender(Graphics2D g) {
49 g.translate(origin.getX(), origin.getY());
50 double scale = 1.5 / g.getTransform().getScaleX();
51 g.scale(scale, scale);
54 double rowHeight = g.getFontMetrics().getHeight() * 1.1;
56 g.setColor(Color.BLACK);
58 for (Tuple3 t : labels) {
59 g.translate(0.f, -rowHeight);
62 g.drawString((String) t.c0, -(W1 + PAD + W2), 0.f);
66 int width1 = g.getFontMetrics().stringWidth((String) t.c1);
67 g.drawString((String) t.c1, - width1, 0.f);
71 g.drawString((String) t.c2, PAD, 0.f);
77 public Rectangle2D getBoundsInLocal() {
81 @SuppressWarnings("unchecked")
82 public void setLabels(List<Tuple3> list) {
83 this.labels = Lists.reverse(list);
86 public void setOrigin(Point2D origin) {
91 public boolean hover(boolean hover, boolean isConnectionTool) {
92 hover = hover && activeNode.updateAndGet(current -> current == null ? this : current) == this;
93 boolean changed = hover != this.hover;
97 if (!hover) activeNode.updateAndGet(current -> current == this ? null : current);
105 public void delete() {
107 activeNode.getAndUpdate(current -> current == this ? null : current);