Fri, 01 Feb 2013 10:18:47 +0100
removed completeness test - the solver automatically returns false when the field is incomplete
3
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
1 | /* |
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
2 | * Copyright 2013 Mike Becker. All rights reserved. |
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
3 | * |
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
4 | * Redistribution and use in source and binary forms, with or without |
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
5 | * modification, are permitted provided that the following conditions are met: |
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
6 | * |
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
7 | * 1. Redistributions of source code must retain the above copyright |
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
8 | * notice, this list of conditions and the following disclaimer. |
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
9 | * |
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
10 | * 2. Redistributions in binary form must reproduce the above copyright |
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
11 | * notice, this list of conditions and the following disclaimer in the |
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
12 | * documentation and/or other materials provided with the distribution. |
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
13 | * |
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
15 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
18 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
19 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
20 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
21 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
22 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
23 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
24 | * POSSIBILITY OF SUCH DAMAGE. |
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
25 | */ |
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
26 | |
2 | 27 | package de.uapcore.sudoku; |
28 | ||
29 | import java.awt.event.ActionEvent; | |
30 | import java.awt.event.ActionListener; | |
6 | 31 | import java.io.File; |
32 | import java.io.IOException; | |
33 | import javax.swing.JFileChooser; | |
2 | 34 | import javax.swing.JOptionPane; |
35 | ||
36 | /** | |
37 | * | |
38 | * @author mike | |
39 | */ | |
40 | public final class ActionHandler implements ActionListener { | |
41 | ||
42 | public static final String SAVE = "save"; | |
43 | public static final String CHECK = "check"; | |
44 | public static final String SOLVE = "solve"; | |
45 | ||
3
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
46 | public static final String NEW = "new"; |
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
47 | public static final String OPEN = "open"; |
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
48 | public static final String SAVE_AS = "save as"; |
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
49 | public static final String QUIT = "quit"; |
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
50 | public static final String ABOUT = "about"; |
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
51 | |
2 | 52 | private Field field; |
53 | private Solver solver; | |
5
8ddf4af937d7
moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
54 | private DocumentHandler doc; |
2 | 55 | |
56 | public ActionHandler(Field f) { | |
57 | field = f; | |
58 | solver = new Solver(); | |
5
8ddf4af937d7
moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
59 | doc = new DocumentHandler(); |
8ddf4af937d7
moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
60 | } |
8ddf4af937d7
moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
61 | |
8ddf4af937d7
moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
62 | private boolean chooseSaveFilename() { |
6 | 63 | JFileChooser fc = new JFileChooser("."); |
64 | fc.setMultiSelectionEnabled(false); | |
65 | if (fc.showSaveDialog(field) == JFileChooser.APPROVE_OPTION) { | |
66 | File f = fc.getSelectedFile(); | |
67 | if (f.exists()) { | |
68 | int result = JOptionPane.showConfirmDialog(field, | |
69 | "Bereits existierende Datei überschreiben?", "Sudoku", | |
70 | JOptionPane.YES_NO_OPTION); | |
71 | if (result == JOptionPane.YES_OPTION) { | |
72 | doc.setFilename(f.getAbsolutePath()); | |
73 | return true; | |
74 | } else { | |
75 | return false; | |
76 | } | |
77 | } else { | |
78 | doc.setFilename(f.getAbsolutePath()); | |
79 | return true; | |
80 | } | |
81 | } else { | |
82 | return false; | |
83 | } | |
2 | 84 | } |
85 | ||
6 | 86 | private void open() { |
87 | JFileChooser fc = new JFileChooser("."); | |
88 | fc.setMultiSelectionEnabled(false); | |
89 | if (fc.showOpenDialog(field) == JFileChooser.APPROVE_OPTION) { | |
90 | File f = fc.getSelectedFile(); | |
91 | doc.setFilename(f.getAbsolutePath()); | |
92 | try { | |
93 | doc.load(field); | |
94 | } catch (IOException e) { | |
95 | JOptionPane.showMessageDialog(field, | |
96 | "Datei konnte nicht geladen werden: "+e.getMessage(), | |
97 | "Sudoku", JOptionPane.ERROR_MESSAGE); | |
98 | } | |
99 | } | |
100 | } | |
101 | ||
102 | private boolean save(boolean rename) { | |
103 | if (!doc.isFilenameSet() || rename) { | |
5
8ddf4af937d7
moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
104 | if (!chooseSaveFilename()) { |
8ddf4af937d7
moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
105 | return false; |
8ddf4af937d7
moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
106 | } |
8ddf4af937d7
moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
107 | } |
2 | 108 | if (solver.check(field)) { |
109 | field.setAllCellsModified(false); | |
6 | 110 | try { |
111 | doc.save(field); | |
112 | } catch (IOException e) { | |
113 | JOptionPane.showMessageDialog(field, | |
114 | "Datei konnte nicht gespeichert werden: "+e.getMessage(), | |
115 | "Sudoku", JOptionPane.ERROR_MESSAGE); | |
116 | } | |
3
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
117 | return true; |
2 | 118 | } else { |
119 | JOptionPane.showMessageDialog(field, | |
120 | "Das Feld kann mit Fehlern nicht gespeichert werden!", | |
121 | "Sudoku", JOptionPane.ERROR_MESSAGE); | |
3
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
122 | return false; |
2 | 123 | } |
124 | } | |
125 | ||
126 | private void check() { | |
127 | if (solver.check(field)) { | |
128 | JOptionPane.showMessageDialog(field, "Überprüfung erfolgreich!", | |
129 | "Sudoku", JOptionPane.INFORMATION_MESSAGE); | |
130 | } else { | |
131 | JOptionPane.showMessageDialog(field, "Das Feld enthält Fehler!", | |
132 | "Sudoku", JOptionPane.WARNING_MESSAGE); | |
133 | } | |
134 | } | |
135 | ||
136 | private void solve() { | |
7 | 137 | if (!solver.check(field) || !solver.solve(field)) { |
138 | JOptionPane.showMessageDialog(field, "Das Feld ist nicht lösbar!", | |
139 | "Sudoku", JOptionPane.WARNING_MESSAGE); | |
140 | } | |
2 | 141 | } |
4 | 142 | |
143 | private boolean saveUnsaved() { | |
144 | boolean proceed = false; | |
145 | if (field.isAnyCellModified()) { | |
146 | int result = JOptionPane.showConfirmDialog(field, | |
147 | "Das Feld ist ungespeichert - jetzt speichern?", | |
148 | "Sudoku", JOptionPane.YES_NO_CANCEL_OPTION); | |
149 | if (result == JOptionPane.YES_OPTION) { | |
6 | 150 | if (save(false)) { |
4 | 151 | proceed = true; |
152 | } | |
153 | } else if (result == JOptionPane.NO_OPTION) { | |
154 | proceed = true; | |
155 | } | |
156 | } else { | |
157 | proceed = true; | |
158 | } | |
159 | ||
160 | return proceed; | |
161 | } | |
2 | 162 | |
163 | @Override | |
164 | public void actionPerformed(ActionEvent e) { | |
165 | switch (e.getActionCommand()) { | |
4 | 166 | case NEW: |
167 | if (saveUnsaved()) { | |
7 | 168 | doc.clearFilename(); |
4 | 169 | field.clear(); |
170 | } | |
171 | break; | |
6 | 172 | case OPEN: |
173 | open(); | |
174 | break; | |
3
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
175 | case SAVE: |
6 | 176 | save(false); |
177 | break; | |
178 | case SAVE_AS: | |
179 | save(true); | |
2 | 180 | break; |
3
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
181 | case CHECK: |
2 | 182 | check(); |
183 | break; | |
3
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
184 | case SOLVE: |
2 | 185 | solve(); |
186 | break; | |
3
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
187 | case QUIT: |
4 | 188 | if (saveUnsaved()) { |
3
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
189 | System.exit(0); |
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
190 | } |
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
191 | break; |
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
192 | case ABOUT: |
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
193 | JOptionPane.showMessageDialog(field, |
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
194 | "Sudoku - Copyright (c) 2013 Mike Becker\nwww.uap-core.de"+ |
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
195 | "\nPublished under the BSD License", |
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
196 | "Sudoku", JOptionPane.INFORMATION_MESSAGE); |
ed931970b4ac
added license and main menu
Mike Becker <universe@uap-core.de>
parents:
2
diff
changeset
|
197 | break; |
2 | 198 | default: |
199 | throw new UnsupportedOperationException( | |
200 | "unknown action: "+e.getActionCommand()); | |
201 | } | |
202 | } | |
203 | ||
204 | } |