source: josm/trunk/src/org/openstreetmap/josm/gui/tagging/presets/items/KeyedItem.java@ 9665

Last change on this file since 9665 was 9665, checked in by stoecker, 8 years ago

fix eol-style issues and similar formating stuff, see #12410

File size: 6.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.tagging.presets.items;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.Collection;
7import java.util.EnumSet;
8import java.util.HashMap;
9import java.util.Map;
10import java.util.SortedSet;
11import java.util.TreeSet;
12
13import org.openstreetmap.josm.data.osm.OsmPrimitive;
14import org.openstreetmap.josm.data.osm.OsmUtils;
15import org.openstreetmap.josm.data.preferences.BooleanProperty;
16import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetItem;
17
18/**
19 * Preset item associated to an OSM key.
20 */
21public abstract class KeyedItem extends TaggingPresetItem {
22
23 /** Translatation of "<different>". Use in combo boxes to display an entry matching several different values. */
24 protected static final String DIFFERENT = tr("<different>");
25
26 protected static final BooleanProperty PROP_FILL_DEFAULT = new BooleanProperty("taggingpreset.fill-default-for-tagged-primitives", false);
27
28 /** Last value of each key used in presets, used for prefilling corresponding fields */
29 protected static final Map<String, String> LAST_VALUES = new HashMap<>();
30
31 /** This specifies the property key that will be modified by the item. */
32 public String key; // NOSONAR
33 /** The text to display */
34 public String text; // NOSONAR
35 /** The context used for translating {@link #text} */
36 public String text_context; // NOSONAR
37 /**
38 * Allows to change the matching process, i.e., determining whether the tags of an OSM object fit into this preset.
39 * If a preset fits then it is linked in the Tags/Membership dialog.<ul>
40 * <li>none: neutral, i.e., do not consider this item for matching</li>
41 * <li>key: positive if key matches, neutral otherwise</li>
42 * <li>key!: positive if key matches, negative otherwise</li>
43 * <li>keyvalue: positive if key and value matches, neutral otherwise</li>
44 * <li>keyvalue!: positive if key and value matches, negative otherwise</li></ul>
45 * Note that for a match, at least one positive and no negative is required.
46 * Default is "keyvalue!" for {@link Key} and "none" for {@link Text}, {@link Combo}, {@link MultiSelect} and {@link Check}.
47 */
48 public String match = getDefaultMatch().getValue(); // NOSONAR
49
50 /**
51 * Enum denoting how a match (see {@link TaggingPresetItem#matches}) is performed.
52 */
53 protected enum MatchType {
54
55 /** Neutral, i.e., do not consider this item for matching. */
56 NONE("none"),
57 /** Positive if key matches, neutral otherwise. */
58 KEY("key"),
59 /** Positive if key matches, negative otherwise. */
60 KEY_REQUIRED("key!"),
61 /** Positive if key and value matches, neutral otherwise. */
62 KEY_VALUE("keyvalue"),
63 /** Positive if key and value matches, negative otherwise. */
64 KEY_VALUE_REQUIRED("keyvalue!");
65
66 private final String value;
67
68 MatchType(String value) {
69 this.value = value;
70 }
71
72 /**
73 * Replies the associated textual value.
74 * @return the associated textual value
75 */
76 public String getValue() {
77 return value;
78 }
79
80 /**
81 * Determines the {@code MatchType} for the given textual value.
82 * @param type the textual value
83 * @return the {@code MatchType} for the given textual value
84 */
85 public static MatchType ofString(String type) {
86 for (MatchType i : EnumSet.allOf(MatchType.class)) {
87 if (i.getValue().equals(type))
88 return i;
89 }
90 throw new IllegalArgumentException(type + " is not allowed");
91 }
92 }
93
94 protected static class Usage {
95 public SortedSet<String> values; // NOSONAR
96 private boolean hadKeys;
97 private boolean hadEmpty;
98
99 public boolean hasUniqueValue() {
100 return values.size() == 1 && !hadEmpty;
101 }
102
103 public boolean unused() {
104 return values.isEmpty();
105 }
106
107 public String getFirst() {
108 return values.first();
109 }
110
111 public boolean hadKeys() {
112 return hadKeys;
113 }
114 }
115
116 protected static Usage determineTextUsage(Collection<OsmPrimitive> sel, String key) {
117 Usage returnValue = new Usage();
118 returnValue.values = new TreeSet<>();
119 for (OsmPrimitive s : sel) {
120 String v = s.get(key);
121 if (v != null) {
122 returnValue.values.add(v);
123 } else {
124 returnValue.hadEmpty = true;
125 }
126 if (s.hasKeys()) {
127 returnValue.hadKeys = true;
128 }
129 }
130 return returnValue;
131 }
132
133 protected static Usage determineBooleanUsage(Collection<OsmPrimitive> sel, String key) {
134
135 Usage returnValue = new Usage();
136 returnValue.values = new TreeSet<>();
137 for (OsmPrimitive s : sel) {
138 String booleanValue = OsmUtils.getNamedOsmBoolean(s.get(key));
139 if (booleanValue != null) {
140 returnValue.values.add(booleanValue);
141 }
142 }
143 return returnValue;
144 }
145
146 /**
147 * Returns the default match.
148 * @return the default match
149 */
150 public abstract MatchType getDefaultMatch();
151
152 /**
153 * Returns the list of values.
154 * @return the list of values
155 */
156 public abstract Collection<String> getValues();
157
158 protected String getKeyTooltipText() {
159 return tr("This corresponds to the key ''{0}''", key);
160 }
161
162 @Override
163 protected Boolean matches(Map<String, String> tags) {
164 switch (MatchType.ofString(match)) {
165 case NONE:
166 return null;
167 case KEY:
168 return tags.containsKey(key) ? Boolean.TRUE : null;
169 case KEY_REQUIRED:
170 return tags.containsKey(key);
171 case KEY_VALUE:
172 return tags.containsKey(key) && getValues().contains(tags.get(key)) ? Boolean.TRUE : null;
173 case KEY_VALUE_REQUIRED:
174 return tags.containsKey(key) && getValues().contains(tags.get(key));
175 default:
176 throw new IllegalStateException();
177 }
178 }
179
180 @Override
181 public String toString() {
182 return "KeyedItem [key=" + key + ", text=" + text
183 + ", text_context=" + text_context + ", match=" + match
184 + ']';
185 }
186}
Note: See TracBrowser for help on using the repository browser.