source: josm/trunk/src/org/openstreetmap/josm/gui/tagging/presets/TaggingPresetItemGuiSupport.java@ 18080

Last change on this file since 18080 was 18080, checked in by Don-vip, 3 years ago

fix #21156 - fix #20851 - see #20861 - disable value_template for multiple selection (patch by marcello)

File size: 4.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.tagging.presets;
3
4import org.openstreetmap.josm.data.osm.OsmPrimitive;
5import org.openstreetmap.josm.data.osm.Tag;
6import org.openstreetmap.josm.data.osm.Tagged;
7import org.openstreetmap.josm.data.osm.search.SearchCompiler;
8import org.openstreetmap.josm.tools.ListenerList;
9import org.openstreetmap.josm.tools.template_engine.TemplateEngineDataProvider;
10
11import java.util.Arrays;
12import java.util.Collection;
13import java.util.Collections;
14import java.util.function.Supplier;
15
16/**
17 * Supporting class for creating the GUI for a preset item.
18 *
19 * @since 17609
20 */
21public final class TaggingPresetItemGuiSupport implements TemplateEngineDataProvider {
22
23 private final Collection<OsmPrimitive> selected;
24 private final boolean presetInitiallyMatches;
25 private final Supplier<Collection<Tag>> changedTagsSupplier;
26 private final ListenerList<ChangeListener> listeners = ListenerList.create();
27
28 /**
29 * Interface to notify listeners that a preset item input as changed.
30 * @since 17610
31 */
32 public interface ChangeListener {
33 /**
34 * Notifies this listener that a preset item input as changed.
35 * @param source the source of this event
36 * @param key the tag key
37 * @param newValue the new tag value
38 */
39 void itemValueModified(TaggingPresetItem source, String key, String newValue);
40 }
41
42 private TaggingPresetItemGuiSupport(
43 boolean presetInitiallyMatches, Collection<OsmPrimitive> selected, Supplier<Collection<Tag>> changedTagsSupplier) {
44 this.selected = selected;
45 this.presetInitiallyMatches = presetInitiallyMatches;
46 this.changedTagsSupplier = changedTagsSupplier;
47 }
48
49 /**
50 * Returns the selected primitives
51 *
52 * @return the selected primitives
53 */
54 public Collection<OsmPrimitive> getSelected() {
55 return selected;
56 }
57
58 /**
59 * Returns whether the preset initially matched (before opening the dialog)
60 *
61 * @return whether the preset initially matched
62 */
63 public boolean isPresetInitiallyMatches() {
64 return presetInitiallyMatches;
65 }
66
67 /**
68 * Creates a new {@code TaggingPresetItemGuiSupport}
69 *
70 * @param presetInitiallyMatches whether the preset initially matched
71 * @param selected the selected primitives
72 * @param changedTagsSupplier the changed tags
73 * @return the new {@code TaggingPresetItemGuiSupport}
74 */
75 public static TaggingPresetItemGuiSupport create(
76 boolean presetInitiallyMatches, Collection<OsmPrimitive> selected, Supplier<Collection<Tag>> changedTagsSupplier) {
77 return new TaggingPresetItemGuiSupport(presetInitiallyMatches, selected, changedTagsSupplier);
78 }
79
80 /**
81 * Creates a new {@code TaggingPresetItemGuiSupport}
82 *
83 * @param presetInitiallyMatches whether the preset initially matched
84 * @param selected the selected primitives
85 * @return the new {@code TaggingPresetItemGuiSupport}
86 */
87 public static TaggingPresetItemGuiSupport create(
88 boolean presetInitiallyMatches, OsmPrimitive... selected) {
89 return new TaggingPresetItemGuiSupport(presetInitiallyMatches, Arrays.asList(selected), Collections::emptyList);
90 }
91
92 /**
93 * Get tags with values as currently shown in the dialog.
94 * If exactly one primitive is selected, get all tags of it, then
95 * overwrite with the current values shown in the dialog.
96 * Else get only the tags shown in the dialog.
97 * @return Tags
98 */
99 public Tagged getTagged() {
100 if (selected.size() != 1) {
101 return Tagged.ofTags(changedTagsSupplier.get());
102 }
103 // if there is only one primitive selected, get its tags
104 Tagged tagged = Tagged.ofMap(selected.iterator().next().getKeys());
105 // update changed tags
106 changedTagsSupplier.get().forEach(tag -> tagged.put(tag));
107 return tagged;
108 }
109
110 @Override
111 public Collection<String> getTemplateKeys() {
112 return getTagged().keySet();
113 }
114
115 @Override
116 public Object getTemplateValue(String key, boolean special) {
117 String value = getTagged().get(key);
118 return (value == null || value.isEmpty()) ? null : value;
119 }
120
121 @Override
122 public boolean evaluateCondition(SearchCompiler.Match condition) {
123 return condition.match(getTagged());
124 }
125
126 /**
127 * Adds a new change listener
128 * @param listener the listener to add
129 */
130 public void addListener(ChangeListener listener) {
131 listeners.addListener(listener);
132 }
133
134 /**
135 * Notifies all listeners that a preset item input as changed.
136 * @param source the source of this event
137 * @param key the tag key
138 * @param newValue the new tag value
139 */
140 public void fireItemValueModified(TaggingPresetItem source, String key, String newValue) {
141 listeners.fireEvent(e -> e.itemValueModified(source, key, newValue));
142 }
143}
Note: See TracBrowser for help on using the repository browser.