1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.tagging.presets;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.trc;
|
---|
6 |
|
---|
7 | import java.io.File;
|
---|
8 | import java.util.Arrays;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.EnumSet;
|
---|
11 | import java.util.LinkedHashMap;
|
---|
12 | import java.util.List;
|
---|
13 | import java.util.Map;
|
---|
14 | import java.util.Set;
|
---|
15 |
|
---|
16 | import javax.swing.ImageIcon;
|
---|
17 | import javax.swing.JPanel;
|
---|
18 |
|
---|
19 | import org.openstreetmap.josm.Main;
|
---|
20 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
21 | import org.openstreetmap.josm.data.osm.Tag;
|
---|
22 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
23 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
|
---|
24 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
|
---|
25 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
26 | import org.xml.sax.SAXException;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Class that represents single part of a preset - one field or text label that is shown to user
|
---|
30 | * @since 6068
|
---|
31 | */
|
---|
32 | public abstract class TaggingPresetItem {
|
---|
33 |
|
---|
34 | // cache the parsing of types using a LRU cache (http://java-planet.blogspot.com/2005/08/how-to-set-up-simple-lru-cache-using.html)
|
---|
35 | private static final Map<String, Set<TaggingPresetType>> TYPE_CACHE = new LinkedHashMap<>(16, 1.1f, true);
|
---|
36 |
|
---|
37 | protected void initAutoCompletionField(AutoCompletingTextField field, String... key) {
|
---|
38 | initAutoCompletionField(field, Arrays.asList(key));
|
---|
39 | }
|
---|
40 |
|
---|
41 | protected void initAutoCompletionField(AutoCompletingTextField field, List<String> keys) {
|
---|
42 | if (Main.main == null) return;
|
---|
43 | OsmDataLayer layer = Main.main.getEditLayer();
|
---|
44 | if (layer == null) {
|
---|
45 | return;
|
---|
46 | }
|
---|
47 | AutoCompletionList list = new AutoCompletionList();
|
---|
48 | layer.data.getAutoCompletionManager().populateWithTagValues(list, keys);
|
---|
49 | field.setAutoCompletionList(list);
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Called by {@link TaggingPreset#createPanel} during tagging preset panel creation.
|
---|
54 | * All components defining this tagging preset item must be added to given panel.
|
---|
55 | *
|
---|
56 | * @param p The panel where components must be added
|
---|
57 | * @param sel The related selected OSM primitives
|
---|
58 | * @param presetInitiallyMatches Whether this {@link TaggingPreset} already matched before applying,
|
---|
59 | * i.e. whether the map feature already existed on the primitive.
|
---|
60 | * @return {@code true} if this item adds semantic tagging elements, {@code false} otherwise.
|
---|
61 | */
|
---|
62 | protected abstract boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel, boolean presetInitiallyMatches);
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Adds the new tags to apply to selected OSM primitives when the preset holding this item is applied.
|
---|
66 | * @param changedTags The list of changed tags to modify if needed
|
---|
67 | */
|
---|
68 | protected abstract void addCommands(List<Tag> changedTags);
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Tests whether the tags match this item.
|
---|
72 | * Note that for a match, at least one positive and no negative is required.
|
---|
73 | * @param tags the tags of an {@link OsmPrimitive}
|
---|
74 | * @return {@code true} if matches (positive), {@code null} if neutral, {@code false} if mismatches (negative).
|
---|
75 | */
|
---|
76 | protected Boolean matches(Map<String, String> tags) {
|
---|
77 | return null;
|
---|
78 | }
|
---|
79 |
|
---|
80 | protected static Set<TaggingPresetType> getType(String types) throws SAXException {
|
---|
81 | if (types == null || types.isEmpty()) {
|
---|
82 | throw new SAXException(tr("Unknown type: {0}", types));
|
---|
83 | }
|
---|
84 | if (TYPE_CACHE.containsKey(types))
|
---|
85 | return TYPE_CACHE.get(types);
|
---|
86 | Set<TaggingPresetType> result = EnumSet.noneOf(TaggingPresetType.class);
|
---|
87 | for (String type : Arrays.asList(types.split(","))) {
|
---|
88 | try {
|
---|
89 | TaggingPresetType presetType = TaggingPresetType.fromString(type);
|
---|
90 | if (presetType != null) {
|
---|
91 | result.add(presetType);
|
---|
92 | }
|
---|
93 | } catch (IllegalArgumentException e) {
|
---|
94 | throw new SAXException(tr("Unknown type: {0}", type), e);
|
---|
95 | }
|
---|
96 | }
|
---|
97 | TYPE_CACHE.put(types, result);
|
---|
98 | return result;
|
---|
99 | }
|
---|
100 |
|
---|
101 | protected static String fixPresetString(String s) {
|
---|
102 | return s == null ? s : s.replaceAll("'", "''");
|
---|
103 | }
|
---|
104 |
|
---|
105 | protected static String getLocaleText(String text, String text_context, String defaultText) {
|
---|
106 | if (text == null) {
|
---|
107 | return defaultText;
|
---|
108 | } else if (text_context != null) {
|
---|
109 | return trc(text_context, fixPresetString(text));
|
---|
110 | } else {
|
---|
111 | return tr(fixPresetString(text));
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | protected static Integer parseInteger(String str) {
|
---|
116 | if (str == null || str.isEmpty())
|
---|
117 | return null;
|
---|
118 | try {
|
---|
119 | return Integer.valueOf(str);
|
---|
120 | } catch (Exception e) {
|
---|
121 | if (Main.isTraceEnabled()) {
|
---|
122 | Main.trace(e.getMessage());
|
---|
123 | }
|
---|
124 | }
|
---|
125 | return null;
|
---|
126 | }
|
---|
127 |
|
---|
128 | protected static ImageIcon loadImageIcon(String iconName, File zipIcons, Integer maxSize) {
|
---|
129 | final Collection<String> s = Main.pref.getCollection("taggingpreset.icon.sources", null);
|
---|
130 | ImageProvider imgProv = new ImageProvider(iconName).setDirs(s).setId("presets").setArchive(zipIcons).setOptional(true);
|
---|
131 | if (maxSize != null) {
|
---|
132 | imgProv.setMaxSize(maxSize);
|
---|
133 | }
|
---|
134 | return imgProv.get();
|
---|
135 | }
|
---|
136 | }
|
---|