src/de/uapcore/sudoku/ActionHandler.java

Sat, 26 Jan 2013 18:43:49 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 26 Jan 2013 18:43:49 +0100
changeset 4
b8588e318001
parent 3
ed931970b4ac
child 5
8ddf4af937d7
permissions
-rw-r--r--

added NEW function

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@4 83
universe@4 84 private boolean saveUnsaved() {
universe@4 85 boolean proceed = false;
universe@4 86 if (field.isAnyCellModified()) {
universe@4 87 int result = JOptionPane.showConfirmDialog(field,
universe@4 88 "Das Feld ist ungespeichert - jetzt speichern?",
universe@4 89 "Sudoku", JOptionPane.YES_NO_CANCEL_OPTION);
universe@4 90 if (result == JOptionPane.YES_OPTION) {
universe@4 91 if (save()) {
universe@4 92 proceed = true;
universe@4 93 }
universe@4 94 } else if (result == JOptionPane.NO_OPTION) {
universe@4 95 proceed = true;
universe@4 96 }
universe@4 97 } else {
universe@4 98 proceed = true;
universe@4 99 }
universe@4 100
universe@4 101 return proceed;
universe@4 102 }
universe@2 103
universe@2 104 @Override
universe@2 105 public void actionPerformed(ActionEvent e) {
universe@2 106 switch (e.getActionCommand()) {
universe@4 107 case NEW:
universe@4 108 if (saveUnsaved()) {
universe@4 109 field.clear();
universe@4 110 }
universe@4 111 break;
universe@3 112 case SAVE:
universe@2 113 save();
universe@2 114 break;
universe@3 115 case CHECK:
universe@2 116 check();
universe@2 117 break;
universe@3 118 case SOLVE:
universe@2 119 solve();
universe@2 120 break;
universe@3 121 case QUIT:
universe@4 122 if (saveUnsaved()) {
universe@3 123 System.exit(0);
universe@3 124 }
universe@3 125 break;
universe@3 126 case ABOUT:
universe@3 127 JOptionPane.showMessageDialog(field,
universe@3 128 "Sudoku - Copyright (c) 2013 Mike Becker\nwww.uap-core.de"+
universe@3 129 "\nPublished under the BSD License",
universe@3 130 "Sudoku", JOptionPane.INFORMATION_MESSAGE);
universe@3 131 break;
universe@2 132 default:
universe@2 133 throw new UnsupportedOperationException(
universe@2 134 "unknown action: "+e.getActionCommand());
universe@2 135 }
universe@2 136 }
universe@2 137
universe@2 138 }

mercurial