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

universe@3 1 /*
universe@3 2 * Copyright 2013 Mike Becker. All rights reserved.
universe@3 3 *
universe@3 4 * Redistribution and use in source and binary forms, with or without
universe@3 5 * modification, are permitted provided that the following conditions are met:
universe@3 6 *
universe@3 7 * 1. Redistributions of source code must retain the above copyright
universe@3 8 * notice, this list of conditions and the following disclaimer.
universe@3 9 *
universe@3 10 * 2. Redistributions in binary form must reproduce the above copyright
universe@3 11 * notice, this list of conditions and the following disclaimer in the
universe@3 12 * documentation and/or other materials provided with the distribution.
universe@3 13 *
universe@3 14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@3 15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@3 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@3 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@3 18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@3 19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@3 20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@3 21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@3 22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@3 23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@3 24 * POSSIBILITY OF SUCH DAMAGE.
universe@3 25 */
universe@3 26
universe@2 27 package de.uapcore.sudoku;
universe@2 28
universe@2 29 import java.awt.event.ActionEvent;
universe@2 30 import java.awt.event.ActionListener;
universe@2 31 import javax.swing.JOptionPane;
universe@2 32
universe@2 33 /**
universe@2 34 *
universe@2 35 * @author mike
universe@2 36 */
universe@2 37 public final class ActionHandler implements ActionListener {
universe@2 38
universe@2 39 public static final String SAVE = "save";
universe@2 40 public static final String CHECK = "check";
universe@2 41 public static final String SOLVE = "solve";
universe@2 42
universe@3 43 public static final String NEW = "new";
universe@3 44 public static final String OPEN = "open";
universe@3 45 public static final String SAVE_AS = "save as";
universe@3 46 public static final String QUIT = "quit";
universe@3 47 public static final String ABOUT = "about";
universe@3 48
universe@2 49 private Field field;
universe@2 50 private Solver solver;
universe@2 51
universe@2 52 public ActionHandler(Field f) {
universe@2 53 field = f;
universe@2 54 solver = new Solver();
universe@2 55 }
universe@2 56
universe@3 57 private boolean save() {
universe@2 58 if (solver.check(field)) {
universe@2 59 field.setAllCellsModified(false);
universe@2 60 // TODO: save to file
universe@3 61 return true;
universe@2 62 } else {
universe@2 63 JOptionPane.showMessageDialog(field,
universe@2 64 "Das Feld kann mit Fehlern nicht gespeichert werden!",
universe@2 65 "Sudoku", JOptionPane.ERROR_MESSAGE);
universe@3 66 return false;
universe@2 67 }
universe@2 68 }
universe@2 69
universe@2 70 private void check() {
universe@2 71 if (solver.check(field)) {
universe@2 72 JOptionPane.showMessageDialog(field, "Überprüfung erfolgreich!",
universe@2 73 "Sudoku", JOptionPane.INFORMATION_MESSAGE);
universe@2 74 } else {
universe@2 75 JOptionPane.showMessageDialog(field, "Das Feld enthält Fehler!",
universe@2 76 "Sudoku", JOptionPane.WARNING_MESSAGE);
universe@2 77 }
universe@2 78 }
universe@2 79
universe@2 80 private void solve() {
universe@2 81 // TODO: solve
universe@2 82 }
universe@2 83
universe@2 84 @Override
universe@2 85 public void actionPerformed(ActionEvent e) {
universe@2 86 switch (e.getActionCommand()) {
universe@3 87 case SAVE:
universe@2 88 save();
universe@2 89 break;
universe@3 90 case CHECK:
universe@2 91 check();
universe@2 92 break;
universe@3 93 case SOLVE:
universe@2 94 solve();
universe@2 95 break;
universe@3 96 case QUIT:
universe@3 97 if (field.isAnyCellModified()) {
universe@3 98 int result = JOptionPane.showConfirmDialog(field,
universe@3 99 "Das Feld ist ungespeichert - jetzt speichern?",
universe@3 100 "Sudoku", JOptionPane.YES_NO_CANCEL_OPTION);
universe@3 101 if (result == JOptionPane.YES_OPTION) {
universe@3 102 if (save()) {
universe@3 103 System.exit(0);
universe@3 104 }
universe@3 105 } else if (result == JOptionPane.NO_OPTION) {
universe@3 106 System.exit(0);
universe@3 107 }
universe@3 108 } else {
universe@3 109 System.exit(0);
universe@3 110 }
universe@3 111 break;
universe@3 112 case ABOUT:
universe@3 113 JOptionPane.showMessageDialog(field,
universe@3 114 "Sudoku - Copyright (c) 2013 Mike Becker\nwww.uap-core.de"+
universe@3 115 "\nPublished under the BSD License",
universe@3 116 "Sudoku", JOptionPane.INFORMATION_MESSAGE);
universe@3 117 break;
universe@2 118 default:
universe@2 119 throw new UnsupportedOperationException(
universe@2 120 "unknown action: "+e.getActionCommand());
universe@2 121 }
universe@2 122 }
universe@2 123
universe@2 124 }

mercurial