source: josm/trunk/src/org/openstreetmap/josm/gui/tagging/presets/TaggingPresets.java@ 12537

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

PMD - VariableNamingConventions

  • Property svn:eol-style set to native
File size: 8.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.tagging.presets;
3
4import java.util.ArrayList;
5import java.util.Collection;
6import java.util.Collections;
7import java.util.HashMap;
8import java.util.HashSet;
9import java.util.Map;
10import java.util.Set;
11
12import javax.swing.JMenu;
13import javax.swing.JMenuItem;
14import javax.swing.JSeparator;
15
16import org.openstreetmap.josm.Main;
17import org.openstreetmap.josm.data.osm.OsmPrimitive;
18import org.openstreetmap.josm.gui.MenuScroller;
19import org.openstreetmap.josm.gui.tagging.presets.items.CheckGroup;
20import org.openstreetmap.josm.gui.tagging.presets.items.KeyedItem;
21import org.openstreetmap.josm.gui.tagging.presets.items.Roles;
22import org.openstreetmap.josm.gui.tagging.presets.items.Roles.Role;
23import org.openstreetmap.josm.tools.MultiMap;
24import org.openstreetmap.josm.tools.SubclassFilteredCollection;
25
26/**
27 * Class holding Tagging Presets and allowing to manage them.
28 * @since 7100
29 */
30public final class TaggingPresets {
31
32 /** The collection of tagging presets */
33 private static final Collection<TaggingPreset> TAGGING_PRESETS = new ArrayList<>();
34
35 /** cache for key/value pairs found in the preset */
36 private static final MultiMap<String, String> PRESET_TAG_CACHE = new MultiMap<>();
37 /** cache for roles found in the preset */
38 private static final Set<String> PRESET_ROLE_CACHE = new HashSet<>();
39
40 /** The collection of listeners */
41 private static final Collection<TaggingPresetListener> LISTENERS = new ArrayList<>();
42
43 private TaggingPresets() {
44 // Hide constructor for utility classes
45 }
46
47 /**
48 * Initializes tagging presets from preferences.
49 */
50 public static void readFromPreferences() {
51 TAGGING_PRESETS.clear();
52 TAGGING_PRESETS.addAll(TaggingPresetReader.readFromPreferences(false, false));
53 cachePresets(TAGGING_PRESETS);
54 }
55
56 /**
57 * Initialize the tagging presets (load and may display error)
58 */
59 public static void initialize() {
60 readFromPreferences();
61 for (TaggingPreset tp: TAGGING_PRESETS) {
62 if (!(tp instanceof TaggingPresetSeparator)) {
63 Main.toolbar.register(tp);
64 }
65 }
66 if (TAGGING_PRESETS.isEmpty()) {
67 Main.main.menu.presetsMenu.setVisible(false);
68 } else {
69 Map<TaggingPresetMenu, JMenu> submenus = new HashMap<>();
70 for (final TaggingPreset p : TAGGING_PRESETS) {
71 JMenu m = p.group != null ? submenus.get(p.group) : Main.main.menu.presetsMenu;
72 if (m == null && p.group != null) {
73 Main.error("No tagging preset submenu for " + p.group);
74 } else if (m == null) {
75 Main.error("No tagging preset menu. Tagging preset " + p + " won't be available there");
76 } else if (p instanceof TaggingPresetSeparator) {
77 m.add(new JSeparator());
78 } else if (p instanceof TaggingPresetMenu) {
79 JMenu submenu = new JMenu(p);
80 submenu.setText(p.getLocaleName());
81 ((TaggingPresetMenu) p).menu = submenu;
82 submenus.put((TaggingPresetMenu) p, submenu);
83 m.add(submenu);
84 } else {
85 JMenuItem mi = new JMenuItem(p);
86 mi.setText(p.getLocaleName());
87 m.add(mi);
88 }
89 }
90 for (JMenu submenu : submenus.values()) {
91 if (submenu.getItemCount() >= Main.pref.getInteger("taggingpreset.min-elements-for-scroller", 15)) {
92 MenuScroller.setScrollerFor(submenu);
93 }
94 }
95 }
96 if (Main.pref.getBoolean("taggingpreset.sortmenu")) {
97 TaggingPresetMenu.sortMenu(Main.main.menu.presetsMenu);
98 }
99 }
100
101 /**
102 * Initialize the cache for presets. This is done only once.
103 * @param presets Tagging presets to cache
104 */
105 public static void cachePresets(Collection<TaggingPreset> presets) {
106 for (final TaggingPreset p : presets) {
107 for (TaggingPresetItem item : p.data) {
108 cachePresetItem(p, item);
109 }
110 }
111 }
112
113 private static void cachePresetItem(TaggingPreset p, TaggingPresetItem item) {
114 if (item instanceof KeyedItem) {
115 KeyedItem ki = (KeyedItem) item;
116 if (ki.key != null && ki.getValues() != null) {
117 PRESET_TAG_CACHE.putAll(ki.key, ki.getValues());
118 }
119 } else if (item instanceof Roles) {
120 Roles r = (Roles) item;
121 for (Role i : r.roles) {
122 if (i.key != null) {
123 PRESET_ROLE_CACHE.add(i.key);
124 }
125 }
126 } else if (item instanceof CheckGroup) {
127 for (KeyedItem check : ((CheckGroup) item).checks) {
128 cachePresetItem(p, check);
129 }
130 }
131 }
132
133 /**
134 * Replies a new collection containing all tagging presets.
135 * @return a new collection containing all tagging presets. Empty if presets are not initialized (never null)
136 */
137 public static Collection<TaggingPreset> getTaggingPresets() {
138 return Collections.unmodifiableCollection(TAGGING_PRESETS);
139 }
140
141 /**
142 * Replies a set of all roles in the tagging presets.
143 * @return a set of all roles in the tagging presets.
144 */
145 public static Set<String> getPresetRoles() {
146 return Collections.unmodifiableSet(PRESET_ROLE_CACHE);
147 }
148
149 /**
150 * Replies a set of all keys in the tagging presets.
151 * @return a set of all keys in the tagging presets.
152 */
153 public static Set<String> getPresetKeys() {
154 return Collections.unmodifiableSet(PRESET_TAG_CACHE.keySet());
155 }
156
157 /**
158 * Return set of values for a key in the tagging presets
159 * @param key the key
160 * @return set of values for a key in the tagging presets or null if none is found
161 */
162 public static Set<String> getPresetValues(String key) {
163 Set<String> values = PRESET_TAG_CACHE.get(key);
164 if (values != null)
165 return Collections.unmodifiableSet(values);
166 return null;
167 }
168
169 /**
170 * Replies a new collection of all presets matching the parameters.
171 *
172 * @param t the preset types to include
173 * @param tags the tags to perform matching on, see {@link TaggingPresetItem#matches(Map)}
174 * @param onlyShowable whether only {@link TaggingPreset#isShowable() showable} presets should be returned
175 * @return a new collection of all presets matching the parameters.
176 * @see TaggingPreset#matches(Collection, Map, boolean)
177 * @since 9266
178 */
179 public static Collection<TaggingPreset> getMatchingPresets(final Collection<TaggingPresetType> t,
180 final Map<String, String> tags, final boolean onlyShowable) {
181 return SubclassFilteredCollection.filter(getTaggingPresets(), preset -> preset.matches(t, tags, onlyShowable));
182 }
183
184 /**
185 * Replies a new collection of all presets matching the given preset.
186 *
187 * @param primitive the primitive
188 * @return a new collection of all presets matching the given preset.
189 * @see TaggingPreset#test(OsmPrimitive)
190 * @since 9265
191 */
192 public static Collection<TaggingPreset> getMatchingPresets(final OsmPrimitive primitive) {
193 return SubclassFilteredCollection.filter(getTaggingPresets(), preset -> preset.test(primitive));
194 }
195
196 /**
197 * Adds a list of tagging presets to the current list.
198 * @param presets The tagging presets to add
199 */
200 public static void addTaggingPresets(Collection<TaggingPreset> presets) {
201 if (presets != null && TAGGING_PRESETS.addAll(presets)) {
202 for (TaggingPresetListener listener : LISTENERS) {
203 listener.taggingPresetsModified();
204 }
205 }
206 }
207
208 /**
209 * Adds a tagging preset listener.
210 * @param listener The listener to add
211 */
212 public static void addListener(TaggingPresetListener listener) {
213 if (listener != null) {
214 LISTENERS.add(listener);
215 }
216 }
217
218 /**
219 * Removes a tagging preset listener.
220 * @param listener The listener to remove
221 */
222 public static void removeListener(TaggingPresetListener listener) {
223 if (listener != null) {
224 LISTENERS.remove(listener);
225 }
226 }
227}
Note: See TracBrowser for help on using the repository browser.