| 1 | // License: GPL. For details, see LICENSE file. |
|---|
| 2 | package org.openstreetmap.josm.tools; |
|---|
| 3 | |
|---|
| 4 | import java.util.Collection; |
|---|
| 5 | import java.util.Collections; |
|---|
| 6 | import java.util.LinkedList; |
|---|
| 7 | import java.util.List; |
|---|
| 8 | |
|---|
| 9 | import org.openstreetmap.josm.data.osm.OsmPrimitive; |
|---|
| 10 | import org.openstreetmap.josm.gui.preferences.map.TaggingPresetPreference; |
|---|
| 11 | import org.openstreetmap.josm.gui.tagging.TaggingPreset; |
|---|
| 12 | import org.openstreetmap.josm.gui.tagging.TaggingPreset.PresetType; |
|---|
| 13 | |
|---|
| 14 | /** |
|---|
| 15 | * List of tagging presets with name templates, allows to find appropriate template based on existing primitive |
|---|
| 16 | */ |
|---|
| 17 | public 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<PresetType> type = Collections.singleton(PresetType.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 | } |
|---|