Reverted package name to 'altosui' from 'AltosUI'
[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 import org.altusmetrum.AltosLib.*;
31
32 public class AltosCSVUI
33         extends AltosDialog
34         implements ActionListener
35 {
36         JFileChooser            csv_chooser;
37         JPanel                  accessory;
38         JComboBox               combo_box;
39         AltosRecordIterable     iterable;
40         AltosWriter             writer;
41
42         static String[]         combo_box_items = { "Comma Separated Values (.CSV)", "Googleearth Data (.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.contains("CSV"))
51                         new_name = Altos.replace_extension(current_name, ".csv");
52                 else if (selected.contains("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 actionPerformed(ActionEvent e) {
59                 if (e.getActionCommand().equals("comboBoxChanged"))
60                         set_default_file();
61         }
62
63         public AltosCSVUI(JFrame frame, AltosRecordIterable in_iterable, File source_file) {
64                 iterable = in_iterable;
65                 csv_chooser = new JFileChooser(source_file);
66
67                 accessory = new JPanel();
68                 accessory.setLayout(new GridBagLayout());
69
70                 GridBagConstraints      c = new GridBagConstraints();
71                 c.fill = GridBagConstraints.NONE;
72                 c.weightx = 1;
73                 c.weighty = 0;
74                 c.insets = new Insets (4, 4, 4, 4);
75
76                 JLabel accessory_label = new JLabel("Export File Type");
77                 c.gridx = 0;
78                 c.gridy = 0;
79                 accessory.add(accessory_label, c);
80
81                 combo_box = new JComboBox(combo_box_items);
82                 combo_box.addActionListener(this);
83                 c.gridx = 0;
84                 c.gridy = 1;
85                 accessory.add(combo_box, c);
86
87                 csv_chooser.setAccessory(accessory);
88                 csv_chooser.setSelectedFile(source_file);
89                 set_default_file();
90                 int ret = csv_chooser.showSaveDialog(frame);
91                 if (ret == JFileChooser.APPROVE_OPTION) {
92                         File file = csv_chooser.getSelectedFile();
93                         String type = (String) combo_box.getSelectedItem();
94                         try {
95                                 if (type.contains("CSV"))
96                                         writer = new AltosCSV(file);
97                                 else
98                                         writer = new AltosKML(file);
99                                 writer.write(iterable);
100                                 writer.close();
101                         } catch (FileNotFoundException ee) {
102                                 JOptionPane.showMessageDialog(frame,
103                                                               ee.getMessage(),
104                                                               "Cannot open file",
105                                                               JOptionPane.ERROR_MESSAGE);
106                         }
107                 }
108         }
109 }