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