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