altosdroid: Add map types and map preloading UIs
[fw/altos] / altosuilib / 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 org.altusmetrum.altosuilib_7;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import java.io.*;
24 import org.altusmetrum.altoslib_7.*;
25
26 public class AltosCSVUI
27         extends AltosUIDialog
28         implements ActionListener
29 {
30         JFileChooser            csv_chooser;
31         JPanel                  accessory;
32         JComboBox<String>       combo_box;
33         Iterable<AltosState>    states;
34         AltosWriter             writer;
35
36         static String[]         combo_box_items = { "Comma Separated Values (.CSV)", "Googleearth Data (.KML)" };
37
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();
43
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");
48                 if (new_name != null)
49                         csv_chooser.setSelectedFile(new File(new_name));
50         }
51
52         public void actionPerformed(ActionEvent e) {
53                 if (e.getActionCommand().equals("comboBoxChanged"))
54                         set_default_file();
55         }
56
57         public AltosCSVUI(JFrame frame, AltosStateIterable states, File source_file) {
58                 this.states = states;
59                 csv_chooser = new JFileChooser(source_file);
60
61                 accessory = new JPanel();
62                 accessory.setLayout(new GridBagLayout());
63
64                 GridBagConstraints      c = new GridBagConstraints();
65                 c.fill = GridBagConstraints.NONE;
66                 c.weightx = 1;
67                 c.weighty = 0;
68                 c.insets = new Insets (4, 4, 4, 4);
69
70                 JLabel accessory_label = new JLabel("Export File Type");
71                 c.gridx = 0;
72                 c.gridy = 0;
73                 accessory.add(accessory_label, c);
74
75                 combo_box = new JComboBox<String>(combo_box_items);
76                 combo_box.addActionListener(this);
77                 c.gridx = 0;
78                 c.gridy = 1;
79                 accessory.add(combo_box, c);
80
81                 csv_chooser.setAccessory(accessory);
82                 csv_chooser.setSelectedFile(source_file);
83                 set_default_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();
88                         try {
89                                 if (type.contains("CSV"))
90                                         writer = new AltosCSV(file);
91                                 else
92                                         writer = new AltosKML(file);
93                                 writer.write(states);
94                                 writer.close();
95                         } catch (FileNotFoundException ee) {
96                                 JOptionPane.showMessageDialog(frame,
97                                                               ee.getMessage(),
98                                                               "Cannot open file",
99                                                               JOptionPane.ERROR_MESSAGE);
100                         }
101                 }
102         }
103 }