Ticket #13956: 13956_v3.patch
File 13956_v3.patch, 15.0 KB (added by , 8 years ago) |
---|
-
src/org/openstreetmap/josm/data/validation/tests/TagChecker.java
68 68 69 69 /** Normalized keys: the key should be substituted by the value if the key was not found in presets */ 70 70 private static final Map<String, String> harmonizedKeys = new HashMap<>(); 71 /** The spell check preset values */72 private static volatile MultiMap<String, String> presetsValueData;71 /** The spell check preset values which are not stored in TaggingPresets */ 72 private static volatile MultiMap<String, String> additionalPresetsValueData; 73 73 /** The TagChecker data */ 74 74 private static final List<CheckerData> checkerData = new ArrayList<>(); 75 75 private static final List<String> ignoreDataStartsWith = new ArrayList<>(); … … 258 258 259 259 Collection<TaggingPreset> presets = TaggingPresets.getTaggingPresets(); 260 260 if (!presets.isEmpty()) { 261 presetsValueData = new MultiMap<>();261 additionalPresetsValueData = new MultiMap<>(); 262 262 for (String a : OsmPrimitive.getUninterestingKeys()) { 263 presetsValueData.putVoid(a);263 additionalPresetsValueData.putVoid(a); 264 264 } 265 265 // TODO directionKeys are no longer in OsmPrimitive (search pattern is used instead) 266 266 for (String a : Main.pref.getCollection(ValidatorPreference.PREFIX + ".knownkeys", 267 267 Arrays.asList(new String[]{"is_in", "int_ref", "fixme", "population"}))) { 268 presetsValueData.putVoid(a);268 additionalPresetsValueData.putVoid(a); 269 269 } 270 270 for (TaggingPreset p : presets) { 271 271 for (TaggingPresetItem i : p.data) { … … 285 285 Collection<String> values = ky.getValues(); 286 286 if (ky.key != null && values != null) { 287 287 try { 288 presetsValueData.putAll(ky.key, values);289 288 harmonizedKeys.put(harmonizeKey(ky.key), ky.key); 290 289 } catch (NullPointerException e) { 291 290 Main.error(e, p+": Unable to initialize "+ky+'.'); … … 308 307 return false; 309 308 } 310 309 310 private static Set<String> getPresetValues(String key) { 311 Set<String> res = TaggingPresets.getPresetValues(key); 312 if (res != null) 313 return res; 314 return additionalPresetsValueData.get(key); 315 } 316 311 317 /** 312 318 * Determines if the given key is in internal presets. 313 319 * @param key key … … 315 321 * @since 9023 316 322 */ 317 323 public static boolean isKeyInPresets(String key) { 318 return presetsValueData.get(key) != null;324 return getPresetValues(key) != null; 319 325 } 320 326 321 327 /** … … 326 332 * @since 9023 327 333 */ 328 334 public static boolean isTagInPresets(String key, String value) { 329 final Set<String> values = presetsValueData.get(key);335 final Set<String> values = getPresetValues(key); 330 336 return values != null && (values.isEmpty() || values.contains(value)); 331 337 } 332 338 … … 465 471 .build()); 466 472 withErrors.put(p, "HTML"); 467 473 } 468 if (checkValues && key != null && value != null && !value.isEmpty() && presetsValueData != null) {474 if (checkValues && key != null && value != null && !value.isEmpty() && additionalPresetsValueData != null) { 469 475 if (!isTagIgnored(key, value)) { 470 476 if (!isKeyInPresets(key)) { 471 477 String prettifiedKey = harmonizeKey(key); … … 491 497 } else if (!isTagInPresets(key, value)) { 492 498 // try to fix common typos and check again if value is still unknown 493 499 String fixedValue = harmonizeValue(prop.getValue()); 494 Map<String, String> possibleValues = getPossibleValues( presetsValueData.get(key));500 Map<String, String> possibleValues = getPossibleValues(getPresetValues(key)); 495 501 if (possibleValues.containsKey(fixedValue)) { 496 502 final String newKey = possibleValues.get(fixedValue); 497 503 // misspelled preset value -
src/org/openstreetmap/josm/gui/tagging/ac/AutoCompletionManager.java
14 14 import java.util.Set; 15 15 import java.util.function.Function; 16 16 17 import org.openstreetmap.josm.Main;18 17 import org.openstreetmap.josm.data.osm.DataSet; 19 18 import org.openstreetmap.josm.data.osm.OsmPrimitive; 20 19 import org.openstreetmap.josm.data.osm.Relation; … … 29 28 import org.openstreetmap.josm.data.osm.event.TagsChangedEvent; 30 29 import org.openstreetmap.josm.data.osm.event.WayNodesChangedEvent; 31 30 import org.openstreetmap.josm.gui.tagging.presets.TaggingPreset; 32 import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetItem;33 31 import org.openstreetmap.josm.gui.tagging.presets.TaggingPresets; 34 import org.openstreetmap.josm.gui.tagging.presets.items.CheckGroup;35 import org.openstreetmap.josm.gui.tagging.presets.items.KeyedItem;36 import org.openstreetmap.josm.gui.tagging.presets.items.Roles;37 32 import org.openstreetmap.josm.gui.tagging.presets.items.Roles.Role; 38 33 import org.openstreetmap.josm.tools.CheckParameterUtil; 39 34 import org.openstreetmap.josm.tools.MultiMap; … … 112 107 protected MultiMap<String, String> tagCache; 113 108 114 109 /** 115 * the same as tagCache but for the preset keys and values can be accessed directly116 */117 protected static final MultiMap<String, String> PRESET_TAG_CACHE = new MultiMap<>();118 119 /**120 110 * Cache for tags that have been entered by the user. 121 111 */ 122 112 protected static final Set<UserInputTag> USER_INPUT_TAG_CACHE = new LinkedHashSet<>(); … … 129 119 protected Set<String> roleCache; 130 120 131 121 /** 132 * the same as roleCache but for the preset roles can be accessed directly133 */134 protected static final Set<String> PRESET_ROLE_CACHE = new HashSet<>();135 136 /**137 122 * Constructs a new {@code AutoCompletionManager}. 138 123 * @param ds data set 139 124 */ … … 203 188 } 204 189 205 190 /** 206 * Initialize the cache for presets. This is done only once.207 * @param presets Tagging presets to cache208 */209 public static void cachePresets(Collection<TaggingPreset> presets) {210 for (final TaggingPreset p : presets) {211 for (TaggingPresetItem item : p.data) {212 cachePresetItem(p, item);213 }214 }215 }216 217 protected static void cachePresetItem(TaggingPreset p, TaggingPresetItem item) {218 if (item instanceof KeyedItem) {219 KeyedItem ki = (KeyedItem) item;220 if (ki.key != null && ki.getValues() != null) {221 try {222 PRESET_TAG_CACHE.putAll(ki.key, ki.getValues());223 } catch (NullPointerException e) {224 Main.error(e, p + ": Unable to cache " + ki);225 }226 }227 } else if (item instanceof Roles) {228 Roles r = (Roles) item;229 for (Role i : r.roles) {230 if (i.key != null) {231 PRESET_ROLE_CACHE.add(i.key);232 }233 }234 } else if (item instanceof CheckGroup) {235 for (KeyedItem check : ((CheckGroup) item).checks) {236 cachePresetItem(p, check);237 }238 }239 }240 241 /**242 191 * Remembers user input for the given key/value. 243 192 * @param key Tag key 244 193 * @param value Tag value … … 259 208 return new ArrayList<>(getTagCache().keySet()); 260 209 } 261 210 262 protected List<String> getPresetKeys() {263 return new ArrayList<>(PRESET_TAG_CACHE.keySet());264 }265 266 211 protected Collection<String> getUserInputKeys() { 267 212 List<String> keys = new ArrayList<>(); 268 213 for (UserInputTag tag : USER_INPUT_TAG_CACHE) { … … 285 230 return new ArrayList<>(getTagCache().getValues(key)); 286 231 } 287 232 288 protected static List<String> getPresetValues(String key) {289 return new ArrayList<>(PRESET_TAG_CACHE.getValues(key));290 }291 292 233 protected static Collection<String> getUserInputValues(String key) { 293 234 List<String> values = new ArrayList<>(); 294 235 for (UserInputTag tag : USER_INPUT_TAG_CACHE) { … … 316 257 * @param list the list to populate 317 258 */ 318 259 public void populateWithMemberRoles(AutoCompletionList list) { 319 list.add( PRESET_ROLE_CACHE, AutoCompletionItemPriority.IS_IN_STANDARD);260 list.add(TaggingPresets.getPresetRoles(), AutoCompletionItemPriority.IS_IN_STANDARD); 320 261 list.add(getRoleCache(), AutoCompletionItemPriority.IS_IN_DATASET); 321 262 } 322 263 … … 351 292 * @param list the list to populate 352 293 */ 353 294 public void populateWithKeys(AutoCompletionList list) { 354 list.add( getPresetKeys(), AutoCompletionItemPriority.IS_IN_STANDARD);295 list.add(TaggingPresets.getPresetKeys(), AutoCompletionItemPriority.IS_IN_STANDARD); 355 296 list.add(new AutoCompletionListItem("source", AutoCompletionItemPriority.IS_IN_STANDARD)); 356 297 list.add(getDataKeys(), AutoCompletionItemPriority.IS_IN_DATASET); 357 298 list.addUserInput(getUserInputKeys()); … … 377 318 */ 378 319 public void populateWithTagValues(AutoCompletionList list, List<String> keys) { 379 320 for (String key : keys) { 380 list.add( getPresetValues(key), AutoCompletionItemPriority.IS_IN_STANDARD);321 list.add(TaggingPresets.getPresetValues(key), AutoCompletionItemPriority.IS_IN_STANDARD); 381 322 list.add(getDataValues(key), AutoCompletionItemPriority.IS_IN_DATASET); 382 323 list.addUserInput(getUserInputValues(key)); 383 324 } -
src/org/openstreetmap/josm/gui/tagging/presets/TaggingPresets.java
3 3 4 4 import java.util.ArrayList; 5 5 import java.util.Collection; 6 import java.util.Collections; 6 7 import java.util.HashMap; 8 import java.util.HashSet; 9 import java.util.List; 7 10 import java.util.Map; 11 import java.util.Set; 8 12 9 13 import javax.swing.JMenu; 10 14 import javax.swing.JMenuItem; … … 13 17 import org.openstreetmap.josm.Main; 14 18 import org.openstreetmap.josm.data.osm.OsmPrimitive; 15 19 import org.openstreetmap.josm.gui.MenuScroller; 16 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager; 20 import org.openstreetmap.josm.gui.tagging.presets.items.CheckGroup; 21 import org.openstreetmap.josm.gui.tagging.presets.items.KeyedItem; 22 import org.openstreetmap.josm.gui.tagging.presets.items.Roles; 23 import org.openstreetmap.josm.gui.tagging.presets.items.Roles.Role; 24 import org.openstreetmap.josm.tools.MultiMap; 17 25 import org.openstreetmap.josm.tools.SubclassFilteredCollection; 18 26 19 27 /** … … 25 33 /** The collection of tagging presets */ 26 34 private static final Collection<TaggingPreset> taggingPresets = new ArrayList<>(); 27 35 36 /** cache for key/value pairs found in the preset */ 37 private static final MultiMap<String, String> PRESET_TAG_CACHE = new MultiMap<>(); 38 /** cache for roles found in the preset */ 39 private static final Set<String> PRESET_ROLE_CACHE = new HashSet<>(); 40 28 41 /** The collection of listeners */ 29 42 private static final Collection<TaggingPresetListener> listeners = new ArrayList<>(); 30 43 … … 38 51 public static void readFromPreferences() { 39 52 taggingPresets.clear(); 40 53 taggingPresets.addAll(TaggingPresetReader.readFromPreferences(false, false)); 54 cachePresets(taggingPresets); 41 55 } 42 56 43 57 /** … … 53 67 if (taggingPresets.isEmpty()) { 54 68 Main.main.menu.presetsMenu.setVisible(false); 55 69 } else { 56 AutoCompletionManager.cachePresets(taggingPresets);57 70 Map<TaggingPresetMenu, JMenu> submenus = new HashMap<>(); 58 71 for (final TaggingPreset p : taggingPresets) { 59 72 JMenu m = p.group != null ? submenus.get(p.group) : Main.main.menu.presetsMenu; … … 87 100 } 88 101 89 102 /** 103 * Initialize the cache for presets. This is done only once. 104 * @param presets Tagging presets to cache 105 */ 106 private static void cachePresets(Collection<TaggingPreset> presets) { 107 for (final TaggingPreset p : presets) { 108 for (TaggingPresetItem item : p.data) { 109 cachePresetItem(p, item); 110 } 111 } 112 } 113 114 private static void cachePresetItem(TaggingPreset p, TaggingPresetItem item) { 115 if (item instanceof KeyedItem) { 116 KeyedItem ki = (KeyedItem) item; 117 if (ki.key != null && ki.getValues() != null) { 118 try { 119 PRESET_TAG_CACHE.putAll(ki.key, ki.getValues()); 120 } catch (NullPointerException e) { 121 Main.error(e, p + ": Unable to cache " + ki); 122 } 123 } 124 } else if (item instanceof Roles) { 125 Roles r = (Roles) item; 126 for (Role i : r.roles) { 127 if (i.key != null) { 128 PRESET_ROLE_CACHE.add(i.key); 129 } 130 } 131 } else if (item instanceof CheckGroup) { 132 for (KeyedItem check : ((CheckGroup) item).checks) { 133 cachePresetItem(p, check); 134 } 135 } 136 } 137 138 /** 90 139 * Replies a new collection containing all tagging presets. 91 140 * @return a new collection containing all tagging presets. Empty if presets are not initialized (never null) 92 141 */ … … 95 144 } 96 145 97 146 /** 147 * Replies a list of all roles in the tagging presets. 148 * @return a list of all roles in the tagging presets. 149 */ 150 public static Set<String> getPresetRoles() { 151 return Collections.unmodifiableSet(PRESET_ROLE_CACHE); 152 } 153 154 /** 155 * Replies a list of all keys in the tagging presets. 156 * @return a list of all keys in the tagging presets. 157 */ 158 public static List<String> getPresetKeys() { 159 return new ArrayList<>(PRESET_TAG_CACHE.keySet()); 160 } 161 162 /** 163 * Return set of values for a key in the tagging presets 164 * @param key the key 165 * @return set of values for a key in the tagging presets or null if none is found 166 */ 167 public static Set<String> getPresetValues(String key) { 168 Set<String> values = PRESET_TAG_CACHE.get(key); 169 if (values != null) 170 return Collections.unmodifiableSet(values); 171 return null; 172 } 173 174 /** 98 175 * Replies a new collection of all presets matching the parameters. 99 176 * 100 177 * @param t the preset types to include