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.Color;
|
---|
7 | import java.awt.GridBagLayout;
|
---|
8 | import java.awt.event.ActionEvent;
|
---|
9 | import java.awt.event.ActionListener;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.HashMap;
|
---|
12 | import java.util.StringTokenizer;
|
---|
13 |
|
---|
14 | import javax.swing.Action;
|
---|
15 | import javax.swing.BorderFactory;
|
---|
16 | import javax.swing.Box;
|
---|
17 | import javax.swing.DefaultListModel;
|
---|
18 | import javax.swing.JButton;
|
---|
19 | import javax.swing.JCheckBox;
|
---|
20 | import javax.swing.JLabel;
|
---|
21 | import javax.swing.JList;
|
---|
22 | import javax.swing.JMenu;
|
---|
23 | import javax.swing.JMenuItem;
|
---|
24 | import javax.swing.JOptionPane;
|
---|
25 | import javax.swing.JPanel;
|
---|
26 | import javax.swing.JScrollPane;
|
---|
27 | import javax.swing.JSeparator;
|
---|
28 |
|
---|
29 | import org.openstreetmap.josm.Main;
|
---|
30 | import org.openstreetmap.josm.gui.tagging.TaggingPreset;
|
---|
31 | import org.openstreetmap.josm.gui.tagging.TaggingPresetMenu;
|
---|
32 | import org.openstreetmap.josm.gui.tagging.TaggingPresetSeperator;
|
---|
33 | import org.openstreetmap.josm.tools.GBC;
|
---|
34 |
|
---|
35 | public class TaggingPresetPreference implements PreferenceSetting {
|
---|
36 |
|
---|
37 | public static Collection<TaggingPreset> taggingPresets;
|
---|
38 | private JList taggingPresetSources;
|
---|
39 | private JCheckBox enableDefault;
|
---|
40 |
|
---|
41 | public void addGui(final PreferenceDialog gui) {
|
---|
42 |
|
---|
43 | taggingPresetSources = new JList(new DefaultListModel());
|
---|
44 | enableDefault = new JCheckBox(tr("Enable built-in defaults"),
|
---|
45 | Main.pref.getBoolean("taggingpreset.enable-defaults", true));
|
---|
46 |
|
---|
47 | String annos = Main.pref.get("taggingpreset.sources");
|
---|
48 | StringTokenizer st = new StringTokenizer(annos, ";");
|
---|
49 | while (st.hasMoreTokens())
|
---|
50 | ((DefaultListModel)taggingPresetSources.getModel()).addElement(st.nextToken());
|
---|
51 |
|
---|
52 | JButton addAnno = new JButton(tr("Add"));
|
---|
53 | addAnno.addActionListener(new ActionListener(){
|
---|
54 | public void actionPerformed(ActionEvent e) {
|
---|
55 | String source = JOptionPane.showInputDialog(Main.parent, tr("Tagging preset source"));
|
---|
56 | if (source == null)
|
---|
57 | return;
|
---|
58 | ((DefaultListModel)taggingPresetSources.getModel()).addElement(source);
|
---|
59 | gui.requiresRestart = true;
|
---|
60 | }
|
---|
61 | });
|
---|
62 |
|
---|
63 | JButton editAnno = new JButton(tr("Edit"));
|
---|
64 | editAnno.addActionListener(new ActionListener(){
|
---|
65 | public void actionPerformed(ActionEvent e) {
|
---|
66 | if (taggingPresetSources.getSelectedIndex() == -1)
|
---|
67 | JOptionPane.showMessageDialog(Main.parent, tr("Please select the row to edit."));
|
---|
68 | else {
|
---|
69 | String source = JOptionPane.showInputDialog(Main.parent, tr("Tagging preset source"), taggingPresetSources.getSelectedValue());
|
---|
70 | if (source == null)
|
---|
71 | return;
|
---|
72 | ((DefaultListModel)taggingPresetSources.getModel()).setElementAt(source, taggingPresetSources.getSelectedIndex());
|
---|
73 | gui.requiresRestart = true;
|
---|
74 | }
|
---|
75 | }
|
---|
76 | });
|
---|
77 |
|
---|
78 | JButton deleteAnno = new JButton(tr("Delete"));
|
---|
79 | deleteAnno.addActionListener(new ActionListener(){
|
---|
80 | public void actionPerformed(ActionEvent e) {
|
---|
81 | if (taggingPresetSources.getSelectedIndex() == -1)
|
---|
82 | JOptionPane.showMessageDialog(Main.parent, tr("Please select the row to delete."));
|
---|
83 | else {
|
---|
84 | ((DefaultListModel)taggingPresetSources.getModel()).remove(taggingPresetSources.getSelectedIndex());
|
---|
85 | gui.requiresRestart = true;
|
---|
86 | }
|
---|
87 | }
|
---|
88 | });
|
---|
89 | taggingPresetSources.setVisibleRowCount(3);
|
---|
90 |
|
---|
91 | taggingPresetSources.setToolTipText(tr("The sources (url or filename) of tagging preset definition files. See http://josm.openstreetmap.de/wiki/TaggingPresets for help."));
|
---|
92 | addAnno.setToolTipText(tr("Add a new tagging preset source to the list."));
|
---|
93 | deleteAnno.setToolTipText(tr("Delete the selected source from the list."));
|
---|
94 |
|
---|
95 | JPanel tpPanel = new JPanel();
|
---|
96 | tpPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.gray), tr("Tagging Presets")));
|
---|
97 | tpPanel.setLayout(new GridBagLayout());
|
---|
98 | tpPanel.add(enableDefault, GBC.eol().insets(5,5,5,0));
|
---|
99 | tpPanel.add(new JLabel(tr("Tagging preset sources")), GBC.eol().insets(5,5,5,0));
|
---|
100 | tpPanel.add(new JScrollPane(taggingPresetSources), GBC.eol().insets(5,0,5,0).fill(GBC.BOTH));
|
---|
101 | JPanel buttonPanel = new JPanel(new GridBagLayout());
|
---|
102 | tpPanel.add(buttonPanel, GBC.eol().insets(5,0,5,5).fill(GBC.HORIZONTAL));
|
---|
103 | buttonPanel.add(Box.createHorizontalGlue(), GBC.std().fill(GBC.HORIZONTAL));
|
---|
104 | buttonPanel.add(addAnno, GBC.std().insets(0,5,0,0));
|
---|
105 | buttonPanel.add(editAnno, GBC.std().insets(5,5,5,0));
|
---|
106 | buttonPanel.add(deleteAnno, GBC.std().insets(0,5,0,0));
|
---|
107 | gui.map.add(tpPanel, GBC.eol().fill(GBC.BOTH));
|
---|
108 | }
|
---|
109 |
|
---|
110 | public void ok() {
|
---|
111 | Main.pref.put("taggingpreset.enable-defaults", enableDefault.getSelectedObjects() != null);
|
---|
112 | if (taggingPresetSources.getModel().getSize() > 0) {
|
---|
113 | StringBuilder sb = new StringBuilder();
|
---|
114 | for (int i = 0; i < taggingPresetSources.getModel().getSize(); ++i)
|
---|
115 | sb.append(";"+taggingPresetSources.getModel().getElementAt(i));
|
---|
116 | Main.pref.put("taggingpreset.sources", sb.toString().substring(1));
|
---|
117 | } else
|
---|
118 | Main.pref.put("taggingpreset.sources", null);
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Initialize the tagging presets (load and may display error)
|
---|
123 | */
|
---|
124 | public static void initialize() {
|
---|
125 | taggingPresets = TaggingPreset.readFromPreferences();
|
---|
126 | if (taggingPresets.isEmpty()) {
|
---|
127 | Main.main.menu.presetsMenu.setVisible(false);
|
---|
128 | }
|
---|
129 | else
|
---|
130 | {
|
---|
131 | HashMap<TaggingPresetMenu,JMenu> submenus = new HashMap<TaggingPresetMenu,JMenu>();
|
---|
132 | for (final TaggingPreset p : taggingPresets) {
|
---|
133 | String name = (String) p.getValue(Action.NAME);
|
---|
134 | if (p instanceof TaggingPresetSeperator) {
|
---|
135 | if(p.group != null)
|
---|
136 | submenus.get(p.group).add(new JSeparator());
|
---|
137 | else
|
---|
138 | Main.main.menu.presetsMenu.add(new JSeparator());
|
---|
139 | }
|
---|
140 | else if (p instanceof TaggingPresetMenu)
|
---|
141 | {
|
---|
142 | JMenu submenu = new JMenu(p);
|
---|
143 | submenus.put((TaggingPresetMenu)p, submenu);
|
---|
144 | Main.main.menu.presetsMenu.add(submenu);
|
---|
145 | } else {
|
---|
146 | if(p.group != null)
|
---|
147 | submenus.get(p.group).add(p);
|
---|
148 | else
|
---|
149 | Main.main.menu.presetsMenu.add(new JMenuItem(p));
|
---|
150 | }
|
---|
151 | }
|
---|
152 | }
|
---|
153 | }
|
---|
154 | }
|
---|