OSDN Git Service

Removed some unused comments and some System.out.println calls.
[sudokuki/sudokuki.git] / src / classes / net / jankenpoi / sudokuki / ui / swing / OpenGridAction.java
1 /*
2  * Sudokuki - essential sudoku game
3  * Copyright (C) 2007-2012 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._;
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.view.GridView;
35
36 @SuppressWarnings("serial")
37 public class OpenGridAction extends AbstractAction {
38
39         private final GridView view;
40
41         private final JFrame frame;
42         
43         OpenGridAction(JFrame frame, GridView view, ActionsRepository actions) {
44                 this.frame = frame;
45                 this.view = view;
46         }
47         
48         @Override
49         public void actionPerformed(ActionEvent e) {
50                 
51                 final JFileChooser fc = new JFileChooser();
52                 
53                 fc.setDialogTitle(_("Open grid..."));
54                 fc.setAcceptAllFileFilterUsed(false);
55                 fc.setFileFilter(new FileFilter() {
56                         
57                         public String getExtension(File f) {
58                         String ext = null;
59                         String s = f.getName();
60                         int i = s.lastIndexOf('.');
61
62                         if (i > 0 &&  i < s.length() - 1) {
63                             ext = s.substring(i+1).toLowerCase();
64                         }
65                         return ext;
66                     }
67                         
68                         @Override
69                         public String getDescription() {
70                                 // TODO Auto-generated method stub
71                                 return _("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                         // TODO Auto-generated catch block
97                         e1.printStackTrace();
98                 }
99                 if (fis == null) {
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);
105             return;
106                 }
107                 
108                 short[] externalCellInfos = new short[81];
109                 for (int i=0; i<81; i++) {
110                         try {
111                                 int lo = fis.read();
112                                 int hi = fis.read();
113                                 short together = (short) (hi << 8 | lo);
114
115                                 externalCellInfos[i] = (short) together;
116                         } catch (IOException e1) {
117                                 // TODO Auto-generated catch block
118                                 e1.printStackTrace();
119                         }
120                 }
121                 view.getController().notifyResetGridFromShorts(externalCellInfos);
122                 
123                 try {
124                         fis.close();
125                 } catch (IOException e1) {
126                         // TODO Auto-generated catch block
127                         e1.printStackTrace();
128                 }
129         }
130                 
131 }