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