1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.tagging.presets;
|
---|
3 |
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.Collection;
|
---|
6 | import java.util.HashMap;
|
---|
7 | import java.util.Map;
|
---|
8 |
|
---|
9 | import javax.swing.JMenu;
|
---|
10 | import javax.swing.JMenuItem;
|
---|
11 | import javax.swing.JSeparator;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.Main;
|
---|
14 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
15 | import org.openstreetmap.josm.gui.MenuScroller;
|
---|
16 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
|
---|
17 | import org.openstreetmap.josm.tools.SubclassFilteredCollection;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Class holding Tagging Presets and allowing to manage them.
|
---|
21 | * @since 7100
|
---|
22 | */
|
---|
23 | public final class TaggingPresets {
|
---|
24 |
|
---|
25 | /** The collection of tagging presets */
|
---|
26 | private static final Collection<TaggingPreset> taggingPresets = new ArrayList<>();
|
---|
27 |
|
---|
28 | /** The collection of listeners */
|
---|
29 | private static final Collection<TaggingPresetListener> listeners = new ArrayList<>();
|
---|
30 |
|
---|
31 | private TaggingPresets() {
|
---|
32 | // Hide constructor for utility classes
|
---|
33 | }
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Initializes tagging presets from preferences.
|
---|
37 | */
|
---|
38 | public static void readFromPreferences() {
|
---|
39 | taggingPresets.clear();
|
---|
40 | taggingPresets.addAll(TaggingPresetReader.readFromPreferences(false, false));
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Initialize the tagging presets (load and may display error)
|
---|
45 | */
|
---|
46 | public static void initialize() {
|
---|
47 | readFromPreferences();
|
---|
48 | for (TaggingPreset tp: taggingPresets) {
|
---|
49 | if (!(tp instanceof TaggingPresetSeparator)) {
|
---|
50 | Main.toolbar.register(tp);
|
---|
51 | }
|
---|
52 | }
|
---|
53 | if (taggingPresets.isEmpty()) {
|
---|
54 | Main.main.menu.presetsMenu.setVisible(false);
|
---|
55 | } else {
|
---|
56 | AutoCompletionManager.cachePresets(taggingPresets);
|
---|
57 | Map<TaggingPresetMenu, JMenu> submenus = new HashMap<>();
|
---|
58 | for (final TaggingPreset p : taggingPresets) {
|
---|
59 | JMenu m = p.group != null ? submenus.get(p.group) : Main.main.menu.presetsMenu;
|
---|
60 | if (m == null && p.group != null) {
|
---|
61 | Main.error("No tagging preset submenu for " + p.group);
|
---|
62 | } else if (m == null) {
|
---|
63 | Main.error("No tagging preset menu. Tagging preset " + p + " won't be available there");
|
---|
64 | } else if (p instanceof TaggingPresetSeparator) {
|
---|
65 | m.add(new JSeparator());
|
---|
66 | } else if (p instanceof TaggingPresetMenu) {
|
---|
67 | JMenu submenu = new JMenu(p);
|
---|
68 | submenu.setText(p.getLocaleName());
|
---|
69 | ((TaggingPresetMenu) p).menu = submenu;
|
---|
70 | submenus.put((TaggingPresetMenu) p, submenu);
|
---|
71 | m.add(submenu);
|
---|
72 | } else {
|
---|
73 | JMenuItem mi = new JMenuItem(p);
|
---|
74 | mi.setText(p.getLocaleName());
|
---|
75 | m.add(mi);
|
---|
76 | }
|
---|
77 | }
|
---|
78 | for (JMenu submenu : submenus.values()) {
|
---|
79 | if (submenu.getItemCount() >= Main.pref.getInteger("taggingpreset.min-elements-for-scroller", 15)) {
|
---|
80 | MenuScroller.setScrollerFor(submenu);
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|
84 | if (Main.pref.getBoolean("taggingpreset.sortmenu")) {
|
---|
85 | TaggingPresetMenu.sortMenu(Main.main.menu.presetsMenu);
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Replies a new collection containing all tagging presets.
|
---|
91 | * @return a new collection containing all tagging presets. Empty if presets are not initialized (never null)
|
---|
92 | */
|
---|
93 | public static Collection<TaggingPreset> getTaggingPresets() {
|
---|
94 | return new ArrayList<>(taggingPresets);
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Replies a new collection of all presets matching the parameters.
|
---|
99 | *
|
---|
100 | * @param t the preset types to include
|
---|
101 | * @param tags the tags to perform matching on, see {@link TaggingPresetItem#matches(Map)}
|
---|
102 | * @param onlyShowable whether only {@link TaggingPreset#isShowable() showable} presets should be returned
|
---|
103 | * @return a new collection of all presets matching the parameters.
|
---|
104 | * @see TaggingPreset#matches(Collection, Map, boolean)
|
---|
105 | * @since 9266
|
---|
106 | */
|
---|
107 | public static Collection<TaggingPreset> getMatchingPresets(final Collection<TaggingPresetType> t,
|
---|
108 | final Map<String, String> tags, final boolean onlyShowable) {
|
---|
109 | return SubclassFilteredCollection.filter(getTaggingPresets(), preset -> preset.matches(t, tags, onlyShowable));
|
---|
110 | }
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Replies a new collection of all presets matching the given preset.
|
---|
114 | *
|
---|
115 | * @param primitive the primitive
|
---|
116 | * @return a new collection of all presets matching the given preset.
|
---|
117 | * @see TaggingPreset#evaluate(OsmPrimitive)
|
---|
118 | * @since 9265
|
---|
119 | */
|
---|
120 | public static Collection<TaggingPreset> getMatchingPresets(final OsmPrimitive primitive) {
|
---|
121 | return SubclassFilteredCollection.filter(getTaggingPresets(), preset -> preset.evaluate(primitive));
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Adds a list of tagging presets to the current list.
|
---|
126 | * @param presets The tagging presets to add
|
---|
127 | */
|
---|
128 | public static void addTaggingPresets(Collection<TaggingPreset> presets) {
|
---|
129 | if (presets != null) {
|
---|
130 | if (taggingPresets.addAll(presets)) {
|
---|
131 | for (TaggingPresetListener listener : listeners) {
|
---|
132 | listener.taggingPresetsModified();
|
---|
133 | }
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Adds a tagging preset listener.
|
---|
140 | * @param listener The listener to add
|
---|
141 | */
|
---|
142 | public static void addListener(TaggingPresetListener listener) {
|
---|
143 | if (listener != null) {
|
---|
144 | listeners.add(listener);
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Removes a tagging preset listener.
|
---|
150 | * @param listener The listener to remove
|
---|
151 | */
|
---|
152 | public static void removeListener(TaggingPresetListener listener) {
|
---|
153 | if (listener != null) {
|
---|
154 | listeners.remove(listener);
|
---|
155 | }
|
---|
156 | }
|
---|
157 | }
|
---|