micropeak: Add view of raw data in GUI
[fw/altos] / micropeak / MicroExport.java
1 /*
2  * Copyright © 2012 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.micropeak;
19
20 import java.io.*;
21 import java.util.ArrayList;
22
23 import java.awt.*;
24 import javax.swing.*;
25 import javax.swing.filechooser.FileNameExtensionFilter;
26 import org.altusmetrum.AltosLib.*;
27 import org.altusmetrum.altosuilib.*;
28
29 public class MicroExport extends JFileChooser {
30
31         JFrame          frame;
32         MicroData       data;
33
34         public boolean runDialog() {
35                 int     ret;
36
37                 setSelectedFile(new File(AltosLib.replace_extension(data.name, ".csv")));
38                 for (;;) {
39                         ret = showSaveDialog(frame);
40                         if (ret != APPROVE_OPTION)
41                                 return false;
42                         File    file;
43                         String  filename;
44                         file = getSelectedFile();
45                         if (file == null)
46                                 continue;
47                         if (!file.getName().contains(".")) {
48                                 String fullname = file.getPath();
49                                 file = new File(fullname.concat(".csv"));
50                         }
51                         filename = file.getName();
52                         if (file.exists()) {
53                                 if (file.isDirectory()) {
54                                         JOptionPane.showMessageDialog(frame,
55                                                                       String.format("\"%s\" is a directory",
56                                                                                     filename),
57                                                                       "Directory",
58                                                                       JOptionPane.ERROR_MESSAGE);
59                                         continue;
60                                 }
61                                 int r = JOptionPane.showConfirmDialog(frame,
62                                                                       String.format("\"%s\" already exists. Overwrite?",
63                                                                                     filename),
64                                                                       "Overwrite file?",
65                                                                       JOptionPane.YES_NO_OPTION);
66                                 if (r != JOptionPane.YES_OPTION)
67                                         continue;
68                                                               
69                                 if (!file.canWrite()) {
70                                         JOptionPane.showMessageDialog(frame,
71                                                                       String.format("\"%s\" is not writable",
72                                                                                     filename),
73                                                                       "File not writable",
74                                                                       JOptionPane.ERROR_MESSAGE);
75                                         continue;
76                                 }
77                         }
78                         try {
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);
85                                 }
86                                 fw.close();
87                                 return true;
88                         } catch (FileNotFoundException fe) {
89                                 JOptionPane.showMessageDialog(frame,
90                                                               fe.getMessage(),
91                                                               "Cannot create file",
92                                                               JOptionPane.ERROR_MESSAGE);
93                         } catch (IOException ioe) {
94                         }
95                 }
96         }
97
98         public MicroExport(JFrame frame, MicroData data) {
99                 this.frame = frame;
100                 this.data = data;
101                 setDialogTitle("Export MicroPeak Data File");
102                 setFileFilter(new FileNameExtensionFilter("MicroPeak CSV file",
103                                                           "csv"));
104                 setCurrentDirectory(AltosUIPreferences.logdir());
105         }
106 }