1 package net.jankenpoi.sudokuki.ui.swing;
3 import static net.jankenpoi.i18n.I18n._;
5 import java.awt.event.ActionEvent;
7 import java.io.FileNotFoundException;
8 import java.io.FileOutputStream;
9 import java.io.IOException;
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;
17 import net.jankenpoi.i18n.I18n;
18 import net.jankenpoi.sudokuki.view.GridView;
20 @SuppressWarnings("serial")
21 public class SaveAsAction extends AbstractAction {
22 private final GridView view;
23 private final JFrame frame;
25 SaveAsAction(JFrame frame, GridView view, ActionsRepository actions) {
30 public void actionPerformed(ActionEvent e) {
31 JFileChooser fc = new JFileChooser() {
34 public File getSelectedFile() {
35 File file = super.getSelectedFile();
37 return new File("myGrid.skg");
39 if (!"skg".equals(getExtension(file))) {
40 file = new File(String.valueOf(file.getAbsolutePath())
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);
57 File fileToSave = fc.getSelectedFile();
58 if (fileToSave == null) {
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);
70 FileOutputStream fos = null;
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);
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);
90 } catch (IOException ioe) {
91 ioe.printStackTrace();
96 } catch (IOException e1) {
101 private static String getExtension(File file) {
103 String s = file.getName();
104 int i = s.lastIndexOf('.');
106 if ((i > 0) && (i < s.length() - 1)) {
107 ext = s.substring(i + 1).toLowerCase();