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

Last change on this file since 7937 was 7937, checked in by bastiK, 9 years ago

add subversion property svn:eol=native

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