]> git.gag.com Git - fw/altos/blob - micropeak/MicroSave.java
altosdroid: Can only use message once
[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_10.*;
28 import org.altusmetrum.altosuilib_10.*;
29
30 public class MicroSave extends JFileChooser {
31
32         JFrame          frame;
33         MicroData       data;
34
35         public static void save(File file, MicroData data) throws FileNotFoundException, IOException {
36                 FileOutputStream fos = new FileOutputStream(file);
37                 data.save(fos);
38                 fos.close();
39         }
40
41         public boolean runDialog() {
42                 int     ret;
43
44                 for (;;) {
45                         ret = showSaveDialog(frame);
46                         if (ret != APPROVE_OPTION)
47                                 return false;
48                         File    file;
49                         String  filename;
50                         file = getSelectedFile();
51                         if (file == null)
52                                 continue;
53                         if (!file.getName().contains(".")) {
54                                 String fullname = file.getPath();
55                                 file = new File(fullname.concat(".mpd"));
56                         }
57                         filename = file.getName();
58                         if (file.exists()) {
59                                 if (file.isDirectory()) {
60                                         JOptionPane.showMessageDialog(frame,
61                                                                       String.format("\"%s\" is a directory",
62                                                                                     filename),
63                                                                       "Directory",
64                                                                       JOptionPane.ERROR_MESSAGE);
65                                         continue;
66                                 }
67                                 int r = JOptionPane.showConfirmDialog(frame,
68                                                                       String.format("\"%s\" already exists. Overwrite?",
69                                                                                     filename),
70                                                                       "Overwrite file?",
71                                                                       JOptionPane.YES_NO_OPTION);
72                                 if (r != JOptionPane.YES_OPTION)
73                                         continue;
74                                                               
75                                 if (!file.canWrite()) {
76                                         JOptionPane.showMessageDialog(frame,
77                                                                       String.format("\"%s\" is not writable",
78                                                                                     filename),
79                                                                       "File not writable",
80                                                                       JOptionPane.ERROR_MESSAGE);
81                                         continue;
82                                 }
83                         }
84                         try {
85                                 save(file, data);
86                                 AltosUIPreferences.set_last_logdir(file.getParentFile());
87                                 data.set_name(filename);
88                                 return true;
89                         } catch (FileNotFoundException fe) {
90                                 JOptionPane.showMessageDialog(frame,
91                                                               fe.getMessage(),
92                                                               "Cannot create file",
93                                                               JOptionPane.ERROR_MESSAGE);
94                         } catch (IOException ioe) {
95                         }
96                 }
97         }
98
99         public MicroSave(JFrame frame, MicroData data) {
100                 this.frame = frame;
101                 this.data = data;
102                 setDialogTitle("Save MicroPeak Data File");
103                 setFileFilter(new FileNameExtensionFilter("MicroPeak data file",
104                                                           "mpd"));
105                 setCurrentDirectory(AltosUIPreferences.last_logdir());
106                 setSelectedFile(MicroFile.make());
107         }
108 }