OSDN Git Service

A "Cancel" button was lacking L10n. Fixed.
[sudokuki/sudokuki.git] / src / classes / net / jankenpoi / sudokuki / ui / swing / ResolveGridDialog.java
1 /*\r
2  * Sudokuki - essential sudoku game\r
3  * Copyright (C) 2007-2012 Sylvain Vedrenne\r
4  *\r
5  * This program is free software: you can redistribute it and/or modify\r
6  * it under the terms of the GNU General Public License as published by\r
7  * the Free Software Foundation, either version 3 of the License, or\r
8  * (at your option) any later version.\r
9  * \r
10  * This program is distributed in the hope that it will be useful,\r
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  * GNU General Public License for more details.\r
14  * \r
15  * You should have received a copy of the GNU General Public License\r
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
17  */\r
18 package net.jankenpoi.sudokuki.ui.swing;\r
19 \r
20 import static net.jankenpoi.i18n.I18n._;\r
21 \r
22 import java.awt.Container;\r
23 import java.awt.GridLayout;\r
24 import java.util.concurrent.ExecutionException;\r
25 \r
26 import javax.swing.JButton;\r
27 import javax.swing.JDialog;\r
28 import javax.swing.JFrame;\r
29 import javax.swing.JLabel;\r
30 import javax.swing.SwingWorker;\r
31 \r
32 import net.jankenpoi.sudokuki.model.GridModel;\r
33 import net.jankenpoi.sudokuki.solver.BruteForceGridSolver;\r
34 import net.jankenpoi.sudokuki.solver.GridSolution;\r
35 import net.jankenpoi.sudokuki.view.GridView;\r
36 /**\r
37  * CheckUpdateDialog.java\r
38  * \r
39  * @author svedrenne\r
40  */\r
41 @SuppressWarnings("serial")\r
42 public class ResolveGridDialog extends JDialog {\r
43 \r
44         private JFrame parent;\r
45 \r
46         private int status = -1;\r
47 \r
48         private final GridView view;\r
49 \r
50         private final SwingWorker<Integer, Void> worker;\r
51 \r
52         private final BruteForceGridSolver bruteSolver;\r
53         private final GridModel gridToSolve;\r
54 \r
55         public ResolveGridDialog(JFrame parent, final GridView view,\r
56                         final ResolveAction resolveAction) {\r
57 \r
58                 super(parent, true);\r
59                 this.parent = parent;\r
60                 this.view = view;\r
61                 setResizable(false);\r
62 \r
63                 short[] flagsTable = new short[81];\r
64                 for (int li = 0; li < 9; li++) {\r
65                         for (int co = 0; co < 9; co++) {\r
66                                 if (view.isCellReadOnly(li, co)) {\r
67                                         flagsTable[9 * li + co] = view.getValueAt(li, co);\r
68                                 }\r
69                         }\r
70                 }\r
71                 gridToSolve = new GridModel(flagsTable, 0);\r
72                 bruteSolver = new BruteForceGridSolver(gridToSolve);\r
73 \r
74                 worker = new SwingWorker<Integer, Void>() {\r
75 \r
76                         @Override\r
77                         /* Executed in the SwingWorker thread */\r
78                         protected Integer doInBackground() {\r
79                                 return resolveGrid();\r
80                         }\r
81 \r
82                         @Override\r
83                         /* Executed in the EDT, triggered when the SwingWorker has completed */\r
84                         protected void done() {\r
85                                 try {\r
86                                         status = get();\r
87                                 } catch (InterruptedException e) {\r
88                                         e.printStackTrace();\r
89                                         return;\r
90                                 } catch (ExecutionException e) {\r
91                                         e.printStackTrace();\r
92                                         return;\r
93                                 }\r
94                                 dispose();\r
95                         }\r
96                 };\r
97                 initComponents();\r
98                 worker.execute();\r
99         }\r
100 \r
101         private void initComponents() {\r
102                 setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);\r
103 \r
104                 Container pane = getContentPane();\r
105                 GridLayout btnLayout = new GridLayout(3, 1);\r
106                 pane.setLayout(btnLayout);\r
107 \r
108                 \r
109                 JLabel messageLbl1 = new JLabel(        \r
110                 "<html>"\r
111                 + "<table border=\"0\">" + "<tr>"\r
112                 + _("Grid resolution in progress...") + "</tr>"\r
113                 + "</html>");\r
114                 \r
115                 JLabel messageLbl3 = new JLabel("");\r
116                 JButton cancelBtn = new JButton(_("Cancel"));\r
117                 cancelBtn.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);\r
118                 cancelBtn.addActionListener(new java.awt.event.ActionListener() {\r
119                         public void actionPerformed(java.awt.event.ActionEvent evt) {\r
120                                 clickedCancel();\r
121                         }\r
122                 });\r
123 \r
124                 pane.add(messageLbl1);\r
125                 pane.add(messageLbl3);\r
126                 pane.add(cancelBtn);\r
127 \r
128                 pack();\r
129                 setLocationRelativeTo(parent);\r
130         }\r
131 \r
132         private void clickedCancel() {\r
133                 System.out.println("CheckUpdateDialog.buttonClicked()");\r
134                 /**\r
135                  * CANCELLED\r
136                  */\r
137                 System.out\r
138                                 .println("ResolveGridDialog.ResolveGridDialog(...) CANCELLED");\r
139                 bruteSolver.cancel();\r
140         }\r
141 \r
142         /**\r
143          * \r
144          * @return <b>0</b> if the resolution was successful<br/>\r
145          *         <b>1</b> if the solving process was canceled by the user before\r
146          *         completion<br/>\r
147          *         <b>2</b> if the process failed to resolve the grid\r
148          */\r
149         public int getResult() {\r
150                 return status;\r
151         }\r
152 \r
153         private int resolveGrid() {\r
154 \r
155                 GridSolution solution = bruteSolver.resolve();\r
156                 if (solution == null) {\r
157                         /**\r
158                          * RESOLUTION PROCESS CANCELLED BEFORE COMPLETION\r
159                          */\r
160                         return 1;\r
161                 }\r
162                 if (solution.isSolved()) {\r
163                         GridModel solGrid = solution.getSolutionGrid();\r
164                         for (int li = 0; li < 9; li++) {\r
165                                 for (int co = 0; co < 9; co++) {\r
166                                         byte value = solGrid.getValueAt(li, co);\r
167                                         view.getController().notifyGridValueChanged(li, co, value, true);\r
168                                 }\r
169                         }\r
170                         view.getController().notifyGridResolutionSuccess();\r
171                         /**\r
172                          * RESOLUTION SUCCESSFULL\r
173                          */\r
174                         return 0;\r
175                 } else {\r
176                         /**\r
177                          * RESOLUTION PROCESS WAS UNABLE TO SOLVE THIS GRID\r
178                          */\r
179                         return 2;\r
180                 }\r
181         }\r
182 \r
183 }\r