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.
18 package org.altusmetrum.altosuilib_2;
21 import java.awt.event.*;
24 import org.altusmetrum.altoslib_4.*;
26 public class AltosCSVUI
28 implements ActionListener
30 JFileChooser csv_chooser;
32 JComboBox<String> combo_box;
33 Iterable<AltosState> states;
36 static String[] combo_box_items = { "Comma Separated Values (.CSV)", "Googleearth Data (.KML)" };
38 void set_default_file() {
39 File current = csv_chooser.getSelectedFile();
40 String current_name = current.getName();
41 String new_name = null;
42 String selected = (String) combo_box.getSelectedItem();
44 if (selected.contains("CSV"))
45 new_name = AltosLib.replace_extension(current_name, ".csv");
46 else if (selected.contains("KML"))
47 new_name = AltosLib.replace_extension(current_name, ".kml");
49 csv_chooser.setSelectedFile(new File(new_name));
52 public void actionPerformed(ActionEvent e) {
53 if (e.getActionCommand().equals("comboBoxChanged"))
57 public AltosCSVUI(JFrame frame, AltosStateIterable states, File source_file) {
59 csv_chooser = new JFileChooser(source_file);
61 accessory = new JPanel();
62 accessory.setLayout(new GridBagLayout());
64 GridBagConstraints c = new GridBagConstraints();
65 c.fill = GridBagConstraints.NONE;
68 c.insets = new Insets (4, 4, 4, 4);
70 JLabel accessory_label = new JLabel("Export File Type");
73 accessory.add(accessory_label, c);
75 combo_box = new JComboBox<String>(combo_box_items);
76 combo_box.addActionListener(this);
79 accessory.add(combo_box, c);
81 csv_chooser.setAccessory(accessory);
82 csv_chooser.setSelectedFile(source_file);
84 int ret = csv_chooser.showSaveDialog(frame);
85 if (ret == JFileChooser.APPROVE_OPTION) {
86 File file = csv_chooser.getSelectedFile();
87 String type = (String) combo_box.getSelectedItem();
89 if (type.contains("CSV"))
90 writer = new AltosCSV(file);
92 writer = new AltosKML(file);
95 } catch (FileNotFoundException ee) {
96 JOptionPane.showMessageDialog(frame,
99 JOptionPane.ERROR_MESSAGE);