OSDN Git Service

- Fixed item #3520726 "Cannot enter filename when saving a grid on MacOS X"
[sudokuki/sudokuki.git] / src / classes / net / jankenpoi / sudokuki / ui / swing / SaveAsAction.java
1 package net.jankenpoi.sudokuki.ui.swing;
2
3 import static net.jankenpoi.i18n.I18n._;
4
5 import java.awt.event.ActionEvent;
6 import java.io.File;
7 import java.io.FileNotFoundException;
8 import java.io.FileOutputStream;
9 import java.io.IOException;
10
11 import javax.swing.AbstractAction;
12 import javax.swing.JFileChooser;
13 import javax.swing.JFrame;
14 import javax.swing.JOptionPane;
15 import javax.swing.filechooser.FileNameExtensionFilter;
16
17 import net.jankenpoi.i18n.I18n;
18 import net.jankenpoi.sudokuki.view.GridView;
19
20 @SuppressWarnings("serial")
21 public class SaveAsAction extends AbstractAction {
22         private final GridView view;
23         private final JFrame frame;
24
25         SaveAsAction(JFrame frame, GridView view, ActionsRepository actions) {
26                 this.frame = frame;
27                 this.view = view;
28         }
29
30         public void actionPerformed(ActionEvent e) {
31                 JFileChooser fc = new JFileChooser() {
32
33                         @Override
34                         public File getSelectedFile() {
35                                 File file = super.getSelectedFile();
36                                 if (file == null) {
37                                         return new File("myGrid.skg");
38                                 }
39                                 if (!"skg".equals(getExtension(file))) {
40                                         file = new File(String.valueOf(file.getAbsolutePath())
41                                                         + ".skg");
42                                 }
43                                 return file;
44                         }
45                 };
46
47                 fc.setDialogTitle(I18n._("Save as..."));
48                 fc.setAcceptAllFileFilterUsed(false);
49                 FileNameExtensionFilter filter = new FileNameExtensionFilter(
50                                 I18n._("Sudokuki grid files"), new String[] { "skg" });
51                 fc.setFileFilter(filter);
52                 int returnVal = fc.showSaveDialog(this.frame);
53
54                 if (returnVal != 0) {
55                         return;
56                 }
57                 File fileToSave = fc.getSelectedFile();
58                 if (fileToSave == null) {
59                         return;
60                 }
61                 fileToSave.delete();
62                 try {
63                         fileToSave.createNewFile();
64                 } catch (IOException e1) {
65             JOptionPane.showMessageDialog(frame, "<html>"
66                     + "<table border=\"0\">" + "<tr>"
67                     + _("Failed to save the grid<br/>at the selected location.") + "</tr>"
68                     + "</html>", "Sudokuki", JOptionPane.ERROR_MESSAGE);
69                 }
70                 FileOutputStream fos = null;
71                 try {
72                         fos = new FileOutputStream(fileToSave);
73                 } catch (FileNotFoundException e1) {
74             JOptionPane.showMessageDialog(frame, "<html>"
75                     + "<table border=\"0\">" + "<tr>"
76                     + _("Failed to save the grid<br/>at the selected location.") + "</tr>"
77                     + "</html>", "Sudokuki", JOptionPane.ERROR_MESSAGE);
78                 }
79                 if (fos == null) {
80                         return;
81                 }
82
83                 int[] cellInfos = this.view.getController().getCellInfosFromModel();
84                 for (int i = 0; i < cellInfos.length; i++) {
85                         byte lo = (byte) (cellInfos[i] & 0xFF);
86                         byte hi = (byte) ((cellInfos[i] & 0xFF00) >> 8);
87                         try {
88                                 fos.write(lo);
89                                 fos.write(hi);
90                         } catch (IOException ioe) {
91                                 ioe.printStackTrace();
92                         }
93                 }
94                 try {
95                         fos.close();
96                 } catch (IOException e1) {
97                         e1.printStackTrace();
98                 }
99         }
100
101         private static String getExtension(File file) {
102                 String ext = null;
103                 String s = file.getName();
104                 int i = s.lastIndexOf('.');
105
106                 if ((i > 0) && (i < s.length() - 1)) {
107                         ext = s.substring(i + 1).toLowerCase();
108                 }
109                 return ext;
110         }
111 }