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

     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     @Override
    85     public void actionPerformed(ActionEvent e) {
    86         switch (e.getActionCommand()) {
    87         case SAVE:
    88             save();
    89             break;
    90         case CHECK:
    91             check();
    92             break;
    93         case 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);
   117             break;
   118         default:
   119             throw new UnsupportedOperationException(
   120                     "unknown action: "+e.getActionCommand());
   121         }
   122     }
   124 }

mercurial