2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
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.
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.
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.
21 import java.awt.event.*;
24 import org.altusmetrum.altoslib_1.*;
25 import org.altusmetrum.altosuilib_1.*;
27 public class AltosCSVUI
29 implements ActionListener
31 JFileChooser csv_chooser;
34 AltosRecordIterable iterable;
37 static String[] combo_box_items = { "Comma Separated Values (.CSV)", "Googleearth Data (.KML)" };
39 void set_default_file() {
40 File current = csv_chooser.getSelectedFile();
41 String current_name = current.getName();
42 String new_name = null;
43 String selected = (String) combo_box.getSelectedItem();
45 if (selected.contains("CSV"))
46 new_name = Altos.replace_extension(current_name, ".csv");
47 else if (selected.contains("KML"))
48 new_name = Altos.replace_extension(current_name, ".kml");
50 csv_chooser.setSelectedFile(new File(new_name));
53 public void actionPerformed(ActionEvent e) {
54 if (e.getActionCommand().equals("comboBoxChanged"))
58 public AltosCSVUI(JFrame frame, AltosRecordIterable in_iterable, File source_file) {
59 iterable = in_iterable;
60 csv_chooser = new JFileChooser(source_file);
62 accessory = new JPanel();
63 accessory.setLayout(new GridBagLayout());
65 GridBagConstraints c = new GridBagConstraints();
66 c.fill = GridBagConstraints.NONE;
69 c.insets = new Insets (4, 4, 4, 4);
71 JLabel accessory_label = new JLabel("Export File Type");
74 accessory.add(accessory_label, c);
76 combo_box = new JComboBox(combo_box_items);
77 combo_box.addActionListener(this);
80 accessory.add(combo_box, c);
82 csv_chooser.setAccessory(accessory);
83 csv_chooser.setSelectedFile(source_file);
85 int ret = csv_chooser.showSaveDialog(frame);
86 if (ret == JFileChooser.APPROVE_OPTION) {
87 File file = csv_chooser.getSelectedFile();
88 String type = (String) combo_box.getSelectedItem();
90 if (type.contains("CSV"))
91 writer = new AltosCSV(file);
93 writer = new AltosKML(file);
94 writer.write(iterable);
96 } catch (FileNotFoundException ee) {
97 JOptionPane.showMessageDialog(frame,
100 JOptionPane.ERROR_MESSAGE);