altosui: Add KML file export.
[fw/altos] / ao-tools / 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 import altosui.AltosLogfileChooser;
32 import altosui.AltosCSV;
33
34 public class AltosCSVUI
35         extends JDialog
36         implements Runnable, ActionListener
37 {
38         JFrame                  frame;
39         Thread                  thread;
40         AltosRecordIterable     iterable;
41         AltosWriter             writer;
42         JFileChooser            csv_chooser;
43         JComboBox               combo_box;
44
45         static String[]         combo_box_items = { "CSV", "KML" };
46
47         void set_default_file() {
48                 File    current = csv_chooser.getSelectedFile();
49                 String  current_name = current.getName();
50                 String  new_name = null;
51                 String  selected = (String) combo_box.getSelectedItem();
52
53                 if (selected.equals("CSV"))
54                         new_name = Altos.replace_extension(current_name, ".csv");
55                 else if (selected.equals("KML"))
56                         new_name = Altos.replace_extension(current_name, ".kml");
57                 if (new_name != null)
58                         csv_chooser.setSelectedFile(new File(new_name));
59         }
60
61         public void run() {
62                 AltosLogfileChooser     chooser;
63
64                 chooser = new AltosLogfileChooser(frame);
65                 iterable = chooser.runDialog();
66                 if (iterable == null)
67                         return;
68
69                 csv_chooser = new JFileChooser(chooser.file());
70                 combo_box = new JComboBox(combo_box_items);
71                 combo_box.addActionListener(this);
72                 csv_chooser.setAccessory(combo_box);
73                 csv_chooser.setSelectedFile(chooser.file());
74                 set_default_file();
75                 int ret = csv_chooser.showSaveDialog(frame);
76                 if (ret == JFileChooser.APPROVE_OPTION) {
77                         File    file = csv_chooser.getSelectedFile();
78                         String  type = (String) combo_box.getSelectedItem();
79                         try {
80                                 if (type.equals("CSV"))
81                                         writer = new AltosCSV(file);
82                                 else
83                                         writer = new AltosKML(file);
84                         } catch (FileNotFoundException ee) {
85                                 JOptionPane.showMessageDialog(frame,
86                                                               file.getName(),
87                                                               "Cannot open file",
88                                                               JOptionPane.ERROR_MESSAGE);
89                         }
90                         writer.write(iterable);
91                         writer.close();
92                 }
93         }
94
95         public void actionPerformed(ActionEvent e) {
96                 System.out.printf("command %s param %s\n", e.getActionCommand(), e.paramString());
97                 if (e.getActionCommand().equals("comboBoxChanged"))
98                         set_default_file();
99         }
100
101         public AltosCSVUI(JFrame in_frame) {
102                 frame = in_frame;
103                 thread = new Thread(this);
104                 thread.start();
105         }
106 }