]> gerrit.simantics Code Review - simantics/district.git/blob
cb7053a01839d4c0a457e859c56a2985c76afd4f
[simantics/district.git] /
1 package org.simantics.district.network.ui.nodes;
2
3 import java.awt.Color;
4 import java.awt.Font;
5 import java.awt.Graphics2D;
6 import java.awt.geom.AffineTransform;
7 import java.awt.geom.Point2D;
8 import java.awt.geom.Rectangle2D;
9 import java.util.List;
10 import java.util.concurrent.atomic.AtomicReference;
11
12 import org.simantics.scenegraph.g2d.G2DNode;
13 import org.simantics.scl.runtime.Lists;
14 import org.simantics.scl.runtime.tuple.Tuple3;
15
16 public class DistrictNetworkHoverInfoNode extends G2DNode implements HoverSensitiveNode {
17
18         private static final Font FONT = new Font(Font.DIALOG, Font.PLAIN, 12);
19
20         private static final long serialVersionUID = 1L;
21
22         public static final String NODE_KEY = "DISTRICT_NETWORK_HOVER_INFO";
23
24         private static final int W1 = 50;
25         private static final int W2 = 30;
26         private static final int PAD = 5;
27
28         private List<Tuple3> labels;
29
30         private Point2D origin;
31
32         private boolean hover = false;
33         
34         private static AtomicReference<DistrictNetworkHoverInfoNode> activeNode = new AtomicReference<>();
35
36         @Override
37         public void render(Graphics2D g) {
38                 if (!hover || activeNode.get() != this)
39                         return;
40
41                 AffineTransform ot = g.getTransform();
42                 Font of = g.getFont();
43                 doRender(g);
44                 g.setFont(of);
45                 g.setTransform(ot);
46         }
47
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);
52
53                 g.setFont(FONT);
54                 double rowHeight = g.getFontMetrics().getHeight() * 1.1;
55
56                 g.setColor(Color.BLACK);
57                 
58                 for (Tuple3 t : labels) {
59                         g.translate(0.f, -rowHeight);
60                         
61                         if (t.c0 != null) {
62                                 g.drawString((String) t.c0, -(W1 + PAD + W2), 0.f);
63                         }
64                         
65                         if (t.c1 != null) {
66                                 int width1 = g.getFontMetrics().stringWidth((String) t.c1);
67                                 g.drawString((String) t.c1, - width1, 0.f);
68                         }
69                         
70                         if (t.c2 != null) {
71                                 g.drawString((String) t.c2, PAD, 0.f);
72                         }
73                 }
74         }
75
76         @Override
77         public Rectangle2D getBoundsInLocal() {
78                 return null;
79         }
80
81         @SuppressWarnings("unchecked")
82         public void setLabels(List<Tuple3> list) {
83                 this.labels = Lists.reverse(list);
84         }
85
86         public void setOrigin(Point2D origin) {
87                 this.origin = origin;
88         }
89
90         @Override
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;
94                 this.hover = hover;
95                 
96                 if (changed) {
97                         if (!hover) activeNode.updateAndGet(current -> current == this ? null : current);
98                         repaint();
99                 }
100                 
101                 return changed;
102         }
103         
104         @Override
105         public void delete() {
106                 super.delete();
107                 activeNode.getAndUpdate(current -> current == this ? null : current);
108         }
109 }