src/main/java/de/uapcore/sudoku/ActionHandler.java

changeset 9
576e7a2861ae
parent 7
2c0a2766461c
child 10
369903afbb29
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/de/uapcore/sudoku/ActionHandler.java	Sat Jul 25 14:01:28 2020 +0200
@@ -0,0 +1,203 @@
+/*
+ * 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 javax.swing.*;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.io.File;
+import java.io.IOException;
+
+/**
+ *
+ * @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() {
+        JFileChooser fc = new JFileChooser(".");
+        fc.setMultiSelectionEnabled(false);
+        if (fc.showSaveDialog(field) == JFileChooser.APPROVE_OPTION) {
+            File f = fc.getSelectedFile();
+            if (f.exists()) {
+                int result = JOptionPane.showConfirmDialog(field,
+                        "Bereits existierende Datei überschreiben?", "Sudoku",
+                        JOptionPane.YES_NO_OPTION);
+                if (result == JOptionPane.YES_OPTION) {
+                    doc.setFilename(f.getAbsolutePath());
+                    return true;
+                } else {
+                    return false;
+                }
+            } else {
+                doc.setFilename(f.getAbsolutePath());
+                return true;
+            }
+        } else {
+            return false;
+        }
+    }
+    
+    private void open() {
+        JFileChooser fc = new JFileChooser(".");
+        fc.setMultiSelectionEnabled(false);
+        if (fc.showOpenDialog(field) == JFileChooser.APPROVE_OPTION) {
+            File f = fc.getSelectedFile();
+            doc.setFilename(f.getAbsolutePath());
+            try {
+                doc.load(field);
+            } catch (IOException e) {
+                JOptionPane.showMessageDialog(field,
+                    "Datei konnte nicht geladen werden: "+e.getMessage(),
+                    "Sudoku", JOptionPane.ERROR_MESSAGE);
+            }
+        }
+    }
+    
+    private boolean save(boolean rename) {
+        if (!doc.isFilenameSet() || rename) {
+            if (!chooseSaveFilename()) {
+                return false;
+            }
+        }
+        if (solver.check(field)) {
+            field.setAllCellsModified(false);
+            try {
+                doc.save(field);
+            } catch (IOException e) {
+                JOptionPane.showMessageDialog(field,
+                    "Datei konnte nicht gespeichert werden: "+e.getMessage(),
+                    "Sudoku", JOptionPane.ERROR_MESSAGE);
+            }
+            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() {
+        if (!solver.check(field) || !solver.solve(field)) {
+            JOptionPane.showMessageDialog(field, "Das Feld ist nicht lösbar!",
+                    "Sudoku", JOptionPane.WARNING_MESSAGE);
+        }
+    }
+    
+    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(false)) {
+                    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()) {
+                doc.clearFilename();
+                field.clear();
+            }
+            break;
+        case OPEN:
+            open();
+            break;
+        case SAVE:
+            save(false);
+            break;
+        case SAVE_AS:
+            save(true);
+            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