1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.tagging;
|
---|
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.gui.tagging.ac.AutoCompletionManager;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Class holding Tagging Presets and allowing to manage them.
|
---|
18 | * @since 7100
|
---|
19 | */
|
---|
20 | public final class TaggingPresets {
|
---|
21 |
|
---|
22 | /** The collection of tagging presets */
|
---|
23 | private static final Collection<TaggingPreset> taggingPresets = new ArrayList<>();
|
---|
24 |
|
---|
25 | /** The collection of listeners */
|
---|
26 | private static final Collection<TaggingPresetListener> listeners = new ArrayList<>();
|
---|
27 |
|
---|
28 | private TaggingPresets() {
|
---|
29 | // Hide constructor for utility classes
|
---|
30 | }
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Initializes tagging presets from preferences.
|
---|
34 | */
|
---|
35 | public static void readFromPreferences() {
|
---|
36 | taggingPresets.clear();
|
---|
37 | taggingPresets.addAll(TaggingPresetReader.readFromPreferences(false, false));
|
---|
38 | }
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Initialize the tagging presets (load and may display error)
|
---|
42 | */
|
---|
43 | public static void initialize() {
|
---|
44 | readFromPreferences();
|
---|
45 | for (TaggingPreset tp: taggingPresets) {
|
---|
46 | if (!(tp instanceof TaggingPresetSeparator)) {
|
---|
47 | Main.toolbar.register(tp);
|
---|
48 | }
|
---|
49 | }
|
---|
50 | if (taggingPresets.isEmpty()) {
|
---|
51 | Main.main.menu.presetsMenu.setVisible(false);
|
---|
52 | } else {
|
---|
53 | AutoCompletionManager.cachePresets(taggingPresets);
|
---|
54 | Map<TaggingPresetMenu,JMenu> submenus = new HashMap<>();
|
---|
55 | for (final TaggingPreset p : taggingPresets) {
|
---|
56 | JMenu m = p.group != null ? submenus.get(p.group) : Main.main.menu.presetsMenu;
|
---|
57 | if (p instanceof TaggingPresetSeparator) {
|
---|
58 | m.add(new JSeparator());
|
---|
59 | } else if (p instanceof TaggingPresetMenu) {
|
---|
60 | JMenu submenu = new JMenu(p);
|
---|
61 | submenu.setText(p.getLocaleName());
|
---|
62 | ((TaggingPresetMenu)p).menu = submenu;
|
---|
63 | submenus.put((TaggingPresetMenu)p, submenu);
|
---|
64 | m.add(submenu);
|
---|
65 | } else {
|
---|
66 | JMenuItem mi = new JMenuItem(p);
|
---|
67 | mi.setText(p.getLocaleName());
|
---|
68 | m.add(mi);
|
---|
69 | }
|
---|
70 | }
|
---|
71 | }
|
---|
72 | if (Main.pref.getBoolean("taggingpreset.sortmenu")) {
|
---|
73 | TaggingPresetMenu.sortMenu(Main.main.menu.presetsMenu);
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Replies a new collection containing all tagging presets.
|
---|
79 | * @return a new collection containing all tagging presets. Empty if presets are not initialized (never null)
|
---|
80 | */
|
---|
81 | public static Collection<TaggingPreset> getTaggingPresets() {
|
---|
82 | return new ArrayList<>(taggingPresets);
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Adds a list of tagging presets to the current list.
|
---|
87 | * @param presets The tagging presets to add
|
---|
88 | */
|
---|
89 | public static void addTaggingPresets(Collection<TaggingPreset> presets) {
|
---|
90 | if (presets != null) {
|
---|
91 | if (taggingPresets.addAll(presets)) {
|
---|
92 | for (TaggingPresetListener listener : listeners) {
|
---|
93 | listener.taggingPresetsModified();
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Adds a tagging preset listener.
|
---|
101 | * @param listener The listener to add
|
---|
102 | */
|
---|
103 | public static void addListener(TaggingPresetListener listener) {
|
---|
104 | if (listener != null) {
|
---|
105 | listeners.add(listener);
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Removes a tagging preset listener.
|
---|
111 | * @param listener The listener to remove
|
---|
112 | */
|
---|
113 | public static void removeListener(TaggingPresetListener listener) {
|
---|
114 | if (listener != null) {
|
---|
115 | listeners.remove(listener);
|
---|
116 | }
|
---|
117 | }
|
---|
118 | }
|
---|