2 * Copyright © 2012 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.micropeak;
21 import java.util.ArrayList;
25 import javax.swing.filechooser.FileNameExtensionFilter;
26 import org.altusmetrum.AltosLib.*;
27 import org.altusmetrum.altosuilib.*;
29 public class MicroExport extends JFileChooser {
34 public boolean runDialog() {
37 setSelectedFile(new File(AltosLib.replace_extension(data.name, ".csv")));
39 ret = showSaveDialog(frame);
40 if (ret != APPROVE_OPTION)
44 file = getSelectedFile();
47 if (!file.getName().contains(".")) {
48 String fullname = file.getPath();
49 file = new File(fullname.concat(".csv"));
51 filename = file.getName();
53 if (file.isDirectory()) {
54 JOptionPane.showMessageDialog(frame,
55 String.format("\"%s\" is a directory",
58 JOptionPane.ERROR_MESSAGE);
61 int r = JOptionPane.showConfirmDialog(frame,
62 String.format("\"%s\" already exists. Overwrite?",
65 JOptionPane.YES_NO_OPTION);
66 if (r != JOptionPane.YES_OPTION)
69 if (!file.canWrite()) {
70 JOptionPane.showMessageDialog(frame,
71 String.format("\"%s\" is not writable",
74 JOptionPane.ERROR_MESSAGE);
79 FileWriter fw = new FileWriter(file);
80 PrintWriter pw = new PrintWriter(fw);
81 pw.printf(" Time, Press, Height, Speed, Accel\n");
82 for (MicroDataPoint point : data.points()) {
83 pw.printf("%6.3f,%6.0f,%7.1f,%7.2f,%7.2f\n",
84 point.time, point.pressure, point.height, point.speed, point.accel);
88 } catch (FileNotFoundException fe) {
89 JOptionPane.showMessageDialog(frame,
92 JOptionPane.ERROR_MESSAGE);
93 } catch (IOException ioe) {
98 public MicroExport(JFrame frame, MicroData data) {
101 setDialogTitle("Export MicroPeak Data File");
102 setFileFilter(new FileNameExtensionFilter("MicroPeak CSV file",
104 setCurrentDirectory(AltosUIPreferences.logdir());