1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.Utils.equal;
|
---|
5 |
|
---|
6 | import java.awt.Color;
|
---|
7 | import java.awt.Rectangle;
|
---|
8 |
|
---|
9 | import org.openstreetmap.josm.data.osm.Node;
|
---|
10 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
11 | import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings;
|
---|
12 | import org.openstreetmap.josm.data.osm.visitor.paint.MapPainter;
|
---|
13 | import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
|
---|
14 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Text style attached to a style with a bounding box, like an icon or a symbol.
|
---|
18 | */
|
---|
19 | public class BoxTextElemStyle extends ElemStyle {
|
---|
20 |
|
---|
21 | public enum HorizontalTextAlignment { LEFT, CENTER, RIGHT }
|
---|
22 | public enum VerticalTextAlignment { ABOVE, TOP, CENTER, BOTTOM, BELOW }
|
---|
23 |
|
---|
24 | public static final Rectangle ZERO_BOX = new Rectangle(0, 0, 0, 0);
|
---|
25 |
|
---|
26 | public TextElement text;
|
---|
27 | public Rectangle box;
|
---|
28 | public HorizontalTextAlignment hAlign;
|
---|
29 | public VerticalTextAlignment vAlign;
|
---|
30 |
|
---|
31 | public BoxTextElemStyle(Cascade c, TextElement text, Rectangle box, HorizontalTextAlignment hAlign, VerticalTextAlignment vAlign) {
|
---|
32 | super(c, 2000f);
|
---|
33 | CheckParameterUtil.ensureParameterNotNull(text);
|
---|
34 | CheckParameterUtil.ensureParameterNotNull(hAlign);
|
---|
35 | CheckParameterUtil.ensureParameterNotNull(vAlign);
|
---|
36 | this.text = text;
|
---|
37 | this.box = box == null ? ZERO_BOX : box;
|
---|
38 | this.hAlign = hAlign;
|
---|
39 | this.vAlign = vAlign;
|
---|
40 | }
|
---|
41 |
|
---|
42 | public static BoxTextElemStyle create(Environment env, Rectangle box) {
|
---|
43 | initDefaultParameters();
|
---|
44 | Cascade c = env.mc.getCascade(env.layer);
|
---|
45 |
|
---|
46 | TextElement text = TextElement.create(c, DEFAULT_TEXT_COLOR, false);
|
---|
47 | if (text == null) return null;
|
---|
48 | // Skip any primtives that don't have text to draw. (Styles are recreated for any tag change.)
|
---|
49 | // The concrete text to render is not cached in this object, but computed for each
|
---|
50 | // repaint. This way, one BoxTextElemStyle object can be used by multiple primitives (to save memory).
|
---|
51 | if (text.labelCompositionStrategy.compose(env.osm) == null) return null;
|
---|
52 |
|
---|
53 | HorizontalTextAlignment hAlign = HorizontalTextAlignment.RIGHT;
|
---|
54 | Keyword hAlignKW = c.get("text-anchor-horizontal", Keyword.RIGHT, Keyword.class);
|
---|
55 | if (equal(hAlignKW.val, "left")) {
|
---|
56 | hAlign = HorizontalTextAlignment.LEFT;
|
---|
57 | } else if (equal(hAlignKW.val, "center")) {
|
---|
58 | hAlign = HorizontalTextAlignment.CENTER;
|
---|
59 | } else if (equal(hAlignKW.val, "right")) {
|
---|
60 | hAlign = HorizontalTextAlignment.RIGHT;
|
---|
61 | }
|
---|
62 | VerticalTextAlignment vAlign = VerticalTextAlignment.BOTTOM;
|
---|
63 | String vAlignStr = c.get("text-anchor-vertical", Keyword.BOTTOM, Keyword.class).val;
|
---|
64 | if (equal(vAlignStr, "above")) {
|
---|
65 | vAlign = VerticalTextAlignment.ABOVE;
|
---|
66 | } else if (equal(vAlignStr, "top")) {
|
---|
67 | vAlign = VerticalTextAlignment.TOP;
|
---|
68 | } else if (equal(vAlignStr, "center")) {
|
---|
69 | vAlign = VerticalTextAlignment.CENTER;
|
---|
70 | } else if (equal(vAlignStr, "bottom")) {
|
---|
71 | vAlign = VerticalTextAlignment.BOTTOM;
|
---|
72 | } else if (equal(vAlignStr, "below")) {
|
---|
73 | vAlign = VerticalTextAlignment.BELOW;
|
---|
74 | }
|
---|
75 |
|
---|
76 | return new BoxTextElemStyle(c, text, box, hAlign, vAlign);
|
---|
77 | }
|
---|
78 |
|
---|
79 | public static final BoxTextElemStyle SIMPLE_NODE_TEXT_ELEMSTYLE;
|
---|
80 | static {
|
---|
81 | MultiCascade mc = new MultiCascade();
|
---|
82 | Cascade c = mc.getOrCreateCascade("default");
|
---|
83 | c.put("text", Keyword.AUTO);
|
---|
84 | Node n = new Node();
|
---|
85 | n.put("name", "dummy");
|
---|
86 | SIMPLE_NODE_TEXT_ELEMSTYLE = create(new Environment(n, mc, "default", null), NodeElemStyle.SIMPLE_NODE_ELEMSTYLE.getBox());
|
---|
87 | if (SIMPLE_NODE_TEXT_ELEMSTYLE == null) throw new AssertionError();
|
---|
88 | }
|
---|
89 | /*
|
---|
90 | * Caches the default text color from the preferences.
|
---|
91 | *
|
---|
92 | * FIXME: the cache isn't updated if the user changes the preference during a JOSM
|
---|
93 | * session. There should be preference listener updating this cache.
|
---|
94 | */
|
---|
95 | static private Color DEFAULT_TEXT_COLOR = null;
|
---|
96 | static private void initDefaultParameters() {
|
---|
97 | if (DEFAULT_TEXT_COLOR != null) return;
|
---|
98 | DEFAULT_TEXT_COLOR = PaintColors.TEXT.get();
|
---|
99 | }
|
---|
100 |
|
---|
101 | @Override
|
---|
102 | public void paintPrimitive(OsmPrimitive osm, MapPaintSettings settings, MapPainter painter, boolean selected, boolean member) {
|
---|
103 | if (osm instanceof Node) {
|
---|
104 | painter.drawBoxText((Node) osm, this);
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | @Override
|
---|
109 | public boolean equals(Object obj) {
|
---|
110 | if (!super.equals(obj))
|
---|
111 | return false;
|
---|
112 | if (obj == null || getClass() != obj.getClass())
|
---|
113 | return false;
|
---|
114 | final BoxTextElemStyle other = (BoxTextElemStyle) obj;
|
---|
115 | return text.equals(other.text) &&
|
---|
116 | box.equals(other.box) &&
|
---|
117 | hAlign == other.hAlign &&
|
---|
118 | vAlign == other.vAlign;
|
---|
119 | }
|
---|
120 |
|
---|
121 | @Override
|
---|
122 | public int hashCode() {
|
---|
123 | int hash = super.hashCode();
|
---|
124 | hash = 97 * hash + text.hashCode();
|
---|
125 | hash = 97 * hash + box.hashCode();
|
---|
126 | hash = 97 * hash + hAlign.hashCode();
|
---|
127 | hash = 97 * hash + vAlign.hashCode();
|
---|
128 | return hash;
|
---|
129 | }
|
---|
130 |
|
---|
131 | @Override
|
---|
132 | public String toString() {
|
---|
133 | return "BoxTextElemStyle{" + super.toString() + " " + text.toStringImpl() + " box=" + box + " hAlign=" + hAlign + " vAlign=" + vAlign + '}';
|
---|
134 | }
|
---|
135 |
|
---|
136 | }
|
---|