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