Sat, 26 Jan 2013 18:43:49 +0100
added NEW function
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 javax.swing.JOptionPane;
33 /**
34 *
35 * @author mike
36 */
37 public final class ActionHandler implements ActionListener {
39 public static final String SAVE = "save";
40 public static final String CHECK = "check";
41 public static final String SOLVE = "solve";
43 public static final String NEW = "new";
44 public static final String OPEN = "open";
45 public static final String SAVE_AS = "save as";
46 public static final String QUIT = "quit";
47 public static final String ABOUT = "about";
49 private Field field;
50 private Solver solver;
52 public ActionHandler(Field f) {
53 field = f;
54 solver = new Solver();
55 }
57 private boolean save() {
58 if (solver.check(field)) {
59 field.setAllCellsModified(false);
60 // TODO: save to file
61 return true;
62 } else {
63 JOptionPane.showMessageDialog(field,
64 "Das Feld kann mit Fehlern nicht gespeichert werden!",
65 "Sudoku", JOptionPane.ERROR_MESSAGE);
66 return false;
67 }
68 }
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 }
80 private void solve() {
81 // TODO: solve
82 }
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 }
101 return proceed;
102 }
104 @Override
105 public void actionPerformed(ActionEvent e) {
106 switch (e.getActionCommand()) {
107 case NEW:
108 if (saveUnsaved()) {
109 field.clear();
110 }
111 break;
112 case SAVE:
113 save();
114 break;
115 case CHECK:
116 check();
117 break;
118 case SOLVE:
119 solve();
120 break;
121 case QUIT:
122 if (saveUnsaved()) {
123 System.exit(0);
124 }
125 break;
126 case ABOUT:
127 JOptionPane.showMessageDialog(field,
128 "Sudoku - Copyright (c) 2013 Mike Becker\nwww.uap-core.de"+
129 "\nPublished under the BSD License",
130 "Sudoku", JOptionPane.INFORMATION_MESSAGE);
131 break;
132 default:
133 throw new UnsupportedOperationException(
134 "unknown action: "+e.getActionCommand());
135 }
136 }
138 }