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