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

Last change on this file since 7005 was 7005, checked in by Don-vip, 10 years ago

see #8465 - use diamond operator where applicable

  • Property svn:eol-style set to native
File size: 10.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 * options <tt>mappaint.nameOrder</tt> and <tt>mappaint.nameComplementOrder</tt>.</li>
32 * </ul>
33 *
34 */
35public abstract class LabelCompositionStrategy {
36
37 /**
38 * Replies the text value to be rendered as label for the primitive {@code primitive}.
39 *
40 * @param primitive the primitive
41 *
42 * @return the text value to be rendered or null, if primitive is null or
43 * if no suitable value could be composed
44 */
45 public abstract String compose(OsmPrimitive primitive);
46
47 public static class StaticLabelCompositionStrategy extends LabelCompositionStrategy {
48 private String defaultLabel;
49
50 public StaticLabelCompositionStrategy(String defaultLabel){
51 this.defaultLabel = defaultLabel;
52 }
53
54 @Override
55 public String compose(OsmPrimitive primitive) {
56 return defaultLabel;
57 }
58
59 public String getDefaultLabel() {
60 return defaultLabel;
61 }
62
63 @Override
64 public String toString() {
65 return "{" + getClass().getSimpleName() + " defaultLabel=" + defaultLabel + "}";
66 }
67
68 @Override
69 public int hashCode() {
70 final int prime = 31;
71 int result = 1;
72 result = prime * result + ((defaultLabel == null) ? 0 : defaultLabel.hashCode());
73 return result;
74 }
75
76 @Override
77 public boolean equals(Object obj) {
78 if (this == obj)
79 return true;
80 if (obj == null)
81 return false;
82 if (getClass() != obj.getClass())
83 return false;
84 StaticLabelCompositionStrategy other = (StaticLabelCompositionStrategy) obj;
85 if (defaultLabel == null) {
86 if (other.defaultLabel != null)
87 return false;
88 } else if (!defaultLabel.equals(other.defaultLabel))
89 return false;
90 return true;
91 }
92 }
93
94 public static class TagLookupCompositionStrategy extends LabelCompositionStrategy {
95
96 private String defaultLabelTag;
97 public TagLookupCompositionStrategy(String defaultLabelTag){
98 if (defaultLabelTag != null) {
99 defaultLabelTag = defaultLabelTag.trim();
100 if (defaultLabelTag.isEmpty()) {
101 defaultLabelTag = null;
102 }
103 }
104 this.defaultLabelTag = defaultLabelTag;
105 }
106
107 @Override
108 public String compose(OsmPrimitive primitive) {
109 if (defaultLabelTag == null) return null;
110 if (primitive == null) return null;
111 return primitive.get(defaultLabelTag);
112 }
113
114 public String getDefaultLabelTag() {
115 return defaultLabelTag;
116 }
117
118 @Override
119 public String toString() {
120 return "{" + getClass().getSimpleName() + " defaultLabelTag=" + defaultLabelTag + "}";
121 }
122
123 @Override
124 public int hashCode() {
125 final int prime = 31;
126 int result = 1;
127 result = prime * result + ((defaultLabelTag == null) ? 0 : defaultLabelTag.hashCode());
128 return result;
129 }
130
131 @Override
132 public boolean equals(Object obj) {
133 if (this == obj)
134 return true;
135 if (obj == null)
136 return false;
137 if (getClass() != obj.getClass())
138 return false;
139 TagLookupCompositionStrategy other = (TagLookupCompositionStrategy) obj;
140 if (defaultLabelTag == null) {
141 if (other.defaultLabelTag != null)
142 return false;
143 } else if (!defaultLabelTag.equals(other.defaultLabelTag))
144 return false;
145 return true;
146 }
147 }
148
149 public static class DeriveLabelFromNameTagsCompositionStrategy extends LabelCompositionStrategy {
150
151 /**
152 * The list of default name tags from which a label candidate is derived.
153 */
154 private static final String[] DEFAULT_NAME_TAGS = {
155 "name:" + LanguageInfo.getJOSMLocaleCode(),
156 "name",
157 "int_name",
158 "ref",
159 "operator",
160 "brand",
161 "addr:housenumber"
162 };
163
164 /**
165 * The list of default name complement tags from which a label candidate is derived.
166 */
167 private static final String[] DEFAULT_NAME_COMPLEMENT_TAGS = {
168 "capacity"
169 };
170
171 private List<String> nameTags = new ArrayList<>();
172 private List<String> nameComplementTags = new ArrayList<>();
173
174 /**
175 * <p>Creates the strategy and initializes its name tags from the preferences.</p>
176 *
177 * <p><strong>Note:</strong> If the list of name tags in the preferences changes, strategy instances
178 * are not notified. It's up to the client to listen to preference changes and
179 * invoke {@link #initNameTagsFromPreferences()} accordingly.</p>
180 *
181 */
182 public DeriveLabelFromNameTagsCompositionStrategy() {
183 initNameTagsFromPreferences();
184 }
185
186 private static List<String> buildNameTags(List<String> nameTags) {
187 if (nameTags == null) {
188 nameTags = Collections.emptyList();
189 }
190 ArrayList<String> result = new ArrayList<>();
191 for(String tag: nameTags) {
192 if (tag == null) {
193 continue;
194 }
195 tag = tag.trim();
196 if (tag.isEmpty()) {
197 continue;
198 }
199 result.add(tag);
200 }
201 return result;
202 }
203
204 /**
205 * Sets the name tags to be looked up in order to build up the label.
206 *
207 * @param nameTags the name tags. null values are ignored.
208 */
209 public void setNameTags(List<String> nameTags){
210 this.nameTags = buildNameTags(nameTags);
211 }
212
213 /**
214 * Sets the name complement tags to be looked up in order to build up the label.
215 *
216 * @param nameComplementTags the name complement tags. null values are ignored.
217 * @since 6541
218 */
219 public void setNameComplementTags(List<String> nameComplementTags){
220 this.nameComplementTags = buildNameTags(nameComplementTags);
221 }
222
223 /**
224 * Replies an unmodifiable list of the name tags used to compose the label.
225 *
226 * @return the list of name tags
227 */
228 public List<String> getNameTags() {
229 return Collections.unmodifiableList(nameTags);
230 }
231
232 /**
233 * Replies an unmodifiable list of the name complement tags used to compose the label.
234 *
235 * @return the list of name complement tags
236 * @since 6541
237 */
238 public List<String> getNameComplementTags() {
239 return Collections.unmodifiableList(nameComplementTags);
240 }
241
242 /**
243 * Initializes the name tags to use from a list of default name tags (see
244 * {@link #DEFAULT_NAME_TAGS} and {@link #DEFAULT_NAME_COMPLEMENT_TAGS})
245 * and from name tags configured in the preferences using the keys
246 * <tt>mappaint.nameOrder</tt> and <tt>mappaint.nameComplementOrder</tt>.
247 */
248 public final void initNameTagsFromPreferences() {
249 if (Main.pref == null){
250 this.nameTags = new ArrayList<>(Arrays.asList(DEFAULT_NAME_TAGS));
251 this.nameComplementTags = new ArrayList<>(Arrays.asList(DEFAULT_NAME_COMPLEMENT_TAGS));
252 } else {
253 this.nameTags = new ArrayList<>(
254 Main.pref.getCollection("mappaint.nameOrder", Arrays.asList(DEFAULT_NAME_TAGS))
255 );
256 this.nameComplementTags = new ArrayList<>(
257 Main.pref.getCollection("mappaint.nameComplementOrder", Arrays.asList(DEFAULT_NAME_COMPLEMENT_TAGS))
258 );
259 }
260 }
261
262 private String getPrimitiveName(OsmPrimitive n) {
263 String name = null;
264 if (!n.hasKeys()) return null;
265 for (String rn : nameTags) {
266 name = n.get(rn);
267 if (name != null) {
268 break;
269 }
270 }
271 for (String rn : nameComplementTags) {
272 String comp = n.get(rn);
273 if (comp != null) {
274 if (name == null) {
275 name = comp;
276 } else {
277 name += " (" + comp + ")";
278 }
279 break;
280 }
281 }
282 return name;
283 }
284
285 @Override
286 public String compose(OsmPrimitive primitive) {
287 if (primitive == null) return null;
288 return getPrimitiveName(primitive);
289 }
290
291 @Override
292 public String toString() {
293 return "{" + getClass().getSimpleName() +"}";
294 }
295 }
296}
Note: See TracBrowser for help on using the repository browser.