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

/*
 * Copyright 2013 Mike Becker. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package de.uapcore.sudoku;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;

/**
 *
 * @author mike
 */
public final class ActionHandler implements ActionListener {
    
    public static final String SAVE = "save";
    public static final String CHECK = "check";
    public static final String SOLVE = "solve";
    
    public static final String NEW = "new";
    public static final String OPEN = "open";
    public static final String SAVE_AS = "save as";
    public static final String QUIT = "quit";
    public static final String ABOUT = "about";
    
    private Field field;
    private Solver solver;
    private DocumentHandler doc;
    
    public ActionHandler(Field f) {
        field = f;
        solver = new Solver();
        doc = new DocumentHandler();
    }
    
    private boolean chooseSaveFilename() {
        // TODO: fileselector
        return false;
    }
    
    private boolean save() {
        if (!doc.isFilenameSet()) {
            if (!chooseSaveFilename()) {
                return false;
            }
        }
        if (solver.check(field)) {
            field.setAllCellsModified(false);
            // TODO: save to file
            return true;
        } else {
            JOptionPane.showMessageDialog(field,
                    "Das Feld kann mit Fehlern nicht gespeichert werden!",
                    "Sudoku", JOptionPane.ERROR_MESSAGE);
            return false;
        }
    }
    
    private void check() {
        if (solver.check(field)) {
            JOptionPane.showMessageDialog(field, "Überprüfung erfolgreich!",
                    "Sudoku", JOptionPane.INFORMATION_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(field, "Das Feld enthält Fehler!",
                    "Sudoku", JOptionPane.WARNING_MESSAGE);
        }
    }
    
    private void solve() {
        // TODO: solve
    }
    
    private boolean saveUnsaved() {
        boolean proceed = false;
        if (field.isAnyCellModified()) {
            int result = JOptionPane.showConfirmDialog(field,
                    "Das Feld ist ungespeichert - jetzt speichern?",
                    "Sudoku", JOptionPane.YES_NO_CANCEL_OPTION);
            if (result == JOptionPane.YES_OPTION) {
                if (save()) {
                    proceed = true;
                }
            } else if (result == JOptionPane.NO_OPTION) {
                proceed = true;
            }
        } else {
            proceed = true;
        }
        
        return proceed;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        switch (e.getActionCommand()) {
        case NEW:
            if (saveUnsaved()) {
                field.clear();
            }
            break;
        case SAVE:
            save();
            break;
        case CHECK:
            check();
            break;
        case SOLVE:
            solve();
            break;
        case QUIT:
            if (saveUnsaved()) {
                System.exit(0);
            }
            break;
        case ABOUT:
            JOptionPane.showMessageDialog(field,
                    "Sudoku - Copyright (c) 2013 Mike Becker\nwww.uap-core.de"+
                    "\nPublished under the BSD License",
                    "Sudoku", JOptionPane.INFORMATION_MESSAGE);
            break;
        default:
            throw new UnsupportedOperationException(
                    "unknown action: "+e.getActionCommand());
        }
    }
    
}

mercurial