View Javadoc
1   package fr.ifremer.dali.ui.swing.util.map.layer;
2   
3   /*-
4    * #%L
5    * Dali :: UI
6    * %%
7    * Copyright (C) 2014 - 2017 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Affero General Public License as published by
11   * the Free Software Foundation, either version 3 of the License, or
12   * (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU Affero General Public License
20   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21   * #L%
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   * @author peck7 on 14/09/2017.
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  //        graphics.setStroke(new BasicStroke(2, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{9}, 0));
74          graphics.setStroke(new BasicStroke(2));
75          graphics.setPaint(Color.BLACK);
76  
77          // calculate text height and width
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 }