[6074] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[8863] | 2 | package org.openstreetmap.josm.gui.tagging.presets;
|
---|
[6068] | 3 |
|
---|
[8863] | 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 | import static org.openstreetmap.josm.tools.I18n.trc;
|
---|
| 6 |
|
---|
| 7 | import java.io.File;
|
---|
[7118] | 8 | import java.util.Arrays;
|
---|
[6068] | 9 | import java.util.Collection;
|
---|
[8863] | 10 | import java.util.EnumSet;
|
---|
| 11 | import java.util.LinkedHashMap;
|
---|
[6068] | 12 | import java.util.List;
|
---|
| 13 | import java.util.Map;
|
---|
[8863] | 14 | import java.util.Set;
|
---|
[6198] | 15 |
|
---|
[8863] | 16 | import javax.swing.ImageIcon;
|
---|
[6068] | 17 | import javax.swing.JPanel;
|
---|
[6198] | 18 |
|
---|
[6068] | 19 | import org.openstreetmap.josm.Main;
|
---|
[12835] | 20 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
[6068] | 21 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
| 22 | import org.openstreetmap.josm.data.osm.Tag;
|
---|
| 23 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
|
---|
| 24 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
|
---|
[12758] | 25 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
|
---|
[8863] | 26 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
[12620] | 27 | import org.openstreetmap.josm.tools.Logging;
|
---|
[8863] | 28 | import org.xml.sax.SAXException;
|
---|
[6068] | 29 |
|
---|
| 30 | /**
|
---|
[6074] | 31 | * Class that represents single part of a preset - one field or text label that is shown to user
|
---|
| 32 | * @since 6068
|
---|
[6068] | 33 | */
|
---|
| 34 | public abstract class TaggingPresetItem {
|
---|
| 35 |
|
---|
[8863] | 36 | // 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)
|
---|
| 37 | private static final Map<String, Set<TaggingPresetType>> TYPE_CACHE = new LinkedHashMap<>(16, 1.1f, true);
|
---|
| 38 |
|
---|
[7118] | 39 | protected void initAutoCompletionField(AutoCompletingTextField field, String... key) {
|
---|
| 40 | initAutoCompletionField(field, Arrays.asList(key));
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | protected void initAutoCompletionField(AutoCompletingTextField field, List<String> keys) {
|
---|
[6072] | 44 | if (Main.main == null) return;
|
---|
[12835] | 45 | DataSet data = Main.main.getEditDataSet();
|
---|
| 46 | if (data == null) {
|
---|
[6068] | 47 | return;
|
---|
| 48 | }
|
---|
| 49 | AutoCompletionList list = new AutoCompletionList();
|
---|
[12835] | 50 | AutoCompletionManager.of(data).populateWithTagValues(list, keys);
|
---|
[6068] | 51 | field.setAutoCompletionList(list);
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[6198] | 54 | /**
|
---|
| 55 | * Called by {@link TaggingPreset#createPanel} during tagging preset panel creation.
|
---|
| 56 | * All components defining this tagging preset item must be added to given panel.
|
---|
[6795] | 57 | *
|
---|
[6198] | 58 | * @param p The panel where components must be added
|
---|
| 59 | * @param sel The related selected OSM primitives
|
---|
[6795] | 60 | * @param presetInitiallyMatches Whether this {@link TaggingPreset} already matched before applying,
|
---|
| 61 | * i.e. whether the map feature already existed on the primitive.
|
---|
[6198] | 62 | * @return {@code true} if this item adds semantic tagging elements, {@code false} otherwise.
|
---|
| 63 | */
|
---|
[8863] | 64 | protected abstract boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel, boolean presetInitiallyMatches);
|
---|
[6068] | 65 |
|
---|
[6198] | 66 | /**
|
---|
| 67 | * Adds the new tags to apply to selected OSM primitives when the preset holding this item is applied.
|
---|
| 68 | * @param changedTags The list of changed tags to modify if needed
|
---|
| 69 | */
|
---|
[8863] | 70 | protected abstract void addCommands(List<Tag> changedTags);
|
---|
[6068] | 71 |
|
---|
| 72 | /**
|
---|
| 73 | * Tests whether the tags match this item.
|
---|
| 74 | * Note that for a match, at least one positive and no negative is required.
|
---|
| 75 | * @param tags the tags of an {@link OsmPrimitive}
|
---|
| 76 | * @return {@code true} if matches (positive), {@code null} if neutral, {@code false} if mismatches (negative).
|
---|
| 77 | */
|
---|
[8863] | 78 | protected Boolean matches(Map<String, String> tags) {
|
---|
[6068] | 79 | return null;
|
---|
| 80 | }
|
---|
[8863] | 81 |
|
---|
| 82 | protected static Set<TaggingPresetType> getType(String types) throws SAXException {
|
---|
| 83 | if (types == null || types.isEmpty()) {
|
---|
| 84 | throw new SAXException(tr("Unknown type: {0}", types));
|
---|
| 85 | }
|
---|
| 86 | if (TYPE_CACHE.containsKey(types))
|
---|
| 87 | return TYPE_CACHE.get(types);
|
---|
| 88 | Set<TaggingPresetType> result = EnumSet.noneOf(TaggingPresetType.class);
|
---|
| 89 | for (String type : Arrays.asList(types.split(","))) {
|
---|
| 90 | try {
|
---|
| 91 | TaggingPresetType presetType = TaggingPresetType.fromString(type);
|
---|
[9574] | 92 | if (presetType != null) {
|
---|
| 93 | result.add(presetType);
|
---|
| 94 | }
|
---|
[8863] | 95 | } catch (IllegalArgumentException e) {
|
---|
| 96 | throw new SAXException(tr("Unknown type: {0}", type), e);
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 | TYPE_CACHE.put(types, result);
|
---|
| 100 | return result;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | protected static String fixPresetString(String s) {
|
---|
| 104 | return s == null ? s : s.replaceAll("'", "''");
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[10001] | 107 | protected static String getLocaleText(String text, String textContext, String defaultText) {
|
---|
[8863] | 108 | if (text == null) {
|
---|
| 109 | return defaultText;
|
---|
[10001] | 110 | } else if (textContext != null) {
|
---|
| 111 | return trc(textContext, fixPresetString(text));
|
---|
[8863] | 112 | } else {
|
---|
| 113 | return tr(fixPresetString(text));
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | protected static Integer parseInteger(String str) {
|
---|
| 118 | if (str == null || str.isEmpty())
|
---|
| 119 | return null;
|
---|
| 120 | try {
|
---|
| 121 | return Integer.valueOf(str);
|
---|
[10212] | 122 | } catch (NumberFormatException e) {
|
---|
[12620] | 123 | Logging.trace(e);
|
---|
[8863] | 124 | }
|
---|
| 125 | return null;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | protected static ImageIcon loadImageIcon(String iconName, File zipIcons, Integer maxSize) {
|
---|
[12841] | 129 | final Collection<String> s = Main.pref.getList("taggingpreset.icon.sources", null);
|
---|
[8863] | 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 | }
|
---|
[9932] | 136 |
|
---|
| 137 | /**
|
---|
| 138 | * Determine whether the given preset items match the tags
|
---|
| 139 | * @param data the preset items
|
---|
| 140 | * @param tags the tags to match
|
---|
| 141 | * @return whether the given preset items match the tags
|
---|
[9936] | 142 | * @since 9932
|
---|
[9932] | 143 | */
|
---|
| 144 | public static boolean matches(Iterable<? extends TaggingPresetItem> data, Map<String, String> tags) {
|
---|
| 145 | boolean atLeastOnePositiveMatch = false;
|
---|
| 146 | for (TaggingPresetItem item : data) {
|
---|
| 147 | Boolean m = item.matches(tags);
|
---|
| 148 | if (m != null && !m)
|
---|
| 149 | return false;
|
---|
| 150 | else if (m != null) {
|
---|
| 151 | atLeastOnePositiveMatch = true;
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 | return atLeastOnePositiveMatch;
|
---|
| 155 | }
|
---|
[6068] | 156 | }
|
---|