a98140ba5970ed1dd86a20d2aa087564cd501d81
[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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.micropeak;
20
21 import java.io.*;
22 import java.util.ArrayList;
23
24 import java.awt.*;
25 import javax.swing.*;
26 import javax.swing.filechooser.FileNameExtensionFilter;
27 import org.altusmetrum.altoslib_13.*;
28 import org.altusmetrum.altosuilib_13.*;
29
30 public class MicroExport extends JFileChooser {
31
32         JFrame          frame;
33         MicroData       data;
34
35         public static void export(File file, MicroData data) throws FileNotFoundException, IOException {
36                 FileWriter fw = new FileWriter(file);
37                 data.export(fw);
38                 fw.close();
39         }
40
41         public boolean runDialog() {
42                 int     ret;
43
44                 setSelectedFile(new File(AltosLib.replace_extension(data.name, ".csv")));
45                 for (;;) {
46                         ret = showSaveDialog(frame);
47                         if (ret != APPROVE_OPTION)
48                                 return false;
49                         File    file;
50                         String  filename;
51                         file = getSelectedFile();
52                         if (file == null)
53                                 continue;
54                         if (!file.getName().contains(".")) {
55                                 String fullname = file.getPath();
56                                 file = new File(fullname.concat(".csv"));
57                         }
58                         filename = file.getName();
59                         if (file.exists()) {
60                                 if (file.isDirectory()) {
61                                         JOptionPane.showMessageDialog(frame,
62                                                                       String.format("\"%s\" is a directory",
63                                                                                     filename),
64                                                                       "Directory",
65                                                                       JOptionPane.ERROR_MESSAGE);
66                                         continue;
67                                 }
68                                 int r = JOptionPane.showConfirmDialog(frame,
69                                                                       String.format("\"%s\" already exists. Overwrite?",
70                                                                                     filename),
71                                                                       "Overwrite file?",
72                                                                       JOptionPane.YES_NO_OPTION);
73                                 if (r != JOptionPane.YES_OPTION)
74                                         continue;
75                                                               
76                                 if (!file.canWrite()) {
77                                         JOptionPane.showMessageDialog(frame,
78                                                                       String.format("\"%s\" is not writable",
79                                                                                     filename),
80                                                                       "File not writable",
81                                                                       JOptionPane.ERROR_MESSAGE);
82                                         continue;
83                                 }
84                         }
85                         try {
86                                 export(file, data);
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 }