001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.tagging; 003 004import java.util.ArrayList; 005import java.util.Collection; 006import java.util.HashMap; 007 008import javax.swing.JMenu; 009import javax.swing.JMenuItem; 010import javax.swing.JSeparator; 011 012import org.openstreetmap.josm.Main; 013import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager; 014 015/** 016 * Class holding Tagging Presets and allowing to manage them. 017 * @since 7100 018 */ 019public final class TaggingPresets { 020 021 /** The collection of tagging presets */ 022 private static final Collection<TaggingPreset> taggingPresets = new ArrayList<>(); 023 024 /** The collection of listeners */ 025 private static final Collection<TaggingPresetListener> listeners = new ArrayList<>(); 026 027 private TaggingPresets() { 028 // Hide constructor for utility classes 029 } 030 031 /** 032 * Initializes tagging presets from preferences. 033 */ 034 public static void readFromPreferences() { 035 taggingPresets.clear(); 036 taggingPresets.addAll(TaggingPresetReader.readFromPreferences(false, false)); 037 } 038 039 /** 040 * Initialize the tagging presets (load and may display error) 041 */ 042 public static void initialize() { 043 readFromPreferences(); 044 for (TaggingPreset tp: taggingPresets) { 045 if (!(tp instanceof TaggingPresetSeparator)) { 046 Main.toolbar.register(tp); 047 } 048 } 049 if (taggingPresets.isEmpty()) { 050 Main.main.menu.presetsMenu.setVisible(false); 051 } else { 052 AutoCompletionManager.cachePresets(taggingPresets); 053 HashMap<TaggingPresetMenu,JMenu> submenus = new HashMap<>(); 054 for (final TaggingPreset p : taggingPresets) { 055 JMenu m = p.group != null ? submenus.get(p.group) : Main.main.menu.presetsMenu; 056 if (p instanceof TaggingPresetSeparator) { 057 m.add(new JSeparator()); 058 } else if (p instanceof TaggingPresetMenu) { 059 JMenu submenu = new JMenu(p); 060 submenu.setText(p.getLocaleName()); 061 ((TaggingPresetMenu)p).menu = submenu; 062 submenus.put((TaggingPresetMenu)p, submenu); 063 m.add(submenu); 064 } else { 065 JMenuItem mi = new JMenuItem(p); 066 mi.setText(p.getLocaleName()); 067 m.add(mi); 068 } 069 } 070 } 071 if (Main.pref.getBoolean("taggingpreset.sortmenu")) { 072 TaggingPresetMenu.sortMenu(Main.main.menu.presetsMenu); 073 } 074 } 075 076 /** 077 * Replies a new collection containing all tagging presets. 078 * @return a new collection containing all tagging presets. Empty if presets are not initialized (never null) 079 */ 080 public static Collection<TaggingPreset> getTaggingPresets() { 081 return new ArrayList<>(taggingPresets); 082 } 083 084 /** 085 * Adds a list of tagging presets to the current list. 086 * @param presets The tagging presets to add 087 */ 088 public static void addTaggingPresets(Collection<TaggingPreset> presets) { 089 if (presets != null) { 090 if (taggingPresets.addAll(presets)) { 091 for (TaggingPresetListener listener : listeners) { 092 listener.taggingPresetsModified(); 093 } 094 } 095 } 096 } 097 098 /** 099 * 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}