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.List;
|
---|
12 | import java.util.Map;
|
---|
13 | import java.util.Set;
|
---|
14 |
|
---|
15 | import javax.swing.ImageIcon;
|
---|
16 | import javax.swing.JPanel;
|
---|
17 |
|
---|
18 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
19 | import org.openstreetmap.josm.data.osm.OsmDataManager;
|
---|
20 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
21 | import org.openstreetmap.josm.data.osm.Tag;
|
---|
22 | import org.openstreetmap.josm.data.preferences.BooleanProperty;
|
---|
23 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
|
---|
24 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
|
---|
25 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
|
---|
26 | import org.openstreetmap.josm.gui.util.LruCache;
|
---|
27 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
28 | import org.openstreetmap.josm.tools.Logging;
|
---|
29 | import org.xml.sax.SAXException;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Class that represents single part of a preset - one field or text label that is shown to user
|
---|
33 | * @since 6068
|
---|
34 | */
|
---|
35 | public abstract class TaggingPresetItem {
|
---|
36 |
|
---|
37 | // cache the parsing of types using a LRU cache
|
---|
38 | private static final Map<String, Set<TaggingPresetType>> TYPE_CACHE = new LruCache<>(16);
|
---|
39 | /**
|
---|
40 | * Display OSM keys as {@linkplain org.openstreetmap.josm.gui.widgets.OsmIdTextField#setHint hint}
|
---|
41 | */
|
---|
42 | protected static BooleanProperty DISPLAY_KEYS_AS_HINT = new BooleanProperty("taggingpreset.display-keys-as-hint", true);
|
---|
43 |
|
---|
44 | protected void initAutoCompletionField(AutoCompletingTextField field, String... key) {
|
---|
45 | initAutoCompletionField(field, Arrays.asList(key));
|
---|
46 | }
|
---|
47 |
|
---|
48 | protected void initAutoCompletionField(AutoCompletingTextField field, List<String> keys) {
|
---|
49 | DataSet data = OsmDataManager.getInstance().getEditDataSet();
|
---|
50 | if (data == null) {
|
---|
51 | return;
|
---|
52 | }
|
---|
53 | AutoCompletionList list = new AutoCompletionList();
|
---|
54 | AutoCompletionManager.of(data).populateWithTagValues(list, keys);
|
---|
55 | field.setAutoCompletionList(list);
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Called by {@link TaggingPreset#createPanel} during tagging preset panel creation.
|
---|
60 | * All components defining this tagging preset item must be added to given panel.
|
---|
61 | *
|
---|
62 | * @param p The panel where components must be added
|
---|
63 | * @param support supporting class for creating the GUI
|
---|
64 | * @return {@code true} if this item adds semantic tagging elements, {@code false} otherwise.
|
---|
65 | */
|
---|
66 | protected abstract boolean addToPanel(JPanel p, TaggingPresetItemGuiSupport support);
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Adds the new tags to apply to selected OSM primitives when the preset holding this item is applied.
|
---|
70 | * @param changedTags The list of changed tags to modify if needed
|
---|
71 | */
|
---|
72 | protected abstract void addCommands(List<Tag> changedTags);
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Tests whether the tags match this item.
|
---|
76 | * Note that for a match, at least one positive and no negative is required.
|
---|
77 | * @param tags the tags of an {@link OsmPrimitive}
|
---|
78 | * @return {@code true} if matches (positive), {@code null} if neutral, {@code false} if mismatches (negative).
|
---|
79 | */
|
---|
80 | public Boolean matches(Map<String, String> tags) {
|
---|
81 | return null;
|
---|
82 | }
|
---|
83 |
|
---|
84 | protected static Set<TaggingPresetType> getType(String types) throws SAXException {
|
---|
85 | if (types == null || types.isEmpty()) {
|
---|
86 | throw new SAXException(tr("Unknown type: {0}", types));
|
---|
87 | }
|
---|
88 | if (TYPE_CACHE.containsKey(types))
|
---|
89 | return TYPE_CACHE.get(types);
|
---|
90 | Set<TaggingPresetType> result = EnumSet.noneOf(TaggingPresetType.class);
|
---|
91 | for (String type : types.split(",", -1)) {
|
---|
92 | try {
|
---|
93 | TaggingPresetType presetType = TaggingPresetType.fromString(type);
|
---|
94 | if (presetType != null) {
|
---|
95 | result.add(presetType);
|
---|
96 | }
|
---|
97 | } catch (IllegalArgumentException e) {
|
---|
98 | throw new SAXException(tr("Unknown type: {0}", type), e);
|
---|
99 | }
|
---|
100 | }
|
---|
101 | TYPE_CACHE.put(types, result);
|
---|
102 | return result;
|
---|
103 | }
|
---|
104 |
|
---|
105 | protected static String fixPresetString(String s) {
|
---|
106 | return s == null ? s : s.replace("'", "''");
|
---|
107 | }
|
---|
108 |
|
---|
109 | protected static String getLocaleText(String text, String textContext, String defaultText) {
|
---|
110 | if (text == null) {
|
---|
111 | return defaultText;
|
---|
112 | } else if (textContext != null) {
|
---|
113 | return trc(textContext, fixPresetString(text));
|
---|
114 | } else {
|
---|
115 | return tr(fixPresetString(text));
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | protected static Integer parseInteger(String str) {
|
---|
120 | if (str == null || str.isEmpty())
|
---|
121 | return null;
|
---|
122 | try {
|
---|
123 | return Integer.valueOf(str);
|
---|
124 | } catch (NumberFormatException e) {
|
---|
125 | Logging.trace(e);
|
---|
126 | }
|
---|
127 | return null;
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Loads a tagging preset icon
|
---|
132 | * @param iconName the icon name
|
---|
133 | * @param zipIcons zip file where the image is located
|
---|
134 | * @param maxSize maximum image size (or null)
|
---|
135 | * @return the requested image or null if the request failed
|
---|
136 | */
|
---|
137 | public static ImageIcon loadImageIcon(String iconName, File zipIcons, Integer maxSize) {
|
---|
138 | final Collection<String> s = TaggingPresets.ICON_SOURCES.get();
|
---|
139 | ImageProvider imgProv = new ImageProvider(iconName).setDirs(s).setId("presets").setArchive(zipIcons).setOptional(true);
|
---|
140 | if (maxSize != null && maxSize > 0) {
|
---|
141 | imgProv.setMaxSize(maxSize);
|
---|
142 | }
|
---|
143 | return imgProv.get();
|
---|
144 | }
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * Determine whether the given preset items match the tags
|
---|
148 | * @param data the preset items
|
---|
149 | * @param tags the tags to match
|
---|
150 | * @return whether the given preset items match the tags
|
---|
151 | * @since 9932
|
---|
152 | */
|
---|
153 | public static boolean matches(Iterable<? extends TaggingPresetItem> data, Map<String, String> tags) {
|
---|
154 | boolean atLeastOnePositiveMatch = false;
|
---|
155 | for (TaggingPresetItem item : data) {
|
---|
156 | Boolean m = item.matches(tags);
|
---|
157 | if (m != null && !m)
|
---|
158 | return false;
|
---|
159 | else if (m != null) {
|
---|
160 | atLeastOnePositiveMatch = true;
|
---|
161 | }
|
---|
162 | }
|
---|
163 | return atLeastOnePositiveMatch;
|
---|
164 | }
|
---|
165 | }
|
---|