2 * Sudokuki - essential sudoku game
3 * Copyright (C) 2007-2016 Sylvain Vedrenne
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.jankenpoi.sudokuki.ui.swing;
20 import static net.jankenpoi.i18n.I18n.gtxt;
22 import java.awt.event.ActionEvent;
24 import java.io.FileInputStream;
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
28 import javax.swing.AbstractAction;
29 import javax.swing.JFileChooser;
30 import javax.swing.JFrame;
31 import javax.swing.JOptionPane;
32 import javax.swing.filechooser.FileFilter;
34 import net.jankenpoi.sudokuki.model.GridModel;
35 import net.jankenpoi.sudokuki.view.GridView;
37 @SuppressWarnings("serial")
38 public class OpenGridAction extends AbstractAction {
40 private final GridView view;
42 private final JFrame frame;
44 OpenGridAction(JFrame frame, GridView view) {
50 public void actionPerformed(ActionEvent e) {
52 final JFileChooser fc = new JFileChooser();
54 fc.setDialogTitle(gtxt("Open grid..."));
55 fc.setAcceptAllFileFilterUsed(false);
56 fc.setFileFilter(new FileFilter() {
58 public String getExtension(File f) {
60 String s = f.getName();
61 int i = s.lastIndexOf('.');
63 if (i > 0 && i < s.length() - 1) {
64 ext = s.substring(i+1).toLowerCase();
70 public String getDescription() {
71 return gtxt("Sudokuki grid files");
75 public boolean accept(File f) {
76 String extension = getExtension(f);
77 if (f.isDirectory() || "skg".equals(extension)) {
83 int returnVal = fc.showOpenDialog(frame);
85 if (returnVal != JFileChooser.APPROVE_OPTION) {
88 File fileToOpen = fc.getSelectedFile();
89 if (fileToOpen == null) {
92 FileInputStream fis = null;
94 fis = new FileInputStream(fileToOpen);
95 } catch (FileNotFoundException e1) {
98 JOptionPane.showMessageDialog(frame, "<html>"
99 + "<table border=\"0\">" + "<tr>"
100 + "File not found:" + "</tr>"
101 + "<tr>" + fileToOpen + "</tr>"
102 + "</html>", "Sudokuki", JOptionPane.PLAIN_MESSAGE);
106 short[] externalCellInfos = new short[81];
108 for (int i = 0; i < 81; i++) {
111 short together = (short) (hi << 8 | lo);
113 if ((together & ~(GridModel.FLAG_CELL_READ_ONLY
114 | GridModel.FLAG_GRID_COMPLETE
115 | GridModel.MASK_CELL_MEMOS | GridModel.MASK_CELL_VALUES)) != 0) {
116 throw new IOException("Invalid cell info");
118 if (9 < (together & GridModel.MASK_CELL_VALUES)) {
119 throw new IOException("Cell value out of range");
121 externalCellInfos[i] = together;
123 view.getController().notifyResetGridFromShorts(externalCellInfos);
124 } catch (IOException e1) {
125 JOptionPane.showMessageDialog(frame, "<html>"
126 + "<table border=\"0\">" + "<tr>"
127 + gtxt("This file is not a Sudokuki grid.") + "</tr>"
128 + "</html>", "Sudokuki", JOptionPane.ERROR_MESSAGE);
132 } catch (IOException e1) {
133 System.err.println("An error occured upon FileInputStream close()");