Sun, 27 Jan 2013 15:03:57 +0100
file IO
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 */
27 package de.uapcore.sudoku;
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;
36 /**
37 *
38 * @author mike
39 */
40 public final class ActionHandler implements ActionListener {
42 public static final String SAVE = "save";
43 public static final String CHECK = "check";
44 public static final String SOLVE = "solve";
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";
52 private Field field;
53 private Solver solver;
54 private DocumentHandler doc;
56 public ActionHandler(Field f) {
57 field = f;
58 solver = new Solver();
59 doc = new DocumentHandler();
60 }
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 }
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 }
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 }
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 }
136 private void solve() {
137 // TODO: solve
138 }
140 private boolean saveUnsaved() {
141 boolean proceed = false;
142 if (field.isAnyCellModified()) {
143 int result = JOptionPane.showConfirmDialog(field,
144 "Das Feld ist ungespeichert - jetzt speichern?",
145 "Sudoku", JOptionPane.YES_NO_CANCEL_OPTION);
146 if (result == JOptionPane.YES_OPTION) {
147 if (save(false)) {
148 proceed = true;
149 }
150 } else if (result == JOptionPane.NO_OPTION) {
151 proceed = true;
152 }
153 } else {
154 proceed = true;
155 }
157 return proceed;
158 }
160 @Override
161 public void actionPerformed(ActionEvent e) {
162 switch (e.getActionCommand()) {
163 case NEW:
164 if (saveUnsaved()) {
165 field.clear();
166 }
167 break;
168 case OPEN:
169 open();
170 break;
171 case SAVE:
172 save(false);
173 break;
174 case SAVE_AS:
175 save(true);
176 break;
177 case CHECK:
178 check();
179 break;
180 case SOLVE:
181 solve();
182 break;
183 case QUIT:
184 if (saveUnsaved()) {
185 System.exit(0);
186 }
187 break;
188 case ABOUT:
189 JOptionPane.showMessageDialog(field,
190 "Sudoku - Copyright (c) 2013 Mike Becker\nwww.uap-core.de"+
191 "\nPublished under the BSD License",
192 "Sudoku", JOptionPane.INFORMATION_MESSAGE);
193 break;
194 default:
195 throw new UnsupportedOperationException(
196 "unknown action: "+e.getActionCommand());
197 }
198 }
200 }