[4431] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[8863] | 2 | package org.openstreetmap.josm.gui.tagging.presets;
|
---|
[4431] | 3 |
|
---|
[5169] | 4 | import java.util.Collection;
|
---|
[10044] | 5 | import java.util.EnumSet;
|
---|
[5155] | 6 | import java.util.LinkedList;
|
---|
[4431] | 7 | import java.util.List;
|
---|
| 8 |
|
---|
[13564] | 9 | import org.openstreetmap.josm.data.osm.IPrimitive;
|
---|
[12620] | 10 | import org.openstreetmap.josm.tools.Logging;
|
---|
[4431] | 11 |
|
---|
| 12 | /**
|
---|
| 13 | * List of tagging presets with name templates, allows to find appropriate template based on existing primitive
|
---|
| 14 | */
|
---|
[7100] | 15 | public final class TaggingPresetNameTemplateList implements TaggingPresetListener {
|
---|
[4431] | 16 |
|
---|
| 17 | private static TaggingPresetNameTemplateList instance;
|
---|
| 18 |
|
---|
[8863] | 19 | private final List<TaggingPreset> presetsWithPattern = new LinkedList<>();
|
---|
| 20 |
|
---|
[7100] | 21 | /**
|
---|
| 22 | * Replies the unique instance.
|
---|
| 23 | * @return the unique instance
|
---|
| 24 | */
|
---|
[8126] | 25 | public static synchronized TaggingPresetNameTemplateList getInstance() {
|
---|
[4431] | 26 | if (instance == null) {
|
---|
| 27 | instance = new TaggingPresetNameTemplateList();
|
---|
[7100] | 28 | TaggingPresets.addListener(instance);
|
---|
[4431] | 29 | }
|
---|
| 30 | return instance;
|
---|
| 31 | }
|
---|
[7100] | 32 |
|
---|
[4431] | 33 | private TaggingPresetNameTemplateList() {
|
---|
[7100] | 34 | buildPresetsWithPattern();
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | private void buildPresetsWithPattern() {
|
---|
[8510] | 38 | synchronized (this) {
|
---|
[12620] | 39 | Logging.debug("Building list of presets with name template");
|
---|
[7100] | 40 | presetsWithPattern.clear();
|
---|
| 41 | for (TaggingPreset tp : TaggingPresets.getTaggingPresets()) {
|
---|
[4837] | 42 | if (tp.nameTemplate != null) {
|
---|
| 43 | presetsWithPattern.add(tp);
|
---|
| 44 | }
|
---|
[4431] | 45 | }
|
---|
| 46 | }
|
---|
| 47 | }
|
---|
| 48 |
|
---|
[7100] | 49 | /**
|
---|
[14273] | 50 | * Finds and returns the first occurrence of preset with template name matching the given primitive
|
---|
[7100] | 51 | * @param primitive The primitive to match
|
---|
[14273] | 52 | * @return the first occurrence of preset with template name matching the primitive
|
---|
[7100] | 53 | */
|
---|
[13564] | 54 | public TaggingPreset findPresetTemplate(IPrimitive primitive) {
|
---|
[8510] | 55 | synchronized (this) {
|
---|
[7100] | 56 | for (TaggingPreset t : presetsWithPattern) {
|
---|
[10044] | 57 | Collection<TaggingPresetType> type = EnumSet.of(TaggingPresetType.forPrimitive(primitive));
|
---|
[7100] | 58 | if (t.typeMatches(type)) {
|
---|
| 59 | if (t.nameTemplateFilter != null) {
|
---|
| 60 | if (t.nameTemplateFilter.match(primitive))
|
---|
| 61 | return t;
|
---|
| 62 | } else if (t.matches(type, primitive.getKeys(), false)) {
|
---|
[5169] | 63 | return t;
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
[4431] | 66 | }
|
---|
| 67 | }
|
---|
| 68 | return null;
|
---|
| 69 | }
|
---|
[7100] | 70 |
|
---|
| 71 | @Override
|
---|
| 72 | public void taggingPresetsModified() {
|
---|
| 73 | buildPresetsWithPattern();
|
---|
| 74 | }
|
---|
[4431] | 75 | }
|
---|