1 | package annotationtester;
|
---|
2 |
|
---|
3 | import java.awt.BorderLayout;
|
---|
4 | import java.awt.event.ActionEvent;
|
---|
5 | import java.awt.event.ActionListener;
|
---|
6 | import java.io.FileInputStream;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.io.InputStream;
|
---|
9 | import java.net.URL;
|
---|
10 | import java.util.Vector;
|
---|
11 |
|
---|
12 | import javax.swing.BorderFactory;
|
---|
13 | import javax.swing.DefaultComboBoxModel;
|
---|
14 | import javax.swing.JButton;
|
---|
15 | import javax.swing.JComboBox;
|
---|
16 | import javax.swing.JFileChooser;
|
---|
17 | import javax.swing.JFrame;
|
---|
18 | import javax.swing.JOptionPane;
|
---|
19 | import javax.swing.JPanel;
|
---|
20 |
|
---|
21 | import org.openstreetmap.josm.gui.annotation.AnnotationCellRenderer;
|
---|
22 | import org.openstreetmap.josm.gui.annotation.AnnotationPreset;
|
---|
23 | import org.xml.sax.SAXException;
|
---|
24 |
|
---|
25 | public class AnnotationTester extends JFrame {
|
---|
26 |
|
---|
27 | private JComboBox annotationPresets;
|
---|
28 | private final String[] args;
|
---|
29 | private JPanel annotationPanel = new JPanel(new BorderLayout());
|
---|
30 | private JPanel panel = new JPanel(new BorderLayout());
|
---|
31 |
|
---|
32 | public void reload() {
|
---|
33 | Vector<AnnotationPreset> allPresets = new Vector<AnnotationPreset>();
|
---|
34 | for (String source : args) {
|
---|
35 | InputStream in = null;
|
---|
36 | try {
|
---|
37 | if (source.startsWith("http") || source.startsWith("ftp") || source.startsWith("file"))
|
---|
38 | in = new URL(source).openStream();
|
---|
39 | else if (source.startsWith("resource://"))
|
---|
40 | in = AnnotationTester.class.getResourceAsStream(source.substring("resource:/".length()));
|
---|
41 | else
|
---|
42 | in = new FileInputStream(source);
|
---|
43 | allPresets.addAll(AnnotationPreset.readAll(in));
|
---|
44 | } catch (IOException e) {
|
---|
45 | e.printStackTrace();
|
---|
46 | JOptionPane.showMessageDialog(null, "Could not read annotation preset source: "+source);
|
---|
47 | } catch (SAXException e) {
|
---|
48 | e.printStackTrace();
|
---|
49 | JOptionPane.showMessageDialog(null, "Error parsing "+source+": "+e.getMessage());
|
---|
50 | }
|
---|
51 |
|
---|
52 | try {
|
---|
53 | if (in != null)
|
---|
54 | in.close();
|
---|
55 | } catch (IOException e) {
|
---|
56 | }
|
---|
57 | }
|
---|
58 | annotationPresets.setModel(new DefaultComboBoxModel(allPresets));
|
---|
59 | }
|
---|
60 |
|
---|
61 | public void reselect() {
|
---|
62 | annotationPanel.removeAll();
|
---|
63 | AnnotationPreset preset = (AnnotationPreset)annotationPresets.getSelectedItem();
|
---|
64 | if (preset == null)
|
---|
65 | return;
|
---|
66 | JPanel p = preset.createPanel();
|
---|
67 | p.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
68 | if (p != null)
|
---|
69 | annotationPanel.add(p, BorderLayout.NORTH);
|
---|
70 | panel.validate();
|
---|
71 | panel.repaint();
|
---|
72 | }
|
---|
73 |
|
---|
74 | public AnnotationTester(String[] args) {
|
---|
75 | super("Annotation Preset Tester");
|
---|
76 | this.args = args;
|
---|
77 | annotationPresets = new JComboBox();
|
---|
78 | annotationPresets.setRenderer(new AnnotationCellRenderer());
|
---|
79 | reload();
|
---|
80 |
|
---|
81 | panel.add(annotationPresets, BorderLayout.NORTH);
|
---|
82 | panel.add(annotationPanel, BorderLayout.CENTER);
|
---|
83 | annotationPresets.addActionListener(new ActionListener(){
|
---|
84 | public void actionPerformed(ActionEvent e) {
|
---|
85 | reselect();
|
---|
86 | }
|
---|
87 | });
|
---|
88 | reselect();
|
---|
89 |
|
---|
90 | JButton b = new JButton("Reload");
|
---|
91 | b.addActionListener(new ActionListener(){
|
---|
92 | public void actionPerformed(ActionEvent e) {
|
---|
93 | int i = annotationPresets.getSelectedIndex();
|
---|
94 | reload();
|
---|
95 | annotationPresets.setSelectedIndex(i);
|
---|
96 | }
|
---|
97 | });
|
---|
98 | panel.add(b, BorderLayout.SOUTH);
|
---|
99 |
|
---|
100 | setContentPane(panel);
|
---|
101 | setSize(300,500);
|
---|
102 | setVisible(true);
|
---|
103 | }
|
---|
104 |
|
---|
105 | public static void main(String[] args) {
|
---|
106 | if (args.length == 0) {
|
---|
107 | JFileChooser c = new JFileChooser();
|
---|
108 | if (c.showOpenDialog(null) != JFileChooser.APPROVE_OPTION)
|
---|
109 | return;
|
---|
110 | args = new String[]{c.getSelectedFile().getPath()};
|
---|
111 | }
|
---|
112 | JFrame f = new AnnotationTester(args);
|
---|
113 | f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
---|
114 | }
|
---|
115 | }
|
---|