source: josm/trunk/src/org/openstreetmap/josm/tools/TaggingPresetNameTemplateList.java@ 6340

Last change on this file since 6340 was 6068, checked in by akks, 11 years ago

see #8853: Massive (and dumb) refactoring of TaggingPreset class (splitting into smaller files)
Separate preset-choosing panel (TaggingPresetSelector class) for reuse not only in F3.

File size: 1.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import java.util.Collection;
5import java.util.Collections;
6import java.util.LinkedList;
7import java.util.List;
8
9import org.openstreetmap.josm.data.osm.OsmPrimitive;
10import org.openstreetmap.josm.gui.preferences.map.TaggingPresetPreference;
11import org.openstreetmap.josm.gui.tagging.TaggingPreset;
12import org.openstreetmap.josm.gui.tagging.TaggingPresetType;
13
14/**
15 * List of tagging presets with name templates, allows to find appropriate template based on existing primitive
16 */
17public class TaggingPresetNameTemplateList {
18
19 private static TaggingPresetNameTemplateList instance;
20
21 public static TaggingPresetNameTemplateList getInstance() {
22 if (instance == null) {
23 instance = new TaggingPresetNameTemplateList();
24 }
25 return instance;
26 }
27 private final List<TaggingPreset> presetsWithPattern = new LinkedList<TaggingPreset>();
28
29 private TaggingPresetNameTemplateList() {
30 if (TaggingPresetPreference.taggingPresets != null) {
31 for (TaggingPreset tp : TaggingPresetPreference.taggingPresets) {
32 if (tp.nameTemplate != null) {
33 presetsWithPattern.add(tp);
34 }
35 }
36 }
37 }
38
39 public TaggingPreset findPresetTemplate(OsmPrimitive primitive) {
40
41 for (TaggingPreset t : presetsWithPattern) {
42 Collection<TaggingPresetType> type = Collections.singleton(TaggingPresetType.forPrimitive(primitive));
43 if (t.typeMatches(type)) {
44 if (t.nameTemplateFilter != null) {
45 if (t.nameTemplateFilter.match(primitive))
46 return t;
47 else {
48 continue;
49 }
50 } else if (t.matches(type, primitive.getKeys(), false)) {
51 return t;
52 }
53 }
54 }
55 return null;
56 }
57}
Note: See TracBrowser for help on using the repository browser.