1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.List;
|
---|
6 |
|
---|
7 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
8 | import org.openstreetmap.josm.data.osm.Way;
|
---|
9 | import org.openstreetmap.josm.gui.preferences.map.TaggingPresetPreference;
|
---|
10 | import org.openstreetmap.josm.gui.tagging.TaggingPreset;
|
---|
11 | import org.openstreetmap.josm.gui.tagging.TaggingPreset.Check;
|
---|
12 | import org.openstreetmap.josm.gui.tagging.TaggingPreset.Combo;
|
---|
13 | import org.openstreetmap.josm.gui.tagging.TaggingPreset.PresetType;
|
---|
14 | import org.openstreetmap.josm.gui.tagging.TaggingPreset.Text;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * List of tagging presets with name templates, allows to find appropriate template based on existing primitive
|
---|
18 | */
|
---|
19 | public class TaggingPresetNameTemplateList {
|
---|
20 |
|
---|
21 | private static TaggingPresetNameTemplateList instance;
|
---|
22 |
|
---|
23 | public static TaggingPresetNameTemplateList getInstance() {
|
---|
24 | if (instance == null) {
|
---|
25 | instance = new TaggingPresetNameTemplateList();
|
---|
26 | }
|
---|
27 | return instance;
|
---|
28 | }
|
---|
29 |
|
---|
30 | private final List<TaggingPreset> presetsWithPattern = new ArrayList<TaggingPreset>();
|
---|
31 |
|
---|
32 | private TaggingPresetNameTemplateList() {
|
---|
33 | if (TaggingPresetPreference.taggingPresets != null) {
|
---|
34 | for (TaggingPreset tp: TaggingPresetPreference.taggingPresets) {
|
---|
35 | if (tp.nameTemplate != null) {
|
---|
36 | presetsWithPattern.add(tp);
|
---|
37 | }
|
---|
38 | }
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | public TaggingPreset findPresetTemplate(OsmPrimitive primitive) {
|
---|
43 |
|
---|
44 | PresetType presetType;
|
---|
45 | switch (primitive.getType()) {
|
---|
46 | case NODE:
|
---|
47 | presetType = PresetType.NODE;
|
---|
48 | break;
|
---|
49 | case WAY:
|
---|
50 | if (((Way) primitive).isClosed()) {
|
---|
51 | presetType = PresetType.CLOSEDWAY;
|
---|
52 | } else {
|
---|
53 | presetType = PresetType.WAY;
|
---|
54 | }
|
---|
55 | break;
|
---|
56 | case RELATION:
|
---|
57 | presetType = PresetType.RELATION;
|
---|
58 | break;
|
---|
59 | default:
|
---|
60 | throw new AssertionError();
|
---|
61 | }
|
---|
62 |
|
---|
63 | for(TaggingPreset t : presetsWithPattern) {
|
---|
64 |
|
---|
65 |
|
---|
66 | if ( t.types == null
|
---|
67 | || t.types.contains(presetType)
|
---|
68 | || (presetType == PresetType.CLOSEDWAY && t.types.contains(PresetType.WAY))) {
|
---|
69 | int found = 0;
|
---|
70 |
|
---|
71 | if (t.nameTemplateFilter != null) {
|
---|
72 | if (t.nameTemplateFilter.match(primitive))
|
---|
73 | return t;
|
---|
74 | else {
|
---|
75 | continue;
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | for(TaggingPreset.Item i : t.data) {
|
---|
80 | if(i instanceof TaggingPreset.Key) {
|
---|
81 | String val = ((TaggingPreset.Key)i).value;
|
---|
82 | String key = ((TaggingPreset.Key)i).key;
|
---|
83 | // we subtract 100 if not found and add 1 if found
|
---|
84 | if (val != null && val.equals(primitive.get(key))) {
|
---|
85 | found+=1;
|
---|
86 | } else {
|
---|
87 | found-=100;
|
---|
88 | }
|
---|
89 | } else {
|
---|
90 | String key = null;
|
---|
91 | if ((i instanceof Text) && ((Text)i).required) {
|
---|
92 | key = ((Text)i).key;
|
---|
93 | } else if ((i instanceof Combo) && ((Combo)i).required) {
|
---|
94 | key = ((Combo)i).key;
|
---|
95 | } else if ((i instanceof Check) && ((Check)i).required) {
|
---|
96 | key = ((Check)i).key;
|
---|
97 | }
|
---|
98 | if (key != null) {
|
---|
99 | if (primitive.get(key) != null) {
|
---|
100 | found += 1;
|
---|
101 | } else {
|
---|
102 | found -= 100;
|
---|
103 | }
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | if(found > 0)
|
---|
109 | return t; // First matching preset wins
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | return null;
|
---|
114 |
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | }
|
---|