universe@3: /* universe@3: * Copyright 2013 Mike Becker. All rights reserved. universe@3: * universe@3: * Redistribution and use in source and binary forms, with or without universe@3: * modification, are permitted provided that the following conditions are met: universe@3: * universe@3: * 1. Redistributions of source code must retain the above copyright universe@3: * notice, this list of conditions and the following disclaimer. universe@3: * universe@3: * 2. Redistributions in binary form must reproduce the above copyright universe@3: * notice, this list of conditions and the following disclaimer in the universe@3: * documentation and/or other materials provided with the distribution. universe@3: * universe@3: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@3: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@3: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@3: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@3: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@3: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@3: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@3: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@3: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@3: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@3: * POSSIBILITY OF SUCH DAMAGE. universe@3: */ universe@3: universe@2: package de.uapcore.sudoku; universe@2: universe@2: import java.awt.event.ActionEvent; universe@2: import java.awt.event.ActionListener; universe@2: import javax.swing.JOptionPane; universe@2: universe@2: /** universe@2: * universe@2: * @author mike universe@2: */ universe@2: public final class ActionHandler implements ActionListener { universe@2: universe@2: public static final String SAVE = "save"; universe@2: public static final String CHECK = "check"; universe@2: public static final String SOLVE = "solve"; universe@2: universe@3: public static final String NEW = "new"; universe@3: public static final String OPEN = "open"; universe@3: public static final String SAVE_AS = "save as"; universe@3: public static final String QUIT = "quit"; universe@3: public static final String ABOUT = "about"; universe@3: universe@2: private Field field; universe@2: private Solver solver; universe@2: universe@2: public ActionHandler(Field f) { universe@2: field = f; universe@2: solver = new Solver(); universe@2: } universe@2: universe@3: private boolean save() { universe@2: if (solver.check(field)) { universe@2: field.setAllCellsModified(false); universe@2: // TODO: save to file universe@3: return true; universe@2: } else { universe@2: JOptionPane.showMessageDialog(field, universe@2: "Das Feld kann mit Fehlern nicht gespeichert werden!", universe@2: "Sudoku", JOptionPane.ERROR_MESSAGE); universe@3: return false; universe@2: } universe@2: } universe@2: universe@2: private void check() { universe@2: if (solver.check(field)) { universe@2: JOptionPane.showMessageDialog(field, "Überprüfung erfolgreich!", universe@2: "Sudoku", JOptionPane.INFORMATION_MESSAGE); universe@2: } else { universe@2: JOptionPane.showMessageDialog(field, "Das Feld enthält Fehler!", universe@2: "Sudoku", JOptionPane.WARNING_MESSAGE); universe@2: } universe@2: } universe@2: universe@2: private void solve() { universe@2: // TODO: solve universe@2: } universe@4: universe@4: private boolean saveUnsaved() { universe@4: boolean proceed = false; universe@4: if (field.isAnyCellModified()) { universe@4: int result = JOptionPane.showConfirmDialog(field, universe@4: "Das Feld ist ungespeichert - jetzt speichern?", universe@4: "Sudoku", JOptionPane.YES_NO_CANCEL_OPTION); universe@4: if (result == JOptionPane.YES_OPTION) { universe@4: if (save()) { universe@4: proceed = true; universe@4: } universe@4: } else if (result == JOptionPane.NO_OPTION) { universe@4: proceed = true; universe@4: } universe@4: } else { universe@4: proceed = true; universe@4: } universe@4: universe@4: return proceed; universe@4: } universe@2: universe@2: @Override universe@2: public void actionPerformed(ActionEvent e) { universe@2: switch (e.getActionCommand()) { universe@4: case NEW: universe@4: if (saveUnsaved()) { universe@4: field.clear(); universe@4: } universe@4: break; universe@3: case SAVE: universe@2: save(); universe@2: break; universe@3: case CHECK: universe@2: check(); universe@2: break; universe@3: case SOLVE: universe@2: solve(); universe@2: break; universe@3: case QUIT: universe@4: if (saveUnsaved()) { universe@3: System.exit(0); universe@3: } universe@3: break; universe@3: case ABOUT: universe@3: JOptionPane.showMessageDialog(field, universe@3: "Sudoku - Copyright (c) 2013 Mike Becker\nwww.uap-core.de"+ universe@3: "\nPublished under the BSD License", universe@3: "Sudoku", JOptionPane.INFORMATION_MESSAGE); universe@3: break; universe@2: default: universe@2: throw new UnsupportedOperationException( universe@2: "unknown action: "+e.getActionCommand()); universe@2: } universe@2: } universe@2: universe@2: }