OSDN Git Service

Support Polish language thanks to Monika Viste
[sudokuki/sudokuki.git] / src / classes / net / jankenpoi / sudokuki / ui / swing / OpenGridAction.java
1 /*
2  * Sudokuki - essential sudoku game
3  * Copyright (C) 2007-2016 Sylvain Vedrenne
4  *
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.
9  * 
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.
14  * 
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/>.
17  */
18 package net.jankenpoi.sudokuki.ui.swing;
19
20 import static net.jankenpoi.i18n.I18n.gtxt;
21
22 import java.awt.event.ActionEvent;
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
27
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;
33
34 import net.jankenpoi.sudokuki.model.GridModel;
35 import net.jankenpoi.sudokuki.view.GridView;
36
37 @SuppressWarnings("serial")
38 public class OpenGridAction extends AbstractAction {
39
40         private final GridView view;
41
42         private final JFrame frame;
43         
44         OpenGridAction(JFrame frame, GridView view) {
45                 this.frame = frame;
46                 this.view = view;
47         }
48         
49         @Override
50         public void actionPerformed(ActionEvent e) {
51                 
52                 final JFileChooser fc = new JFileChooser();
53                 
54                 fc.setDialogTitle(gtxt("Open grid..."));
55                 fc.setAcceptAllFileFilterUsed(false);
56                 fc.setFileFilter(new FileFilter() {
57                         
58                         public String getExtension(File f) {
59                         String ext = null;
60                         String s = f.getName();
61                         int i = s.lastIndexOf('.');
62
63                         if (i > 0 &&  i < s.length() - 1) {
64                             ext = s.substring(i+1).toLowerCase();
65                         }
66                         return ext;
67                     }
68                         
69                         @Override
70                         public String getDescription() {
71                                 return gtxt("Sudokuki grid files");
72                         }
73                         
74                         @Override
75                         public boolean accept(File f) {
76                                 String extension = getExtension(f);
77                                 if (f.isDirectory() || "skg".equals(extension)) {
78                                         return true;
79                                 }
80                                 return false;
81                         }
82                 });
83                 int returnVal = fc.showOpenDialog(frame);
84                 
85                 if (returnVal != JFileChooser.APPROVE_OPTION) {
86                         return;
87                 }
88                 File fileToOpen = fc.getSelectedFile();
89                 if (fileToOpen == null) {
90                         return;
91                 }
92                 FileInputStream fis = null;
93                 try {
94                         fis = new FileInputStream(fileToOpen);
95                 } catch (FileNotFoundException e1) {
96                 }
97                 if (fis == null) {
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);
103             return;
104                 }
105                 
106                 short[] externalCellInfos = new short[81];
107                 try {
108                         for (int i = 0; i < 81; i++) {
109                                 int lo = fis.read();
110                                 int hi = fis.read();
111                                 short together = (short) (hi << 8 | lo);
112
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");
117                                 }
118                                 if (9 < (together & GridModel.MASK_CELL_VALUES)) {
119                                         throw new IOException("Cell value out of range");
120                                 }
121                                 externalCellInfos[i] = together;
122                         }
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);
129                 } finally {
130                         try {
131                                 fis.close();
132                         } catch (IOException e1) {
133                                 System.err.println("An error occured upon FileInputStream close()");
134                         }
135                 }
136         }
137                 
138 }