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 java.awt.event.ActionEvent; |
|
30 import java.awt.event.ActionListener; |
|
31 import java.io.File; |
|
32 import java.io.IOException; |
|
33 import javax.swing.JFileChooser; |
|
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 |
|
46 public static final String NEW = "new"; |
|
47 public static final String OPEN = "open"; |
|
48 public static final String SAVE_AS = "save as"; |
|
49 public static final String QUIT = "quit"; |
|
50 public static final String ABOUT = "about"; |
|
51 |
|
52 private Field field; |
|
53 private Solver solver; |
|
54 private DocumentHandler doc; |
|
55 |
|
56 public ActionHandler(Field f) { |
|
57 field = f; |
|
58 solver = new Solver(); |
|
59 doc = new DocumentHandler(); |
|
60 } |
|
61 |
|
62 private boolean chooseSaveFilename() { |
|
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 } |
|
84 } |
|
85 |
|
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) { |
|
104 if (!chooseSaveFilename()) { |
|
105 return false; |
|
106 } |
|
107 } |
|
108 if (solver.check(field)) { |
|
109 field.setAllCellsModified(false); |
|
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 } |
|
117 return true; |
|
118 } else { |
|
119 JOptionPane.showMessageDialog(field, |
|
120 "Das Feld kann mit Fehlern nicht gespeichert werden!", |
|
121 "Sudoku", JOptionPane.ERROR_MESSAGE); |
|
122 return false; |
|
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() { |
|
137 if (!solver.check(field) || !solver.solve(field)) { |
|
138 JOptionPane.showMessageDialog(field, "Das Feld ist nicht lösbar!", |
|
139 "Sudoku", JOptionPane.WARNING_MESSAGE); |
|
140 } |
|
141 } |
|
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) { |
|
150 if (save(false)) { |
|
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 } |
|
162 |
|
163 @Override |
|
164 public void actionPerformed(ActionEvent e) { |
|
165 switch (e.getActionCommand()) { |
|
166 case NEW: |
|
167 if (saveUnsaved()) { |
|
168 doc.clearFilename(); |
|
169 field.clear(); |
|
170 } |
|
171 break; |
|
172 case OPEN: |
|
173 open(); |
|
174 break; |
|
175 case SAVE: |
|
176 save(false); |
|
177 break; |
|
178 case SAVE_AS: |
|
179 save(true); |
|
180 break; |
|
181 case CHECK: |
|
182 check(); |
|
183 break; |
|
184 case SOLVE: |
|
185 solve(); |
|
186 break; |
|
187 case QUIT: |
|
188 if (saveUnsaved()) { |
|
189 System.exit(0); |
|
190 } |
|
191 break; |
|
192 case ABOUT: |
|
193 JOptionPane.showMessageDialog(field, |
|
194 "Sudoku - Copyright (c) 2013 Mike Becker\nwww.uap-core.de"+ |
|
195 "\nPublished under the BSD License", |
|
196 "Sudoku", JOptionPane.INFORMATION_MESSAGE); |
|
197 break; |
|
198 default: |
|
199 throw new UnsupportedOperationException( |
|
200 "unknown action: "+e.getActionCommand()); |
|
201 } |
|
202 } |
|
203 |
|
204 } |
|