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