source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/LabelCompositionStrategy.java@ 6070

Last change on this file since 6070 was 6070, checked in by stoecker, 11 years ago

see #8853 remove tabs, trailing spaces, windows line ends, strange characters

  • Property svn:eol-style set to native
File size: 8.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint;
3
4import java.util.ArrayList;
5import java.util.Arrays;
6import java.util.Collections;
7import java.util.List;
8
9import org.openstreetmap.josm.Main;
10import org.openstreetmap.josm.data.osm.OsmPrimitive;
11import org.openstreetmap.josm.tools.LanguageInfo;
12
13/**
14 * <p>Provides an abstract parent class and three concrete sub classes for various
15 * strategies on how to compose the text label which can be rendered close to a node
16 * or within an area in an OSM map.</p>
17 *
18 * <p>The three strategies below support three rules for composing a label:
19 * <ul>
20 * <li>{@link StaticLabelCompositionStrategy} - the label is given by a static text
21 * specified in the MapCSS style file</li>
22 *
23 * <li>{@link TagLookupCompositionStrategy} - the label is given by the content of a
24 * tag whose name specified in the MapCSS style file</li>
25 *
26 * <li>{@link DeriveLabelFromNameTagsCompositionStrategy} - the label is given by the value
27 * of one
28 * of the configured "name tags". The list of relevant name tags can be configured
29 * in the JOSM preferences
30 * content of a tag whose name specified in the MapCSS style file, see the preference
31 * option <tt>mappaint.nameOrder</tt>.</li>
32 * </ul>
33 * </p>
34 *
35 */
36public abstract class LabelCompositionStrategy {
37
38 /**
39 * Replies the text value to be rendered as label for the primitive {@code primitive}.
40 *
41 * @param primitive the primitive
42 *
43 * @return the text value to be rendered or null, if primitive is null or
44 * if no suitable value could be composed
45 */
46 abstract public String compose(OsmPrimitive primitive);
47
48 static public class StaticLabelCompositionStrategy extends LabelCompositionStrategy {
49 private String defaultLabel;
50
51 public StaticLabelCompositionStrategy(String defaultLabel){
52 this.defaultLabel = defaultLabel;
53 }
54
55 @Override
56 public String compose(OsmPrimitive primitive) {
57 return defaultLabel;
58 }
59
60 public String getDefaultLabel() {
61 return defaultLabel;
62 }
63
64 @Override
65 public String toString() {
66 return "{" + getClass().getSimpleName() + " defaultLabel=" + defaultLabel + "}";
67 }
68
69 @Override
70 public int hashCode() {
71 final int prime = 31;
72 int result = 1;
73 result = prime * result + ((defaultLabel == null) ? 0 : defaultLabel.hashCode());
74 return result;
75 }
76
77 @Override
78 public boolean equals(Object obj) {
79 if (this == obj)
80 return true;
81 if (obj == null)
82 return false;
83 if (getClass() != obj.getClass())
84 return false;
85 StaticLabelCompositionStrategy other = (StaticLabelCompositionStrategy) obj;
86 if (defaultLabel == null) {
87 if (other.defaultLabel != null)
88 return false;
89 } else if (!defaultLabel.equals(other.defaultLabel))
90 return false;
91 return true;
92 }
93 }
94
95 static public class TagLookupCompositionStrategy extends LabelCompositionStrategy {
96
97 private String defaultLabelTag;
98 public TagLookupCompositionStrategy(String defaultLabelTag){
99 if (defaultLabelTag != null) {
100 defaultLabelTag = defaultLabelTag.trim();
101 if (defaultLabelTag.isEmpty()) {
102 defaultLabelTag = null;
103 }
104 }
105 this.defaultLabelTag = defaultLabelTag;
106 }
107
108 @Override
109 public String compose(OsmPrimitive primitive) {
110 if (defaultLabelTag == null) return null;
111 if (primitive == null) return null;
112 return primitive.get(defaultLabelTag);
113 }
114
115 public String getDefaultLabelTag() {
116 return defaultLabelTag;
117 }
118
119 @Override
120 public String toString() {
121 return "{" + getClass().getSimpleName() + " defaultLabelTag=" + defaultLabelTag + "}";
122 }
123
124 @Override
125 public int hashCode() {
126 final int prime = 31;
127 int result = 1;
128 result = prime * result + ((defaultLabelTag == null) ? 0 : defaultLabelTag.hashCode());
129 return result;
130 }
131
132 @Override
133 public boolean equals(Object obj) {
134 if (this == obj)
135 return true;
136 if (obj == null)
137 return false;
138 if (getClass() != obj.getClass())
139 return false;
140 TagLookupCompositionStrategy other = (TagLookupCompositionStrategy) obj;
141 if (defaultLabelTag == null) {
142 if (other.defaultLabelTag != null)
143 return false;
144 } else if (!defaultLabelTag.equals(other.defaultLabelTag))
145 return false;
146 return true;
147 }
148 }
149
150 static public class DeriveLabelFromNameTagsCompositionStrategy extends LabelCompositionStrategy {
151
152 /**
153 * The list of default name tags from which a label candidate is derived.
154 */
155 static public final String[] DEFAULT_NAME_TAGS = {
156 "name:" + LanguageInfo.getJOSMLocaleCode(),
157 "name",
158 "int_name",
159 "ref",
160 "operator",
161 "brand",
162 "addr:housenumber"
163 };
164
165 private List<String> nameTags = new ArrayList<String>();
166
167 /**
168 * <p>Creates the strategy and initializes its name tags from the preferences.</p>
169 *
170 * <p><strong>Note:</strong> If the list of name tags in the preferences changes, strategy instances
171 * are not notified. It's up to the client to listen to preference changes and
172 * invoke {@link #initNameTagsFromPreferences()} accordingly.</p>
173 *
174 */
175 public DeriveLabelFromNameTagsCompositionStrategy() {
176 initNameTagsFromPreferences();
177 }
178
179 /**
180 * Sets the name tags to be looked up in order to build up the label
181 *
182 * @param nameTags the name tags. null values are ignore.
183 */
184 public void setNameTags(List<String> nameTags){
185 if (nameTags == null) {
186 nameTags = Collections.emptyList();
187 }
188 this.nameTags = new ArrayList<String>();
189 for(String tag: nameTags) {
190 if (tag == null) {
191 continue;
192 }
193 tag = tag.trim();
194 if (tag.isEmpty()) {
195 continue;
196 }
197 this.nameTags.add(tag);
198 }
199 }
200
201 /**
202 * Replies an unmodifiable list of the name tags used to compose the label.
203 *
204 * @return the list of name tags
205 */
206 public List<String> getNameTags() {
207 return Collections.unmodifiableList(nameTags);
208 }
209
210 /**
211 * Initializes the name tags to use from a list of default name tags (see
212 * {@link #DEFAULT_NAME_TAGS}) and from name tags configured in the preferences
213 * using the preference key <tt>mappaint.nameOrder</tt>.
214 */
215 public void initNameTagsFromPreferences() {
216 if (Main.pref == null){
217 this.nameTags = new ArrayList<String>(Arrays.asList(DEFAULT_NAME_TAGS));
218 } else {
219 this.nameTags = new ArrayList<String>(
220 Main.pref.getCollection("mappaint.nameOrder", Arrays.asList(DEFAULT_NAME_TAGS))
221 );
222 }
223 }
224
225 private String getPrimitiveName(OsmPrimitive n) {
226 String name = null;
227 if (!n.hasKeys()) return null;
228 for (String rn : nameTags) {
229 name = n.get(rn);
230 if (name != null) return name;
231 }
232 return null;
233 }
234
235 @Override
236 public String compose(OsmPrimitive primitive) {
237 if (primitive == null) return null;
238 return getPrimitiveName(primitive);
239 }
240
241 @Override
242 public String toString() {
243 return "{" + getClass().getSimpleName() +"}";
244 }
245 }
246}
Note: See TracBrowser for help on using the repository browser.