1 /**
2 * This program is free software: you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation, version 3.
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program. If not, see <http://www.gnu.org/licenses/>.
13 */
14
15 package net.curre.prefcount.bean;
16
17 import java.awt.FontMetrics;
18 import java.awt.Graphics2D;
19 import java.awt.Shape;
20 import java.awt.geom.Point2D;
21 import java.awt.geom.Rectangle2D;
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import net.curre.prefcount.gui.type.Place;
26 import net.curre.prefcount.gui.type.ScoreItem;
27
28 import org.apache.commons.lang.StringUtils;
29
30 /**
31 * This is a map to hold the score item locations (shapes)
32 * for the tooltip logic.
33 * <p/>
34 * Created date: Jun 25, 2008
35 *
36 * @author Yevgeny Nyden
37 */
38 public class TooltipLocationsMap extends HashMap<ScoreItem, Map<Place, Shape>> {
39
40 /** {@inheritDoc} */
41 @Override
42 public Map<Place, Shape> get(Object item) {
43 if (item instanceof ScoreItem == false) {
44 throw new IllegalArgumentException("Key is not an instance of " + ScoreItem.class.getName());
45 }
46
47 Map<Place, Shape> sMap = super.get(item);
48 if (sMap == null) {
49 sMap = new HashMap<Place, Shape>();
50 super.put((ScoreItem) item, sMap);
51 }
52
53 return sMap;
54 }
55
56 /**
57 * Clears a location for the given place and items.
58 *
59 * @param place player's place.
60 * @param items score items.
61 */
62 public void removeLocation(Place place, ScoreItem... items) {
63 for (ScoreItem item : items) {
64 this.get(item).remove(place);
65 }
66 }
67
68 /**
69 * Adds a shape that represents a tooltip area to the map.
70 *
71 * @param item score board item.
72 * @param place player's place.
73 * @param shape shpae to add.
74 */
75 public void addShapeLocation(ScoreItem item, Place place, Shape shape) {
76 Map<Place, Shape> shapesMap = this.get(item);
77 shapesMap.put(place, shape);
78 }
79
80 /**
81 * Helper method to compute and add a tooltip location
82 * to the tooltip location map for the given data. Note,
83 * that this method adds rectangular tooltip bounds.
84 *
85 * @param item score board item.
86 * @param place player's place.
87 * @param g2 graphics context.
88 * @param point point where the string is drawn.
89 * @param str string for which the tooltip location is recorded.
90 */
91 public void addRectangleLocation(ScoreItem item, Place place, Graphics2D g2,
92 Point2D.Double point, String str) {
93 Map<Place, Shape> shapesMap = this.get(item);
94 if (StringUtils.isNotBlank(str)) {
95 shapesMap.put(place, getRectangleBounds(g2, point, str));
96 } else {
97 shapesMap.remove(place);
98 }
99 }
100
101 /**
102 * Computes the tooltip bounding rectangle for the passed String
103 * using the passed graphics context.
104 *
105 * @param g2 Graphics object to use.
106 * @param point point at which the string is drawn.
107 * @param str string to be measured.
108 * @return The rectangle that represents the tooltip bounds of the passed string.
109 */
110 private static Rectangle2D.Double getRectangleBounds(Graphics2D g2, Point2D.Double point, String str) {
111 FontMetrics metrics = g2.getFontMetrics(g2.getFont());
112 final double width = metrics.stringWidth(str);
113 final double height = metrics.getHeight() - 3D;
114 final double x = point.getX();
115 final double y = point.getY() - height + 3D;
116 return new Rectangle2D.Double(x, y, width, height);
117 }
118
119 }