Add keywords to .desktop files
[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; 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.awt.*;
22 import java.awt.event.*;
23 import javax.swing.*;
24 import javax.swing.filechooser.FileNameExtensionFilter;
25 import java.io.*;
26 import java.util.concurrent.*;
27 import java.util.*;
28 import org.altusmetrum.altoslib_13.*;
29 import org.altusmetrum.altosuilib_13.*;
30
31 public class MicroSave extends JFileChooser {
32
33         JFrame          frame;
34         MicroData       data;
35
36         public static void save(File file, MicroData data) throws FileNotFoundException, IOException {
37                 FileOutputStream fos = new FileOutputStream(file);
38                 data.save(fos);
39                 fos.close();
40         }
41
42         public boolean runDialog() {
43                 int     ret;
44
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(".mpd"));
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                                 save(file, data);
87                                 AltosUIPreferences.set_last_logdir(file.getParentFile());
88                                 data.set_name(filename);
89                                 return true;
90                         } catch (FileNotFoundException fe) {
91                                 JOptionPane.showMessageDialog(frame,
92                                                               fe.getMessage(),
93                                                               "Cannot create file",
94                                                               JOptionPane.ERROR_MESSAGE);
95                         } catch (IOException ioe) {
96                         }
97                 }
98         }
99
100         public MicroSave(JFrame frame, MicroData data) {
101                 this.frame = frame;
102                 this.data = data;
103                 setDialogTitle("Save MicroPeak Data File");
104                 setFileFilter(new FileNameExtensionFilter("MicroPeak data file",
105                                                           "mpd"));
106                 setCurrentDirectory(AltosUIPreferences.last_logdir());
107                 setSelectedFile(MicroFile.make());
108         }
109 }