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

Last change on this file since 824 was 824, checked in by stoecker, 16 years ago

fixed tagging presets touse proper XML makeing translations much easier

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