1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/main/java/de/uapcore/sudoku/ActionHandler.java Sat Jul 25 14:01:28 2020 +0200 1.3 @@ -0,0 +1,203 @@ 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 javax.swing.*; 1.33 +import java.awt.event.ActionEvent; 1.34 +import java.awt.event.ActionListener; 1.35 +import java.io.File; 1.36 +import java.io.IOException; 1.37 + 1.38 +/** 1.39 + * 1.40 + * @author mike 1.41 + */ 1.42 +public final class ActionHandler implements ActionListener { 1.43 + 1.44 + public static final String SAVE = "save"; 1.45 + public static final String CHECK = "check"; 1.46 + public static final String SOLVE = "solve"; 1.47 + 1.48 + public static final String NEW = "new"; 1.49 + public static final String OPEN = "open"; 1.50 + public static final String SAVE_AS = "save as"; 1.51 + public static final String QUIT = "quit"; 1.52 + public static final String ABOUT = "about"; 1.53 + 1.54 + private Field field; 1.55 + private Solver solver; 1.56 + private DocumentHandler doc; 1.57 + 1.58 + public ActionHandler(Field f) { 1.59 + field = f; 1.60 + solver = new Solver(); 1.61 + doc = new DocumentHandler(); 1.62 + } 1.63 + 1.64 + private boolean chooseSaveFilename() { 1.65 + JFileChooser fc = new JFileChooser("."); 1.66 + fc.setMultiSelectionEnabled(false); 1.67 + if (fc.showSaveDialog(field) == JFileChooser.APPROVE_OPTION) { 1.68 + File f = fc.getSelectedFile(); 1.69 + if (f.exists()) { 1.70 + int result = JOptionPane.showConfirmDialog(field, 1.71 + "Bereits existierende Datei überschreiben?", "Sudoku", 1.72 + JOptionPane.YES_NO_OPTION); 1.73 + if (result == JOptionPane.YES_OPTION) { 1.74 + doc.setFilename(f.getAbsolutePath()); 1.75 + return true; 1.76 + } else { 1.77 + return false; 1.78 + } 1.79 + } else { 1.80 + doc.setFilename(f.getAbsolutePath()); 1.81 + return true; 1.82 + } 1.83 + } else { 1.84 + return false; 1.85 + } 1.86 + } 1.87 + 1.88 + private void open() { 1.89 + JFileChooser fc = new JFileChooser("."); 1.90 + fc.setMultiSelectionEnabled(false); 1.91 + if (fc.showOpenDialog(field) == JFileChooser.APPROVE_OPTION) { 1.92 + File f = fc.getSelectedFile(); 1.93 + doc.setFilename(f.getAbsolutePath()); 1.94 + try { 1.95 + doc.load(field); 1.96 + } catch (IOException e) { 1.97 + JOptionPane.showMessageDialog(field, 1.98 + "Datei konnte nicht geladen werden: "+e.getMessage(), 1.99 + "Sudoku", JOptionPane.ERROR_MESSAGE); 1.100 + } 1.101 + } 1.102 + } 1.103 + 1.104 + private boolean save(boolean rename) { 1.105 + if (!doc.isFilenameSet() || rename) { 1.106 + if (!chooseSaveFilename()) { 1.107 + return false; 1.108 + } 1.109 + } 1.110 + if (solver.check(field)) { 1.111 + field.setAllCellsModified(false); 1.112 + try { 1.113 + doc.save(field); 1.114 + } catch (IOException e) { 1.115 + JOptionPane.showMessageDialog(field, 1.116 + "Datei konnte nicht gespeichert werden: "+e.getMessage(), 1.117 + "Sudoku", JOptionPane.ERROR_MESSAGE); 1.118 + } 1.119 + return true; 1.120 + } else { 1.121 + JOptionPane.showMessageDialog(field, 1.122 + "Das Feld kann mit Fehlern nicht gespeichert werden!", 1.123 + "Sudoku", JOptionPane.ERROR_MESSAGE); 1.124 + return false; 1.125 + } 1.126 + } 1.127 + 1.128 + private void check() { 1.129 + if (solver.check(field)) { 1.130 + JOptionPane.showMessageDialog(field, "Überprüfung erfolgreich!", 1.131 + "Sudoku", JOptionPane.INFORMATION_MESSAGE); 1.132 + } else { 1.133 + JOptionPane.showMessageDialog(field, "Das Feld enthält Fehler!", 1.134 + "Sudoku", JOptionPane.WARNING_MESSAGE); 1.135 + } 1.136 + } 1.137 + 1.138 + private void solve() { 1.139 + if (!solver.check(field) || !solver.solve(field)) { 1.140 + JOptionPane.showMessageDialog(field, "Das Feld ist nicht lösbar!", 1.141 + "Sudoku", JOptionPane.WARNING_MESSAGE); 1.142 + } 1.143 + } 1.144 + 1.145 + private boolean saveUnsaved() { 1.146 + boolean proceed = false; 1.147 + if (field.isAnyCellModified()) { 1.148 + int result = JOptionPane.showConfirmDialog(field, 1.149 + "Das Feld ist ungespeichert - jetzt speichern?", 1.150 + "Sudoku", JOptionPane.YES_NO_CANCEL_OPTION); 1.151 + if (result == JOptionPane.YES_OPTION) { 1.152 + if (save(false)) { 1.153 + proceed = true; 1.154 + } 1.155 + } else if (result == JOptionPane.NO_OPTION) { 1.156 + proceed = true; 1.157 + } 1.158 + } else { 1.159 + proceed = true; 1.160 + } 1.161 + 1.162 + return proceed; 1.163 + } 1.164 + 1.165 + @Override 1.166 + public void actionPerformed(ActionEvent e) { 1.167 + switch (e.getActionCommand()) { 1.168 + case NEW: 1.169 + if (saveUnsaved()) { 1.170 + doc.clearFilename(); 1.171 + field.clear(); 1.172 + } 1.173 + break; 1.174 + case OPEN: 1.175 + open(); 1.176 + break; 1.177 + case SAVE: 1.178 + save(false); 1.179 + break; 1.180 + case SAVE_AS: 1.181 + save(true); 1.182 + break; 1.183 + case CHECK: 1.184 + check(); 1.185 + break; 1.186 + case SOLVE: 1.187 + solve(); 1.188 + break; 1.189 + case QUIT: 1.190 + if (saveUnsaved()) { 1.191 + System.exit(0); 1.192 + } 1.193 + break; 1.194 + case ABOUT: 1.195 + JOptionPane.showMessageDialog(field, 1.196 + "Sudoku - Copyright (c) 2013 Mike Becker\nwww.uap-core.de"+ 1.197 + "\nPublished under the BSD License", 1.198 + "Sudoku", JOptionPane.INFORMATION_MESSAGE); 1.199 + break; 1.200 + default: 1.201 + throw new UnsupportedOperationException( 1.202 + "unknown action: "+e.getActionCommand()); 1.203 + } 1.204 + } 1.205 + 1.206 +}