1 package fr.ifremer.dali.ui.swing.util.map.layer;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 import org.apache.commons.collections4.CollectionUtils;
25 import org.geotools.geometry.jts.ReferencedEnvelope;
26 import org.geotools.map.DirectLayer;
27 import org.geotools.map.MapContent;
28 import org.geotools.map.MapViewport;
29
30 import java.awt.*;
31 import java.awt.geom.Rectangle2D;
32 import java.util.List;
33
34
35
36
37 public class InfoPanelLayer extends DirectLayer implements DataLayer {
38
39 private Point point;
40 private List<String> texts;
41
42 private static final int LINE_LENGTH = 30;
43 private static final int TEXT_BORDER = 10;
44
45 public InfoPanelLayer() {
46 setTitle("infoPanelLayer");
47 }
48
49 public void setInfo(Point point, List<String> texts) {
50 this.point = point;
51 this.texts = texts;
52 setVisible(true);
53 }
54
55 public void clear() {
56 point = null;
57 texts = null;
58 }
59
60 private boolean isValid() {
61 return point != null && CollectionUtils.isNotEmpty(texts);
62 }
63
64 @Override
65 public void draw(Graphics2D graphics, MapContent map, MapViewport viewport) {
66
67 if (!isValid()) return;
68
69 Stroke oldStroke = graphics.getStroke();
70 Paint oldPaint = graphics.getPaint();
71 Color oldBackground = graphics.getBackground();
72
73
74 graphics.setStroke(new BasicStroke(2));
75 graphics.setPaint(Color.BLACK);
76
77
78 FontMetrics fontMetrics = graphics.getFontMetrics();
79 int maxTextWidth = 0;
80 int maxTextHeight = 0;
81 for (String text : texts) {
82 Rectangle2D textRect = fontMetrics.getStringBounds(text, graphics);
83 maxTextWidth = Math.max(maxTextWidth, (int) (textRect.getWidth() + 0.5));
84 maxTextHeight = Math.max(maxTextHeight, (int) (textRect.getHeight() + 0.5));
85 }
86
87 int boxWidth = maxTextWidth + TEXT_BORDER * 2;
88 int boxHeight = maxTextHeight * texts.size() + TEXT_BORDER * 2;
89
90 graphics.setPaint(Color.WHITE);
91 graphics.fillRoundRect(point.x - boxWidth / 2, point.y - LINE_LENGTH - boxHeight, boxWidth, boxHeight, 10, 10);
92 graphics.setPaint(Color.BLACK);
93 graphics.drawRoundRect(point.x - boxWidth / 2, point.y - LINE_LENGTH - boxHeight, boxWidth, boxHeight, 10, 10);
94
95 for (int i = 0; i < texts.size(); i++) {
96 graphics.drawString(texts.get(i),
97 point.x - maxTextWidth / 2,
98 point.y - LINE_LENGTH - maxTextHeight * texts.size() + (i * maxTextHeight));
99 }
100
101 graphics.drawLine(point.x, point.y, point.x, point.y - LINE_LENGTH);
102
103 graphics.setStroke(oldStroke);
104 graphics.setPaint(oldPaint);
105 graphics.setBackground(oldBackground);
106 }
107
108 @Override
109 public ReferencedEnvelope getBounds() {
110 return null;
111 }
112 }