src/main/java/de/uapcore/sudoku/ActionHandler.java

Mon, 27 Jul 2020 11:56:41 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 27 Jul 2020 11:56:41 +0200
changeset 13
5e69b1bb707f
parent 12
1c62c6009161
child 22
06170a0be62a
permissions
-rw-r--r--

adds SudokuTextFieldTest

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.
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
3 *
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:
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
6 *
3
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.
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
9 *
3
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.
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
13 *
3
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
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 package de.uapcore.sudoku;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28
9
576e7a2861ae converts to maven project
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
29 import javax.swing.*;
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30 import java.awt.event.ActionEvent;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31 import java.awt.event.ActionListener;
6
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
32 import java.io.File;
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
33 import java.io.IOException;
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35 /**
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
36 * Handles all user issued actions in the application.
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37 */
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 public final class ActionHandler implements ActionListener {
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
39
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 public static final String SAVE = "save";
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 public static final String CHECK = "check";
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42 public static final String SOLVE = "solve";
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
43
3
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
44 public static final String NEW = "new";
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
45 public static final String OPEN = "open";
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
46 public static final String SAVE_AS = "save as";
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
47 public static final String QUIT = "quit";
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
48 public static final String ABOUT = "about";
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
49
12
1c62c6009161 fixes some code inspection issues
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
50 private final Field field;
1c62c6009161 fixes some code inspection issues
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
51 private final Solver solver;
1c62c6009161 fixes some code inspection issues
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
52 private final DocumentHandler doc;
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
53
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
54 /**
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
55 * Constructs a new action handler instance.
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
56 *
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
57 * @param f a reference to the playing field
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
58 */
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 public ActionHandler(Field f) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 field = f;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 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
62 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
63 }
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
64
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
65 /**
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
66 * Prompts the user for a file name.
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
67 * <p>
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
68 * If the user chooses an existing file, they are asked whether the file should be overwritten.
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
69 *
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
70 * @return true if the user approves a chosen file name
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
71 */
5
8ddf4af937d7 moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
72 private boolean chooseSaveFilename() {
6
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
73 JFileChooser fc = new JFileChooser(".");
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
74 fc.setMultiSelectionEnabled(false);
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
75 if (fc.showSaveDialog(field) == JFileChooser.APPROVE_OPTION) {
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
76 File f = fc.getSelectedFile();
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
77 if (f.exists()) {
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
78 int result = JOptionPane.showConfirmDialog(field,
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
79 "Bereits existierende Datei ĂĽberschreiben?", "Sudoku",
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
80 JOptionPane.YES_NO_OPTION);
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
81 if (result == JOptionPane.YES_OPTION) {
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
82 doc.setFilename(f.getAbsolutePath());
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
83 return true;
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
84 } else {
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
85 return false;
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
86 }
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
87 } else {
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
88 doc.setFilename(f.getAbsolutePath());
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
89 return true;
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
90 }
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
91 } else {
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
92 return false;
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
93 }
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
94 }
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
95
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
96 /**
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
97 * Prompts the user for a file to open and, if approved, loads that file.
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
98 */
6
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
99 private void open() {
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
100 JFileChooser fc = new JFileChooser(".");
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
101 fc.setMultiSelectionEnabled(false);
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
102 if (fc.showOpenDialog(field) == JFileChooser.APPROVE_OPTION) {
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
103 File f = fc.getSelectedFile();
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
104 doc.setFilename(f.getAbsolutePath());
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
105 try {
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
106 doc.load(field);
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
107 } catch (IOException e) {
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
108 JOptionPane.showMessageDialog(field,
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
109 "Datei konnte nicht geladen werden: " + e.getMessage(),
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
110 "Sudoku", JOptionPane.ERROR_MESSAGE);
6
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
111 }
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
112 }
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
113 }
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
114
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
115 /**
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
116 * Attempts to save the Sudoku field to a file.
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
117 * <p>
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
118 * If necessary, the user is prompted for a file name.
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
119 * <p>
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
120 * The field must be solvable, otherwise it cannot be saved.
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
121 *
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
122 * @param rename true if the user shall always be prompted, even if a file name is already known
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
123 * @return true if the user approves the chosen file name
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
124 */
6
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
125 private boolean save(boolean rename) {
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
126 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
127 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
128 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
129 }
8ddf4af937d7 moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
130 }
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
131 if (solver.check(field)) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
132 field.setAllCellsModified(false);
6
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
133 try {
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
134 doc.save(field);
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
135 } catch (IOException e) {
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
136 JOptionPane.showMessageDialog(field,
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
137 "Datei konnte nicht gespeichert werden: " + e.getMessage(),
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
138 "Sudoku", JOptionPane.ERROR_MESSAGE);
6
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
139 }
3
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
140 return true;
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
141 } else {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
142 JOptionPane.showMessageDialog(field,
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
143 "Das Feld kann mit Fehlern nicht gespeichert werden!",
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
144 "Sudoku", JOptionPane.ERROR_MESSAGE);
3
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
145 return false;
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
146 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
147 }
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
148
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
149 /**
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
150 * Checks the Sudoku field and displays the result as a dialog box.
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
151 */
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
152 private void check() {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
153 if (solver.check(field)) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
154 JOptionPane.showMessageDialog(field, "ĂśberprĂĽfung erfolgreich!",
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
155 "Sudoku", JOptionPane.INFORMATION_MESSAGE);
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
156 } else {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
157 JOptionPane.showMessageDialog(field, "Das Feld enthält Fehler!",
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
158 "Sudoku", JOptionPane.WARNING_MESSAGE);
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
159 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
160 }
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
161
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
162 /**
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
163 * Solves the field or displays an error dialog if the field is not solvable.
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
164 */
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
165 private void solve() {
7
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
166 if (!solver.check(field) || !solver.solve(field)) {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
167 JOptionPane.showMessageDialog(field, "Das Feld ist nicht lösbar!",
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
168 "Sudoku", JOptionPane.WARNING_MESSAGE);
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
169 }
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
170 }
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
171
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
172 /**
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
173 * Checks whether there are unsaved changes and asks the user to save the field.
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
174 *
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
175 * @return true if there are no unsaved changes or the user actively decides to continue - false, otherwise
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
176 */
4
b8588e318001 added NEW function
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
177 private boolean saveUnsaved() {
b8588e318001 added NEW function
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
178 boolean proceed = false;
b8588e318001 added NEW function
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
179 if (field.isAnyCellModified()) {
b8588e318001 added NEW function
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
180 int result = JOptionPane.showConfirmDialog(field,
b8588e318001 added NEW function
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
181 "Das Feld ist ungespeichert - jetzt speichern?",
b8588e318001 added NEW function
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
182 "Sudoku", JOptionPane.YES_NO_CANCEL_OPTION);
b8588e318001 added NEW function
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
183 if (result == JOptionPane.YES_OPTION) {
6
5bab2e971333 file IO
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
184 if (save(false)) {
4
b8588e318001 added NEW function
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
185 proceed = true;
b8588e318001 added NEW function
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
186 }
b8588e318001 added NEW function
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
187 } else if (result == JOptionPane.NO_OPTION) {
b8588e318001 added NEW function
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
188 proceed = true;
b8588e318001 added NEW function
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
189 }
b8588e318001 added NEW function
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
190 } else {
b8588e318001 added NEW function
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
191 proceed = true;
b8588e318001 added NEW function
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
192 }
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
193
4
b8588e318001 added NEW function
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
194 return proceed;
b8588e318001 added NEW function
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
195 }
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
196
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
197 @Override
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
198 public void actionPerformed(ActionEvent e) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
199 switch (e.getActionCommand()) {
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
200 case NEW:
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
201 if (saveUnsaved()) {
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
202 doc.clearFilename();
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
203 field.clear();
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
204 }
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
205 break;
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
206 case OPEN:
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
207 open();
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
208 break;
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
209 case SAVE:
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
210 save(false);
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
211 break;
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
212 case SAVE_AS:
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
213 save(true);
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
214 break;
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
215 case CHECK:
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
216 check();
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
217 break;
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
218 case SOLVE:
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
219 solve();
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
220 break;
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
221 case QUIT:
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
222 if (saveUnsaved()) {
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
223 System.exit(0);
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
224 }
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
225 break;
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
226 case ABOUT:
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
227 JOptionPane.showMessageDialog(field,
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
228 "Sudoku - Copyright (c) 2013 Mike Becker\nwww.uap-core.de" +
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
229 "\nPublished under the BSD License",
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
230 "Sudoku", JOptionPane.INFORMATION_MESSAGE);
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
231 break;
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
232 default:
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
233 throw new UnsupportedOperationException(
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
234 "unknown action: " + e.getActionCommand());
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
235 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
236 }
10
369903afbb29 adds more javadoc
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
237
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
238 }

mercurial