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