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