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