altosui: Make flight log downloading handle 'Connecting...' dialog
[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 javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import java.io.*;
26 import java.util.*;
27 import java.text.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.LinkedBlockingQueue;
30
31 public class AltosCSVUI
32         extends JDialog
33         implements ActionListener
34 {
35         JFileChooser            csv_chooser;
36         JPanel                  accessory;
37         JComboBox               combo_box;
38         AltosRecordIterable     iterable;
39         AltosWriter             writer;
40
41         static String[]         combo_box_items = { "Comma Separated Values (.CSV)", "Googleearth Data (.KML)" };
42
43         void set_default_file() {
44                 File    current = csv_chooser.getSelectedFile();
45                 String  current_name = current.getName();
46                 String  new_name = null;
47                 String  selected = (String) combo_box.getSelectedItem();
48
49                 if (selected.contains("CSV"))
50                         new_name = Altos.replace_extension(current_name, ".csv");
51                 else if (selected.contains("KML"))
52                         new_name = Altos.replace_extension(current_name, ".kml");
53                 if (new_name != null)
54                         csv_chooser.setSelectedFile(new File(new_name));
55         }
56
57         public void actionPerformed(ActionEvent e) {
58                 if (e.getActionCommand().equals("comboBoxChanged"))
59                         set_default_file();
60         }
61
62         public AltosCSVUI(JFrame frame, AltosRecordIterable in_iterable, File source_file) {
63                 iterable = in_iterable;
64                 csv_chooser = new JFileChooser(source_file);
65
66                 accessory = new JPanel();
67                 accessory.setLayout(new GridBagLayout());
68
69                 GridBagConstraints      c = new GridBagConstraints();
70                 c.fill = GridBagConstraints.NONE;
71                 c.weightx = 1;
72                 c.weighty = 0;
73                 c.insets = new Insets (4, 4, 4, 4);
74
75                 JLabel accessory_label = new JLabel("Export File Type");
76                 c.gridx = 0;
77                 c.gridy = 0;
78                 accessory.add(accessory_label, c);
79
80                 combo_box = new JComboBox(combo_box_items);
81                 combo_box.addActionListener(this);
82                 c.gridx = 0;
83                 c.gridy = 1;
84                 accessory.add(combo_box, c);
85
86                 csv_chooser.setAccessory(accessory);
87                 csv_chooser.setSelectedFile(source_file);
88                 set_default_file();
89                 int ret = csv_chooser.showSaveDialog(frame);
90                 if (ret == JFileChooser.APPROVE_OPTION) {
91                         File file = csv_chooser.getSelectedFile();
92                         String type = (String) combo_box.getSelectedItem();
93                         try {
94                                 if (type.contains("CSV"))
95                                         writer = new AltosCSV(file);
96                                 else
97                                         writer = new AltosKML(file);
98                                 writer.write(iterable);
99                                 writer.close();
100                         } catch (FileNotFoundException ee) {
101                                 JOptionPane.showMessageDialog(frame,
102                                                               file.getName(),
103                                                               "Cannot open file",
104                                                               JOptionPane.ERROR_MESSAGE);
105                         }
106                 }
107         }
108 }