2 * Sudokuki - essential sudoku game
\r
3 * Copyright (C) 2007-2012 Sylvain Vedrenne
\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
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
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
18 package net.jankenpoi.sudokuki.ui.swing;
\r
20 import static net.jankenpoi.i18n.I18n._;
\r
22 import java.awt.Container;
\r
23 import java.awt.GridLayout;
\r
24 import java.util.concurrent.ExecutionException;
\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
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
37 * CheckUpdateDialog.java
\r
41 @SuppressWarnings("serial")
\r
42 public class ResolveGridDialog extends JDialog {
\r
44 private JFrame parent;
\r
46 private int status = -1;
\r
48 private final GridView view;
\r
50 private final SwingWorker<Integer, Void> worker;
\r
52 private final BruteForceGridSolver bruteSolver;
\r
53 private final GridModel gridToSolve;
\r
55 public ResolveGridDialog(JFrame parent, final GridView view,
\r
56 final ResolveAction resolveAction) {
\r
58 super(parent, true);
\r
59 this.parent = parent;
\r
61 setResizable(false);
\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
71 gridToSolve = new GridModel(flagsTable, 0);
\r
72 bruteSolver = new BruteForceGridSolver(gridToSolve);
\r
74 worker = new SwingWorker<Integer, Void>() {
\r
77 /* Executed in the SwingWorker thread */
\r
78 protected Integer doInBackground() {
\r
79 return resolveGrid();
\r
83 /* Executed in the EDT, triggered when the SwingWorker has completed */
\r
84 protected void done() {
\r
87 } catch (InterruptedException e) {
\r
88 e.printStackTrace();
\r
90 } catch (ExecutionException e) {
\r
91 e.printStackTrace();
\r
101 private void initComponents() {
\r
102 setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
\r
104 Container pane = getContentPane();
\r
105 GridLayout btnLayout = new GridLayout(3, 1);
\r
106 pane.setLayout(btnLayout);
\r
109 JLabel messageLbl1 = new JLabel(
\r
111 + "<table border=\"0\">" + "<tr>"
\r
112 + _("Grid resolution in progress...") + "</tr>"
\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
124 pane.add(messageLbl1);
\r
125 pane.add(messageLbl3);
\r
126 pane.add(cancelBtn);
\r
129 setLocationRelativeTo(parent);
\r
132 private void clickedCancel() {
\r
133 System.out.println("CheckUpdateDialog.buttonClicked()");
\r
138 .println("ResolveGridDialog.ResolveGridDialog(...) CANCELLED");
\r
139 bruteSolver.cancel();
\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
147 * <b>2</b> if the process failed to resolve the grid
\r
149 public int getResult() {
\r
153 private int resolveGrid() {
\r
155 GridSolution solution = bruteSolver.resolve();
\r
156 if (solution == null) {
\r
158 * RESOLUTION PROCESS CANCELLED BEFORE COMPLETION
\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
170 view.getController().notifyGridResolutionSuccess();
\r
172 * RESOLUTION SUCCESSFULL
\r
177 * RESOLUTION PROCESS WAS UNABLE TO SOLVE THIS GRID
\r