|
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 */ |
|
26 |
1 package de.uapcore.sudoku; |
27 package de.uapcore.sudoku; |
2 |
28 |
3 import java.awt.event.ActionEvent; |
29 import java.awt.event.ActionEvent; |
4 import java.awt.event.ActionListener; |
30 import java.awt.event.ActionListener; |
5 import javax.swing.JOptionPane; |
31 import javax.swing.JOptionPane; |
12 |
38 |
13 public static final String SAVE = "save"; |
39 public static final String SAVE = "save"; |
14 public static final String CHECK = "check"; |
40 public static final String CHECK = "check"; |
15 public static final String SOLVE = "solve"; |
41 public static final String SOLVE = "solve"; |
16 |
42 |
|
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"; |
|
48 |
17 private Field field; |
49 private Field field; |
18 private Solver solver; |
50 private Solver solver; |
19 |
51 |
20 public ActionHandler(Field f) { |
52 public ActionHandler(Field f) { |
21 field = f; |
53 field = f; |
22 solver = new Solver(); |
54 solver = new Solver(); |
23 } |
55 } |
24 |
56 |
25 private void save() { |
57 private boolean save() { |
26 if (solver.check(field)) { |
58 if (solver.check(field)) { |
27 field.setAllCellsModified(false); |
59 field.setAllCellsModified(false); |
28 // TODO: save to file |
60 // TODO: save to file |
|
61 return true; |
29 } else { |
62 } else { |
30 JOptionPane.showMessageDialog(field, |
63 JOptionPane.showMessageDialog(field, |
31 "Das Feld kann mit Fehlern nicht gespeichert werden!", |
64 "Das Feld kann mit Fehlern nicht gespeichert werden!", |
32 "Sudoku", JOptionPane.ERROR_MESSAGE); |
65 "Sudoku", JOptionPane.ERROR_MESSAGE); |
|
66 return false; |
33 } |
67 } |
34 } |
68 } |
35 |
69 |
36 private void check() { |
70 private void check() { |
37 if (solver.check(field)) { |
71 if (solver.check(field)) { |
48 } |
82 } |
49 |
83 |
50 @Override |
84 @Override |
51 public void actionPerformed(ActionEvent e) { |
85 public void actionPerformed(ActionEvent e) { |
52 switch (e.getActionCommand()) { |
86 switch (e.getActionCommand()) { |
53 case "save": |
87 case SAVE: |
54 save(); |
88 save(); |
55 break; |
89 break; |
56 case "check": |
90 case CHECK: |
57 check(); |
91 check(); |
58 break; |
92 break; |
59 case "solve": |
93 case SOLVE: |
60 solve(); |
94 solve(); |
|
95 break; |
|
96 case QUIT: |
|
97 if (field.isAnyCellModified()) { |
|
98 int result = JOptionPane.showConfirmDialog(field, |
|
99 "Das Feld ist ungespeichert - jetzt speichern?", |
|
100 "Sudoku", JOptionPane.YES_NO_CANCEL_OPTION); |
|
101 if (result == JOptionPane.YES_OPTION) { |
|
102 if (save()) { |
|
103 System.exit(0); |
|
104 } |
|
105 } else if (result == JOptionPane.NO_OPTION) { |
|
106 System.exit(0); |
|
107 } |
|
108 } else { |
|
109 System.exit(0); |
|
110 } |
|
111 break; |
|
112 case ABOUT: |
|
113 JOptionPane.showMessageDialog(field, |
|
114 "Sudoku - Copyright (c) 2013 Mike Becker\nwww.uap-core.de"+ |
|
115 "\nPublished under the BSD License", |
|
116 "Sudoku", JOptionPane.INFORMATION_MESSAGE); |
61 break; |
117 break; |
62 default: |
118 default: |
63 throw new UnsupportedOperationException( |
119 throw new UnsupportedOperationException( |
64 "unknown action: "+e.getActionCommand()); |
120 "unknown action: "+e.getActionCommand()); |
65 } |
121 } |