src/de/uapcore/sudoku/ActionHandler.java

Sat, 26 Jan 2013 19:34:31 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 26 Jan 2013 19:34:31 +0100
changeset 5
8ddf4af937d7
parent 4
b8588e318001
child 6
5bab2e971333
permissions
-rw-r--r--

moved field methods to field class + added (parts of the) document handler

     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;
    51     private DocumentHandler doc;
    53     public ActionHandler(Field f) {
    54         field = f;
    55         solver = new Solver();
    56         doc = new DocumentHandler();
    57     }
    59     private boolean chooseSaveFilename() {
    60         // TODO: fileselector
    61         return false;
    62     }
    64     private boolean save() {
    65         if (!doc.isFilenameSet()) {
    66             if (!chooseSaveFilename()) {
    67                 return false;
    68             }
    69         }
    70         if (solver.check(field)) {
    71             field.setAllCellsModified(false);
    72             // TODO: save to file
    73             return true;
    74         } else {
    75             JOptionPane.showMessageDialog(field,
    76                     "Das Feld kann mit Fehlern nicht gespeichert werden!",
    77                     "Sudoku", JOptionPane.ERROR_MESSAGE);
    78             return false;
    79         }
    80     }
    82     private void check() {
    83         if (solver.check(field)) {
    84             JOptionPane.showMessageDialog(field, "Überprüfung erfolgreich!",
    85                     "Sudoku", JOptionPane.INFORMATION_MESSAGE);
    86         } else {
    87             JOptionPane.showMessageDialog(field, "Das Feld enthält Fehler!",
    88                     "Sudoku", JOptionPane.WARNING_MESSAGE);
    89         }
    90     }
    92     private void solve() {
    93         // TODO: solve
    94     }
    96     private boolean saveUnsaved() {
    97         boolean proceed = false;
    98         if (field.isAnyCellModified()) {
    99             int result = JOptionPane.showConfirmDialog(field,
   100                     "Das Feld ist ungespeichert - jetzt speichern?",
   101                     "Sudoku", JOptionPane.YES_NO_CANCEL_OPTION);
   102             if (result == JOptionPane.YES_OPTION) {
   103                 if (save()) {
   104                     proceed = true;
   105                 }
   106             } else if (result == JOptionPane.NO_OPTION) {
   107                 proceed = true;
   108             }
   109         } else {
   110             proceed = true;
   111         }
   113         return proceed;
   114     }
   116     @Override
   117     public void actionPerformed(ActionEvent e) {
   118         switch (e.getActionCommand()) {
   119         case NEW:
   120             if (saveUnsaved()) {
   121                 field.clear();
   122             }
   123             break;
   124         case SAVE:
   125             save();
   126             break;
   127         case CHECK:
   128             check();
   129             break;
   130         case SOLVE:
   131             solve();
   132             break;
   133         case QUIT:
   134             if (saveUnsaved()) {
   135                 System.exit(0);
   136             }
   137             break;
   138         case ABOUT:
   139             JOptionPane.showMessageDialog(field,
   140                     "Sudoku - Copyright (c) 2013 Mike Becker\nwww.uap-core.de"+
   141                     "\nPublished under the BSD License",
   142                     "Sudoku", JOptionPane.INFORMATION_MESSAGE);
   143             break;
   144         default:
   145             throw new UnsupportedOperationException(
   146                     "unknown action: "+e.getActionCommand());
   147         }
   148     }
   150 }

mercurial