altos: Add gyro-based orientation tracking
[fw/altos] / altosui / AltosCSVUI.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package altosui;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import java.io.*;
24 import org.altusmetrum.altoslib_2.*;
25 import org.altusmetrum.altosuilib_1.*;
26
27 public class AltosCSVUI
28         extends AltosUIDialog
29         implements ActionListener
30 {
31         JFileChooser            csv_chooser;
32         JPanel                  accessory;
33         JComboBox               combo_box;
34         Iterable<AltosState>    states;
35         AltosWriter             writer;
36
37         static String[]         combo_box_items = { "Comma Separated Values (.CSV)", "Googleearth Data (.KML)" };
38
39         void set_default_file() {
40                 File    current = csv_chooser.getSelectedFile();
41                 String  current_name = current.getName();
42                 String  new_name = null;
43                 String  selected = (String) combo_box.getSelectedItem();
44
45                 if (selected.contains("CSV"))
46                         new_name = Altos.replace_extension(current_name, ".csv");
47                 else if (selected.contains("KML"))
48                         new_name = Altos.replace_extension(current_name, ".kml");
49                 if (new_name != null)
50                         csv_chooser.setSelectedFile(new File(new_name));
51         }
52
53         public void actionPerformed(ActionEvent e) {
54                 if (e.getActionCommand().equals("comboBoxChanged"))
55                         set_default_file();
56         }
57
58         public AltosCSVUI(JFrame frame, AltosStateIterable states, File source_file) {
59                 this.states = states;
60                 csv_chooser = new JFileChooser(source_file);
61
62                 accessory = new JPanel();
63                 accessory.setLayout(new GridBagLayout());
64
65                 GridBagConstraints      c = new GridBagConstraints();
66                 c.fill = GridBagConstraints.NONE;
67                 c.weightx = 1;
68                 c.weighty = 0;
69                 c.insets = new Insets (4, 4, 4, 4);
70
71                 JLabel accessory_label = new JLabel("Export File Type");
72                 c.gridx = 0;
73                 c.gridy = 0;
74                 accessory.add(accessory_label, c);
75
76                 combo_box = new JComboBox(combo_box_items);
77                 combo_box.addActionListener(this);
78                 c.gridx = 0;
79                 c.gridy = 1;
80                 accessory.add(combo_box, c);
81
82                 csv_chooser.setAccessory(accessory);
83                 csv_chooser.setSelectedFile(source_file);
84                 set_default_file();
85                 int ret = csv_chooser.showSaveDialog(frame);
86                 if (ret == JFileChooser.APPROVE_OPTION) {
87                         File file = csv_chooser.getSelectedFile();
88                         String type = (String) combo_box.getSelectedItem();
89                         try {
90                                 if (type.contains("CSV"))
91                                         writer = new AltosCSV(file);
92                                 else
93                                         writer = new AltosKML(file);
94                                 writer.write(states);
95                                 writer.close();
96                         } catch (FileNotFoundException ee) {
97                                 JOptionPane.showMessageDialog(frame,
98                                                               ee.getMessage(),
99                                                               "Cannot open file",
100                                                               JOptionPane.ERROR_MESSAGE);
101                         }
102                 }
103         }
104 }