source: josm/trunk/src/org/openstreetmap/josm/gui/preferences/TaggingPresetPreference.java@ 3128

Last change on this file since 3128 was 2946, checked in by bastiK, 14 years ago

autocompletion: Always add the preset keys/values to the autocompletion list.
Does not apply to the Properties dialog, because it has a seperate implementation of the autocompletion feature.

  • Property svn:eol-style set to native
File size: 4.4 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.gui.preferences;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.GridBagLayout;
7import java.util.Collection;
8import java.util.HashMap;
9
10import javax.swing.BorderFactory;
11import javax.swing.JCheckBox;
12import javax.swing.JMenu;
13import javax.swing.JMenuItem;
14import javax.swing.JPanel;
15import javax.swing.JSeparator;
16import javax.swing.event.ChangeEvent;
17import javax.swing.event.ChangeListener;
18
19import org.openstreetmap.josm.Main;
20import org.openstreetmap.josm.gui.tagging.TaggingPreset;
21import org.openstreetmap.josm.gui.tagging.TaggingPresetMenu;
22import org.openstreetmap.josm.gui.tagging.TaggingPresetSeparator;
23import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionCache;
24import org.openstreetmap.josm.tools.GBC;
25
26public class TaggingPresetPreference implements PreferenceSetting {
27
28 public static class Factory implements PreferenceSettingFactory {
29 public PreferenceSetting createPreferenceSetting() {
30 return new TaggingPresetPreference();
31 }
32 }
33
34 public static Collection<TaggingPreset> taggingPresets;
35 private StyleSourceEditor sources;
36 private JCheckBox sortMenu;
37 private JCheckBox enableDefault;
38
39 public void addGui(final PreferenceTabbedPane gui) {
40 sortMenu = new JCheckBox(tr("Sort presets menu"),
41 Main.pref.getBoolean("taggingpreset.sortmenu", false));
42 enableDefault = new JCheckBox(tr("Enable built-in defaults"),
43 Main.pref.getBoolean("taggingpreset.enable-defaults", true));
44
45 final JPanel panel = new JPanel(new GridBagLayout());
46 panel.setBorder(BorderFactory.createEmptyBorder( 0, 0, 0, 0 ));
47 panel.add(sortMenu, GBC.eol().insets(5,5,5,0));
48 panel.add(enableDefault, GBC.eol().insets(5,0,5,0));
49 sources = new StyleSourceEditor("taggingpreset.sources", "taggingpreset.icon.sources",
50 "http://josm.openstreetmap.de/presets");
51 panel.add(sources, GBC.eol().fill(GBC.BOTH));
52 gui.mapcontent.addTab(tr("Tagging Presets"), panel);
53
54 // this defers loading of tagging preset sources to the first time the tab
55 // with the tagging presets is selected by the user
56 //
57 gui.mapcontent.addChangeListener(
58 new ChangeListener() {
59 public void stateChanged(ChangeEvent e) {
60 if (gui.mapcontent.getSelectedComponent() == panel) {
61 sources.initiallyLoadAvailableStyles();
62 }
63 }
64 }
65 );
66 }
67
68 public boolean ok() {
69 boolean restart = Main.pref.put("taggingpreset.enable-defaults",
70 enableDefault.getSelectedObjects() != null);
71 if(Main.pref.put("taggingpreset.sortmenu", sortMenu.getSelectedObjects() != null)) {
72 restart = true;
73 }
74 if(sources.finish()) {
75 restart = true;
76 }
77 return restart;
78 }
79
80 /**
81 * Initialize the tagging presets (load and may display error)
82 */
83 public static void initialize() {
84 taggingPresets = TaggingPreset.readFromPreferences();
85 if (taggingPresets.isEmpty()) {
86 Main.main.menu.presetsMenu.setVisible(false);
87 }
88 else
89 {
90 AutoCompletionCache.cachePresets(taggingPresets);
91 HashMap<TaggingPresetMenu,JMenu> submenus = new HashMap<TaggingPresetMenu,JMenu>();
92 for (final TaggingPreset p : taggingPresets)
93 {
94 JMenu m = p.group != null ? submenus.get(p.group) : Main.main.menu.presetsMenu;
95 if (p instanceof TaggingPresetSeparator) {
96 m.add(new JSeparator());
97 } else if (p instanceof TaggingPresetMenu)
98 {
99 JMenu submenu = new JMenu(p);
100 submenu.setText(p.getLocaleName());
101 ((TaggingPresetMenu)p).menu = submenu;
102 submenus.put((TaggingPresetMenu)p, submenu);
103 m.add(submenu);
104 }
105 else
106 {
107 JMenuItem mi = new JMenuItem(p);
108 mi.setText(p.getLocaleName());
109 m.add(mi);
110 }
111 }
112 }
113 if(Main.pref.getBoolean("taggingpreset.sortmenu")) {
114 TaggingPresetMenu.sortMenu(Main.main.menu.presetsMenu);
115 }
116 }
117}
Note: See TracBrowser for help on using the repository browser.