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

Last change on this file since 397 was 397, checked in by framm, 17 years ago
  • added a built-in set of tagging presets. this can be disabled from preferences panel, or replaced with an own control file.
File size: 5.1 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.gui.preferences;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Color;
7import java.awt.GridBagLayout;
8import java.awt.event.ActionEvent;
9import java.awt.event.ActionListener;
10import java.util.Collection;
11import java.util.StringTokenizer;
12
13import javax.swing.Action;
14import javax.swing.BorderFactory;
15import javax.swing.Box;
16import javax.swing.DefaultListModel;
17import javax.swing.JButton;
18import javax.swing.JCheckBox;
19import javax.swing.JLabel;
20import javax.swing.JList;
21import javax.swing.JMenuItem;
22import javax.swing.JOptionPane;
23import javax.swing.JPanel;
24import javax.swing.JScrollPane;
25import javax.swing.JSeparator;
26import javax.swing.border.BevelBorder;
27
28import org.openstreetmap.josm.Main;
29import org.openstreetmap.josm.gui.tagging.TaggingPreset;
30import org.openstreetmap.josm.tools.GBC;
31
32public 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}
Note: See TracBrowser for help on using the repository browser.