1.1 --- a/src/de/uapcore/sudoku/ActionHandler.java Fri Feb 01 10:18:47 2013 +0100 1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 @@ -1,204 +0,0 @@ 1.4 -/* 1.5 - * Copyright 2013 Mike Becker. All rights reserved. 1.6 - * 1.7 - * Redistribution and use in source and binary forms, with or without 1.8 - * modification, are permitted provided that the following conditions are met: 1.9 - * 1.10 - * 1. Redistributions of source code must retain the above copyright 1.11 - * notice, this list of conditions and the following disclaimer. 1.12 - * 1.13 - * 2. Redistributions in binary form must reproduce the above copyright 1.14 - * notice, this list of conditions and the following disclaimer in the 1.15 - * documentation and/or other materials provided with the distribution. 1.16 - * 1.17 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 1.18 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1.19 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1.20 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 1.21 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 1.22 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 1.23 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 1.24 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 1.25 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 1.26 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 1.27 - * POSSIBILITY OF SUCH DAMAGE. 1.28 - */ 1.29 - 1.30 -package de.uapcore.sudoku; 1.31 - 1.32 -import java.awt.event.ActionEvent; 1.33 -import java.awt.event.ActionListener; 1.34 -import java.io.File; 1.35 -import java.io.IOException; 1.36 -import javax.swing.JFileChooser; 1.37 -import javax.swing.JOptionPane; 1.38 - 1.39 -/** 1.40 - * 1.41 - * @author mike 1.42 - */ 1.43 -public final class ActionHandler implements ActionListener { 1.44 - 1.45 - public static final String SAVE = "save"; 1.46 - public static final String CHECK = "check"; 1.47 - public static final String SOLVE = "solve"; 1.48 - 1.49 - public static final String NEW = "new"; 1.50 - public static final String OPEN = "open"; 1.51 - public static final String SAVE_AS = "save as"; 1.52 - public static final String QUIT = "quit"; 1.53 - public static final String ABOUT = "about"; 1.54 - 1.55 - private Field field; 1.56 - private Solver solver; 1.57 - private DocumentHandler doc; 1.58 - 1.59 - public ActionHandler(Field f) { 1.60 - field = f; 1.61 - solver = new Solver(); 1.62 - doc = new DocumentHandler(); 1.63 - } 1.64 - 1.65 - private boolean chooseSaveFilename() { 1.66 - JFileChooser fc = new JFileChooser("."); 1.67 - fc.setMultiSelectionEnabled(false); 1.68 - if (fc.showSaveDialog(field) == JFileChooser.APPROVE_OPTION) { 1.69 - File f = fc.getSelectedFile(); 1.70 - if (f.exists()) { 1.71 - int result = JOptionPane.showConfirmDialog(field, 1.72 - "Bereits existierende Datei überschreiben?", "Sudoku", 1.73 - JOptionPane.YES_NO_OPTION); 1.74 - if (result == JOptionPane.YES_OPTION) { 1.75 - doc.setFilename(f.getAbsolutePath()); 1.76 - return true; 1.77 - } else { 1.78 - return false; 1.79 - } 1.80 - } else { 1.81 - doc.setFilename(f.getAbsolutePath()); 1.82 - return true; 1.83 - } 1.84 - } else { 1.85 - return false; 1.86 - } 1.87 - } 1.88 - 1.89 - private void open() { 1.90 - JFileChooser fc = new JFileChooser("."); 1.91 - fc.setMultiSelectionEnabled(false); 1.92 - if (fc.showOpenDialog(field) == JFileChooser.APPROVE_OPTION) { 1.93 - File f = fc.getSelectedFile(); 1.94 - doc.setFilename(f.getAbsolutePath()); 1.95 - try { 1.96 - doc.load(field); 1.97 - } catch (IOException e) { 1.98 - JOptionPane.showMessageDialog(field, 1.99 - "Datei konnte nicht geladen werden: "+e.getMessage(), 1.100 - "Sudoku", JOptionPane.ERROR_MESSAGE); 1.101 - } 1.102 - } 1.103 - } 1.104 - 1.105 - private boolean save(boolean rename) { 1.106 - if (!doc.isFilenameSet() || rename) { 1.107 - if (!chooseSaveFilename()) { 1.108 - return false; 1.109 - } 1.110 - } 1.111 - if (solver.check(field)) { 1.112 - field.setAllCellsModified(false); 1.113 - try { 1.114 - doc.save(field); 1.115 - } catch (IOException e) { 1.116 - JOptionPane.showMessageDialog(field, 1.117 - "Datei konnte nicht gespeichert werden: "+e.getMessage(), 1.118 - "Sudoku", JOptionPane.ERROR_MESSAGE); 1.119 - } 1.120 - return true; 1.121 - } else { 1.122 - JOptionPane.showMessageDialog(field, 1.123 - "Das Feld kann mit Fehlern nicht gespeichert werden!", 1.124 - "Sudoku", JOptionPane.ERROR_MESSAGE); 1.125 - return false; 1.126 - } 1.127 - } 1.128 - 1.129 - private void check() { 1.130 - if (solver.check(field)) { 1.131 - JOptionPane.showMessageDialog(field, "Überprüfung erfolgreich!", 1.132 - "Sudoku", JOptionPane.INFORMATION_MESSAGE); 1.133 - } else { 1.134 - JOptionPane.showMessageDialog(field, "Das Feld enthält Fehler!", 1.135 - "Sudoku", JOptionPane.WARNING_MESSAGE); 1.136 - } 1.137 - } 1.138 - 1.139 - private void solve() { 1.140 - if (!solver.check(field) || !solver.solve(field)) { 1.141 - JOptionPane.showMessageDialog(field, "Das Feld ist nicht lösbar!", 1.142 - "Sudoku", JOptionPane.WARNING_MESSAGE); 1.143 - } 1.144 - } 1.145 - 1.146 - private boolean saveUnsaved() { 1.147 - boolean proceed = false; 1.148 - if (field.isAnyCellModified()) { 1.149 - int result = JOptionPane.showConfirmDialog(field, 1.150 - "Das Feld ist ungespeichert - jetzt speichern?", 1.151 - "Sudoku", JOptionPane.YES_NO_CANCEL_OPTION); 1.152 - if (result == JOptionPane.YES_OPTION) { 1.153 - if (save(false)) { 1.154 - proceed = true; 1.155 - } 1.156 - } else if (result == JOptionPane.NO_OPTION) { 1.157 - proceed = true; 1.158 - } 1.159 - } else { 1.160 - proceed = true; 1.161 - } 1.162 - 1.163 - return proceed; 1.164 - } 1.165 - 1.166 - @Override 1.167 - public void actionPerformed(ActionEvent e) { 1.168 - switch (e.getActionCommand()) { 1.169 - case NEW: 1.170 - if (saveUnsaved()) { 1.171 - doc.clearFilename(); 1.172 - field.clear(); 1.173 - } 1.174 - break; 1.175 - case OPEN: 1.176 - open(); 1.177 - break; 1.178 - case SAVE: 1.179 - save(false); 1.180 - break; 1.181 - case SAVE_AS: 1.182 - save(true); 1.183 - break; 1.184 - case CHECK: 1.185 - check(); 1.186 - break; 1.187 - case SOLVE: 1.188 - solve(); 1.189 - break; 1.190 - case QUIT: 1.191 - if (saveUnsaved()) { 1.192 - System.exit(0); 1.193 - } 1.194 - break; 1.195 - case ABOUT: 1.196 - JOptionPane.showMessageDialog(field, 1.197 - "Sudoku - Copyright (c) 2013 Mike Becker\nwww.uap-core.de"+ 1.198 - "\nPublished under the BSD License", 1.199 - "Sudoku", JOptionPane.INFORMATION_MESSAGE); 1.200 - break; 1.201 - default: 1.202 - throw new UnsupportedOperationException( 1.203 - "unknown action: "+e.getActionCommand()); 1.204 - } 1.205 - } 1.206 - 1.207 -}