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