2 * Sudokuki - essential sudoku game
3 * Copyright (C) 2007-2012 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._;
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.view.GridView;
36 @SuppressWarnings("serial")
37 public class OpenGridAction extends AbstractAction {
39 private final GridView view;
41 private final JFrame frame;
43 OpenGridAction(JFrame frame, GridView view, ActionsRepository actions) {
49 public void actionPerformed(ActionEvent e) {
51 final JFileChooser fc = new JFileChooser();
53 fc.setDialogTitle(_("Open grid..."));
54 fc.setAcceptAllFileFilterUsed(false);
55 fc.setFileFilter(new FileFilter() {
57 public String getExtension(File f) {
59 String s = f.getName();
60 int i = s.lastIndexOf('.');
62 if (i > 0 && i < s.length() - 1) {
63 ext = s.substring(i+1).toLowerCase();
69 public String getDescription() {
70 // TODO Auto-generated method stub
71 return _("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) {
96 // TODO Auto-generated catch block
100 JOptionPane.showMessageDialog(frame, "<html>"
101 + "<table border=\"0\">" + "<tr>"
102 + "File not found:" + "</tr>"
103 + "<tr>" + fileToOpen + "</tr>"
104 + "</html>", "Sudokuki", JOptionPane.PLAIN_MESSAGE);
108 short[] externalCellInfos = new short[81];
109 for (int i=0; i<81; i++) {
113 short together = (short) (hi << 8 | lo);
115 externalCellInfos[i] = (short) together;
116 } catch (IOException e1) {
117 // TODO Auto-generated catch block
118 e1.printStackTrace();
121 view.getController().notifyResetGridFromShorts(externalCellInfos);
125 } catch (IOException e1) {
126 // TODO Auto-generated catch block
127 e1.printStackTrace();