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

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