src/de/uapcore/sudoku/ActionHandler.java

Sat, 26 Jan 2013 18:38:12 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 26 Jan 2013 18:38:12 +0100
changeset 3
ed931970b4ac
parent 2
5179eff8a9b6
child 4
b8588e318001
permissions
-rw-r--r--

added license and main menu

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
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
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 import java.awt.event.ActionEvent;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30 import java.awt.event.ActionListener;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31 import javax.swing.JOptionPane;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33 /**
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 * @author mike
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36 */
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37 public final class ActionHandler implements ActionListener {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 public static final String SAVE = "save";
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 public static final String CHECK = "check";
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 public static final String SOLVE = "solve";
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42
3
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
43 public static final String NEW = "new";
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
44 public static final String OPEN = "open";
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
45 public static final String SAVE_AS = "save as";
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
46 public static final String QUIT = "quit";
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
47 public static final String ABOUT = "about";
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
48
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49 private Field field;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 private Solver solver;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 public ActionHandler(Field f) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 field = f;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 solver = new Solver();
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56
3
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
57 private boolean save() {
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 if (solver.check(field)) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 field.setAllCellsModified(false);
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 // TODO: save to file
3
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
61 return true;
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 } else {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 JOptionPane.showMessageDialog(field,
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64 "Das Feld kann mit Fehlern nicht gespeichert werden!",
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 "Sudoku", JOptionPane.ERROR_MESSAGE);
3
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
66 return false;
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 private void check() {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 if (solver.check(field)) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 JOptionPane.showMessageDialog(field, "Überprüfung erfolgreich!",
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73 "Sudoku", JOptionPane.INFORMATION_MESSAGE);
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 } else {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75 JOptionPane.showMessageDialog(field, "Das Feld enthält Fehler!",
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 "Sudoku", JOptionPane.WARNING_MESSAGE);
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 private void solve() {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 // TODO: solve
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 @Override
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85 public void actionPerformed(ActionEvent e) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 switch (e.getActionCommand()) {
3
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
87 case SAVE:
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 save();
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89 break;
3
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
90 case CHECK:
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91 check();
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92 break;
3
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
93 case SOLVE:
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
94 solve();
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95 break;
3
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
96 case QUIT:
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
97 if (field.isAnyCellModified()) {
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
98 int result = JOptionPane.showConfirmDialog(field,
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
99 "Das Feld ist ungespeichert - jetzt speichern?",
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
100 "Sudoku", JOptionPane.YES_NO_CANCEL_OPTION);
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
101 if (result == JOptionPane.YES_OPTION) {
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
102 if (save()) {
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
103 System.exit(0);
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
104 }
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
105 } else if (result == JOptionPane.NO_OPTION) {
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
106 System.exit(0);
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
107 }
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
108 } else {
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
109 System.exit(0);
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
110 }
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
111 break;
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
112 case ABOUT:
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
113 JOptionPane.showMessageDialog(field,
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
114 "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
115 "\nPublished under the BSD License",
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
116 "Sudoku", JOptionPane.INFORMATION_MESSAGE);
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
117 break;
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
118 default:
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
119 throw new UnsupportedOperationException(
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
120 "unknown action: "+e.getActionCommand());
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
121 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
122 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
123
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
124 }

mercurial