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

Last change on this file since 294 was 294, checked in by imi, 17 years ago
  • renamed annotation presets to tagging presets
File size: 4.1 KB
Line 
1package org.openstreetmap.josm.gui.preferences;
2
3import static org.openstreetmap.josm.tools.I18n.tr;
4
5import java.awt.GridBagLayout;
6import java.awt.event.ActionEvent;
7import java.awt.event.ActionListener;
8import java.util.Collection;
9import java.util.StringTokenizer;
10
11import javax.swing.Box;
12import javax.swing.DefaultListModel;
13import javax.swing.JButton;
14import javax.swing.JLabel;
15import javax.swing.JList;
16import javax.swing.JOptionPane;
17import javax.swing.JPanel;
18import javax.swing.JScrollPane;
19
20import org.openstreetmap.josm.Main;
21import org.openstreetmap.josm.gui.tagging.TaggingPreset;
22import org.openstreetmap.josm.tools.GBC;
23
24public class TaggingPresetPreference implements PreferenceSetting {
25
26 public static Collection<TaggingPreset> taggingPresets;
27 private JList taggingPresetSources;
28
29 public void addGui(final PreferenceDialog gui) {
30 taggingPresetSources = new JList(new DefaultListModel());
31 String annos = Main.pref.get("taggingpreset.sources");
32 StringTokenizer st = new StringTokenizer(annos, ";");
33 while (st.hasMoreTokens())
34 ((DefaultListModel)taggingPresetSources.getModel()).addElement(st.nextToken());
35
36 JButton addAnno = new JButton(tr("Add"));
37 addAnno.addActionListener(new ActionListener(){
38 public void actionPerformed(ActionEvent e) {
39 String source = JOptionPane.showInputDialog(Main.parent, tr("Tagging preset source"));
40 if (source == null)
41 return;
42 ((DefaultListModel)taggingPresetSources.getModel()).addElement(source);
43 gui.requiresRestart = true;
44 }
45 });
46
47 JButton editAnno = new JButton(tr("Edit"));
48 editAnno.addActionListener(new ActionListener(){
49 public void actionPerformed(ActionEvent e) {
50 if (taggingPresetSources.getSelectedIndex() == -1)
51 JOptionPane.showMessageDialog(Main.parent, tr("Please select the row to edit."));
52 else {
53 String source = JOptionPane.showInputDialog(Main.parent, tr("Tagging preset source"), taggingPresetSources.getSelectedValue());
54 if (source == null)
55 return;
56 ((DefaultListModel)taggingPresetSources.getModel()).setElementAt(source, taggingPresetSources.getSelectedIndex());
57 gui.requiresRestart = true;
58 }
59 }
60 });
61
62 JButton deleteAnno = new JButton(tr("Delete"));
63 deleteAnno.addActionListener(new ActionListener(){
64 public void actionPerformed(ActionEvent e) {
65 if (taggingPresetSources.getSelectedIndex() == -1)
66 JOptionPane.showMessageDialog(Main.parent, tr("Please select the row to delete."));
67 else {
68 ((DefaultListModel)taggingPresetSources.getModel()).remove(taggingPresetSources.getSelectedIndex());
69 gui.requiresRestart = true;
70 }
71 }
72 });
73 taggingPresetSources.setVisibleRowCount(3);
74
75 taggingPresetSources.setToolTipText(tr("The sources (url or filename) of tagging preset definition files. See http://josm.eigenheimstrasse.de/wiki/TaggingPresets for help."));
76 addAnno.setToolTipText(tr("Add a new tagging preset source to the list."));
77 deleteAnno.setToolTipText(tr("Delete the selected source from the list."));
78
79 gui.map.add(new JLabel(tr("Tagging preset sources")), GBC.eol().insets(0,5,0,0));
80 gui.map.add(new JScrollPane(taggingPresetSources), GBC.eol().fill(GBC.BOTH));
81 JPanel buttonPanel = new JPanel(new GridBagLayout());
82 gui.map.add(buttonPanel, GBC.eol().fill(GBC.HORIZONTAL));
83 buttonPanel.add(Box.createHorizontalGlue(), GBC.std().fill(GBC.HORIZONTAL));
84 buttonPanel.add(addAnno, GBC.std().insets(0,5,0,0));
85 buttonPanel.add(editAnno, GBC.std().insets(5,5,5,0));
86 buttonPanel.add(deleteAnno, GBC.std().insets(0,5,0,0));
87 }
88
89 public void ok() {
90 if (taggingPresetSources.getModel().getSize() > 0) {
91 StringBuilder sb = new StringBuilder();
92 for (int i = 0; i < taggingPresetSources.getModel().getSize(); ++i)
93 sb.append(";"+taggingPresetSources.getModel().getElementAt(i));
94 Main.pref.put("taggingpreset.sources", sb.toString().substring(1));
95 } else
96 Main.pref.put("taggingpreset.sources", null);
97 }
98
99 /**
100 * Initialize the tagging presets (load and may display error)
101 */
102 public static void initialize() {
103 taggingPresets = TaggingPreset.readFromPreferences();
104 }
105}
Note: See TracBrowser for help on using the repository browser.