Build installable versions of MicroPeak GUI
[fw/altos] / micropeak / MicroSave.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.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import java.io.*;
25 import java.util.concurrent.*;
26 import java.util.*;
27 import org.altusmetrum.AltosLib.*;
28 import org.altusmetrum.altosuilib.*;
29
30 public class MicroSave extends JFileChooser {
31
32         JFrame          frame;
33         MicroData       data;
34
35         public boolean runDialog() {
36                 int     ret;
37
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(".mpd"));
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                                 FileOutputStream fos = new FileOutputStream(file);
80                                 data.save(fos);
81                                 fos.close();
82                                 data.set_name(filename);
83                                 return true;
84                         } catch (FileNotFoundException fe) {
85                                 JOptionPane.showMessageDialog(frame,
86                                                               fe.getMessage(),
87                                                               "Cannot create file",
88                                                               JOptionPane.ERROR_MESSAGE);
89                         } catch (IOException ioe) {
90                         }
91                 }
92         }
93
94         public MicroSave(JFrame frame, MicroData data) {
95                 this.frame = frame;
96                 this.data = data;
97                 setDialogTitle("Save MicroPeak Data File");
98                 setFileFilter(new FileNameExtensionFilter("MicroPeak data file",
99                                                           "mpd"));
100                 setCurrentDirectory(AltosUIPreferences.logdir());
101         }
102 }