]> gerrit.simantics Code Review - simantics/district.git/blob
4fc1bebae4ec12f3e013ff1f42ad187c11589447
[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.Comparator;
10 import java.util.List;
11 import java.util.Optional;
12 import java.util.concurrent.atomic.AtomicReference;
13
14 import org.simantics.maps.MapScalingTransform;
15 import org.simantics.scenegraph.g2d.G2DNode;
16 import org.simantics.scenegraph.utils.DPIUtil;
17 import org.simantics.scl.runtime.Lists;
18 import org.simantics.scl.runtime.tuple.Tuple3;
19
20 public class DistrictNetworkHoverInfoNode extends G2DNode implements HoverSensitiveNode {
21
22         private static final Font FONT = new Font("Tahoma", Font.PLAIN, (int)(DPIUtil.upscale(9) * MapScalingTransform.getScaleY() + 0.5));
23
24         private static final long serialVersionUID = 1L;
25
26         public static final String NODE_KEY = "DISTRICT_NETWORK_HOVER_INFO";
27
28         private static final int W1 = 50;
29         private static final int W2 = 30;
30         private static final int PAD = 5;
31
32         private List<Tuple3> labels;
33
34         private Point2D origin;
35
36         private boolean hover = false;
37         
38         private static AtomicReference<DistrictNetworkHoverInfoNode> activeNode = new AtomicReference<>();
39
40         @Override
41         public void render(Graphics2D g) {
42                 if (!hover || activeNode.get() != this)
43                         return;
44
45                 AffineTransform ot = g.getTransform();
46                 Font of = g.getFont();
47                 doRender(g);
48                 g.setFont(of);
49                 g.setTransform(ot);
50         }
51
52         private void doRender(Graphics2D g) {
53                 g.translate(origin.getX(), origin.getY());
54                 double scale = 1.5 / g.getTransform().getScaleX();
55                 g.scale(scale, scale);
56
57                 g.setFont(FONT);
58                 double rowHeight = g.getFontMetrics().getHeight() * 1.1;
59
60         // let's calculate the max width
61         Optional<Integer> max = labels.stream().map(t -> g.getFontMetrics().stringWidth((String) t.c2)).max(Comparator.naturalOrder());
62         int width = max.orElse(10);
63         g.setColor(Color.WHITE);
64         int totalHeight = (int)Math.round(rowHeight * labels.size());
65         g.fillRect(-(W1 + PAD + W2 + 5), -(totalHeight + (int)Math.round(rowHeight)), (W1 + PAD + W2 + width + 10), totalHeight + 5);
66
67                 g.setColor(Color.BLACK);
68                 
69                 for (Tuple3 t : labels) {
70                         g.translate(0.f, -rowHeight);
71                         
72                         if (t.c0 != null) {
73                                 g.drawString((String) t.c0, -(W1 + PAD + W2), 0.f);
74                         }
75                         
76                         if (t.c1 != null) {
77                                 int width1 = g.getFontMetrics().stringWidth((String) t.c1);
78                                 g.drawString((String) t.c1, - width1, 0.f);
79                         }
80                         
81                         if (t.c2 != null) {
82                                 g.drawString((String) t.c2, PAD, 0.f);
83                         }
84                 }
85         }
86
87         @Override
88         public Rectangle2D getBoundsInLocal() {
89                 return null;
90         }
91
92         @SuppressWarnings("unchecked")
93         public void setLabels(List<Tuple3> list) {
94                 this.labels = Lists.reverse(list);
95         }
96
97         public void setOrigin(Point2D origin) {
98                 this.origin = origin;
99         }
100
101         @Override
102         public boolean hover(boolean hover, boolean isConnectionTool) {
103                 hover = hover && activeNode.updateAndGet(current -> current == null ? this : current) == this;
104                 boolean changed = hover != this.hover;
105                 this.hover = hover;
106                 
107                 if (changed) {
108                         if (!hover) activeNode.updateAndGet(current -> current == this ? null : current);
109                         repaint();
110                 }
111                 
112                 return changed;
113         }
114         
115         @Override
116         public void delete() {
117                 super.delete();
118                 activeNode.getAndUpdate(current -> current == this ? null : current);
119         }
120 }