source: josm/trunk/src/org/openstreetmap/josm/gui/tagging/ac/AutoCompletionManager.java@ 4223

Last change on this file since 4223 was 4223, checked in by stoecker, 13 years ago

fix #6570 (revert change in r4215, see #6547), i18n update, fix typo in function names

  • Property svn:eol-style set to native
File size: 11.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.tagging.ac;
3
4import java.util.ArrayList;
5import java.util.Collection;
6import java.util.Collections;
7import java.util.HashSet;
8import java.util.List;
9import java.util.Map;
10import java.util.Set;
11import java.util.Map.Entry;
12
13import org.openstreetmap.josm.data.osm.DataSet;
14import org.openstreetmap.josm.data.osm.OsmPrimitive;
15import org.openstreetmap.josm.data.osm.OsmUtils;
16import org.openstreetmap.josm.data.osm.Relation;
17import org.openstreetmap.josm.data.osm.RelationMember;
18import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent;
19import org.openstreetmap.josm.data.osm.event.DataChangedEvent;
20import org.openstreetmap.josm.data.osm.event.DataSetListener;
21import org.openstreetmap.josm.data.osm.event.NodeMovedEvent;
22import org.openstreetmap.josm.data.osm.event.PrimitivesAddedEvent;
23import org.openstreetmap.josm.data.osm.event.PrimitivesRemovedEvent;
24import org.openstreetmap.josm.data.osm.event.RelationMembersChangedEvent;
25import org.openstreetmap.josm.data.osm.event.TagsChangedEvent;
26import org.openstreetmap.josm.data.osm.event.WayNodesChangedEvent;
27import org.openstreetmap.josm.gui.tagging.TaggingPreset;
28import org.openstreetmap.josm.tools.MultiMap;
29
30/**
31 * AutoCompletionManager holds a cache of keys with a list of
32 * possible auto completion values for each key.
33 *
34 * Each DataSet is assigned one AutoCompletionManager instance such that
35 * <ol>
36 * <li>any key used in a tag in the data set is part of the key list in the cache</li>
37 * <li>any value used in a tag for a specific key is part of the autocompletion list of
38 * this key</li>
39 * </ol>
40 *
41 * Building up auto completion lists should not
42 * slow down tabbing from input field to input field. Looping through the complete
43 * data set in order to build up the auto completion list for a specific input
44 * field is not efficient enough, hence this cache.
45 *
46 * TODO: respect the relation type for member role autocompletion
47 */
48public class AutoCompletionManager implements DataSetListener {
49
50 /** If the dirty flag is set true, a rebuild is necessary. */
51 protected boolean dirty;
52 /** The data set that is managed */
53 protected DataSet ds;
54
55 /**
56 * the cached tags given by a tag key and a list of values for this tag
57 * only accessed by getTagCache(), rebuild() and cachePrimitiveTags()
58 * use getTagCache() accessor
59 */
60 protected MultiMap<String, String> tagCache;
61 /**
62 * the same as tagCache but for the preset keys and values
63 * can be accessed directly
64 */
65 protected static MultiMap<String, String> presetTagCache = new MultiMap<String, String>();
66 /**
67 * the cached list of member roles
68 * only accessed by getRoleCache(), rebuild() and cacheRelationMemberRoles()
69 * use getRoleCache() accessor
70 */
71 protected Set<String> roleCache;
72 /**
73 * the same as roleCache but for the preset roles
74 * can be accessed directly
75 */
76 protected static Set<String> presetRoleCache = new HashSet<String>();
77
78 public AutoCompletionManager(DataSet ds) {
79 this.ds = ds;
80 dirty = true;
81 }
82
83 protected MultiMap<String, String> getTagCache() {
84 if (dirty) {
85 rebuild();
86 dirty = false;
87 }
88 return tagCache;
89 }
90
91 protected Set<String> getRoleCache() {
92 if (dirty) {
93 rebuild();
94 dirty = false;
95 }
96 return roleCache;
97 }
98
99 /**
100 * initializes the cache from the primitives in the dataset
101 *
102 */
103 protected void rebuild() {
104 tagCache = new MultiMap<String, String>();
105 roleCache = new HashSet<String>();
106 cachePrimitives(ds.allNonDeletedCompletePrimitives());
107 }
108
109 protected void cachePrimitives(Collection<? extends OsmPrimitive> primitives) {
110 for (OsmPrimitive primitive : primitives) {
111 cachePrimitiveTags(primitive);
112 if (primitive instanceof Relation) {
113 cacheRelationMemberRoles((Relation) primitive);
114 }
115 }
116 }
117
118 /**
119 * make sure, the keys and values of all tags held by primitive are
120 * in the auto completion cache
121 *
122 * @param primitive an OSM primitive
123 */
124 protected void cachePrimitiveTags(OsmPrimitive primitive) {
125 for (String key: primitive.keySet()) {
126 String value = primitive.get(key);
127 tagCache.put(key, value);
128 }
129 }
130
131 /**
132 * Caches all member roles of the relation <code>relation</code>
133 *
134 * @param relation the relation
135 */
136 protected void cacheRelationMemberRoles(Relation relation){
137 for (RelationMember m: relation.getMembers()) {
138 if (m.hasRole()) {
139 roleCache.add(m.getRole());
140 }
141 }
142 }
143
144 /**
145 * Initialize the cache for presets. This is done only once.
146 */
147 public static void cachePresets(Collection<TaggingPreset> presets) {
148 for (final TaggingPreset p : presets) {
149 for (TaggingPreset.Item item : p.data) {
150 if (item instanceof TaggingPreset.Check) {
151 TaggingPreset.Check ch = (TaggingPreset.Check) item;
152 if (ch.key == null) {
153 continue;
154 }
155 presetTagCache.put(ch.key, OsmUtils.falseval);
156 presetTagCache.put(ch.key, OsmUtils.trueval);
157 } else if (item instanceof TaggingPreset.Combo) {
158 TaggingPreset.Combo co = (TaggingPreset.Combo) item;
159 if (co.key == null || co.values == null) {
160 continue;
161 }
162 for (String value : co.values.split(",")) {
163 presetTagCache.put(co.key, value);
164 }
165 } else if (item instanceof TaggingPreset.Key) {
166 TaggingPreset.Key ky = (TaggingPreset.Key) item;
167 if (ky.key == null || ky.value == null) {
168 continue;
169 }
170 presetTagCache.put(ky.key, ky.value);
171 } else if (item instanceof TaggingPreset.Text) {
172 TaggingPreset.Text tt = (TaggingPreset.Text) item;
173 if (tt.key == null) {
174 continue;
175 }
176 presetTagCache.putVoid(tt.key);
177 if (tt.default_ != null && !tt.default_.equals("")) {
178 presetTagCache.put(tt.key, tt.default_);
179 }
180 } else if (item instanceof TaggingPreset.Roles) {
181 TaggingPreset.Roles r = (TaggingPreset.Roles) item;
182 for (TaggingPreset.Role i : r.roles) {
183 if (i.key != null) {
184 presetRoleCache.add(i.key);
185 }
186 }
187 }
188 }
189 }
190 }
191
192 /**
193 * replies the keys held by the cache
194 *
195 * @return the list of keys held by the cache
196 */
197 protected List<String> getDataKeys() {
198 return new ArrayList<String>(getTagCache().keySet());
199 }
200
201 protected List<String> getPresetKeys() {
202 return new ArrayList<String>(presetTagCache.keySet());
203 }
204
205 /**
206 * replies the auto completion values allowed for a specific key. Replies
207 * an empty list if key is null or if key is not in {@link #getKeys()}.
208 *
209 * @param key
210 * @return the list of auto completion values
211 */
212 protected List<String> getDataValues(String key) {
213 return new ArrayList<String>(getTagCache().getValues(key));
214 }
215
216 protected static List<String> getPresetValues(String key) {
217 return new ArrayList<String>(presetTagCache.getValues(key));
218 }
219
220 /**
221 * Replies the list of member roles
222 *
223 * @return the list of member roles
224 */
225 public List<String> getMemberRoles() {
226 return new ArrayList<String>(getRoleCache());
227 }
228
229 /**
230 * Populates the an {@see AutoCompletionList} with the currently cached
231 * member roles.
232 *
233 * @param list the list to populate
234 */
235 public void populateWithMemberRoles(AutoCompletionList list) {
236 list.add(presetRoleCache, AutoCompletionItemPritority.IS_IN_STANDARD);
237 list.add(getRoleCache(), AutoCompletionItemPritority.IS_IN_DATASET);
238 }
239
240 /**
241 * Populates the an {@see AutoCompletionList} with the currently cached
242 * tag keys
243 *
244 * @param list the list to populate
245 * @param append true to add the keys to the list; false, to replace the keys
246 * in the list by the keys in the cache
247 */
248 public void populateWithKeys(AutoCompletionList list) {
249 list.add(getPresetKeys(), AutoCompletionItemPritority.IS_IN_STANDARD);
250 list.add(getDataKeys(), AutoCompletionItemPritority.IS_IN_DATASET);
251 }
252
253 /**
254 * Populates the an {@see AutoCompletionList} with the currently cached
255 * values for a tag
256 *
257 * @param list the list to populate
258 * @param key the tag key
259 * @param append true to add the values to the list; false, to replace the values
260 * in the list by the tag values
261 */
262 public void populateWithTagValues(AutoCompletionList list, String key) {
263 list.add(getPresetValues(key), AutoCompletionItemPritority.IS_IN_STANDARD);
264 list.add(getDataValues(key), AutoCompletionItemPritority.IS_IN_DATASET);
265 }
266
267 public List<AutoCompletionListItem> getKeys() {
268 AutoCompletionList list = new AutoCompletionList();
269 populateWithKeys(list);
270 return new ArrayList<AutoCompletionListItem>(list.getList());
271 }
272
273 public List<AutoCompletionListItem> getValues(String key) {
274 AutoCompletionList list = new AutoCompletionList();
275 populateWithTagValues(list, key);
276 return new ArrayList<AutoCompletionListItem>(list.getList());
277 }
278
279 /*********************************************************
280 * Implementation of the DataSetListener interface
281 *
282 **/
283
284 public void primitivesAdded(PrimitivesAddedEvent event) {
285 if (dirty)
286 return;
287 cachePrimitives(event.getPrimitives());
288 }
289
290 public void primitivesRemoved(PrimitivesRemovedEvent event) {
291 dirty = true;
292 }
293
294 public void tagsChanged(TagsChangedEvent event) {
295 if (dirty)
296 return;
297 Map<String, String> newKeys = event.getPrimitive().getKeys();
298 Map<String, String> oldKeys = event.getOriginalKeys();
299
300 if (!newKeys.keySet().containsAll(oldKeys.keySet())) {
301 // Some keys removed, might be the last instance of key, rebuild necessary
302 dirty = true;
303 } else {
304 for (Entry<String, String> oldEntry: oldKeys.entrySet()) {
305 if (!oldEntry.getValue().equals(newKeys.get(oldEntry.getKey()))) {
306 // Value changed, might be last instance of value, rebuild necessary
307 dirty = true;
308 return;
309 }
310 }
311 cachePrimitives(Collections.singleton(event.getPrimitive()));
312 }
313 }
314
315 public void nodeMoved(NodeMovedEvent event) {/* ignored */}
316
317 public void wayNodesChanged(WayNodesChangedEvent event) {/* ignored */}
318
319 public void relationMembersChanged(RelationMembersChangedEvent event) {
320 dirty = true; // TODO: not necessary to rebuid if a member is added
321 }
322
323 public void otherDatasetChange(AbstractDatasetChangedEvent event) {/* ignored */}
324
325 public void dataChanged(DataChangedEvent event) {
326 dirty = true;
327 }
328}
Note: See TracBrowser for help on using the repository browser.