source: osm/utils/josm/plugins/annotation-tester/src/annotationtester/AnnotationTester.java@ 1467

Last change on this file since 1467 was 1467, checked in by imi, 18 years ago

added josm translation plugins and annotation-tester plugin

File size: 3.4 KB
Line 
1package annotationtester;
2
3import java.awt.BorderLayout;
4import java.awt.event.ActionEvent;
5import java.awt.event.ActionListener;
6import java.io.FileInputStream;
7import java.io.IOException;
8import java.io.InputStream;
9import java.net.URL;
10import java.util.Vector;
11
12import javax.swing.BorderFactory;
13import javax.swing.DefaultComboBoxModel;
14import javax.swing.JButton;
15import javax.swing.JComboBox;
16import javax.swing.JFileChooser;
17import javax.swing.JFrame;
18import javax.swing.JOptionPane;
19import javax.swing.JPanel;
20
21import org.openstreetmap.josm.gui.annotation.AnnotationCellRenderer;
22import org.openstreetmap.josm.gui.annotation.AnnotationPreset;
23import org.xml.sax.SAXException;
24
25public 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}
Note: See TracBrowser for help on using the repository browser.