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

changeset 10
369903afbb29
parent 9
576e7a2861ae
child 12
1c62c6009161
     1.1 --- a/src/main/java/de/uapcore/sudoku/ActionHandler.java	Sat Jul 25 14:01:28 2020 +0200
     1.2 +++ b/src/main/java/de/uapcore/sudoku/ActionHandler.java	Sat Jul 25 15:29:51 2020 +0200
     1.3 @@ -1,16 +1,16 @@
     1.4  /*
     1.5   * Copyright 2013 Mike Becker. All rights reserved.
     1.6 - * 
     1.7 + *
     1.8   * Redistribution and use in source and binary forms, with or without
     1.9   * modification, are permitted provided that the following conditions are met:
    1.10 - * 
    1.11 + *
    1.12   * 1. Redistributions of source code must retain the above copyright
    1.13   *    notice, this list of conditions and the following disclaimer.
    1.14 - * 
    1.15 + *
    1.16   * 2. Redistributions in binary form must reproduce the above copyright
    1.17   *    notice, this list of conditions and the following disclaimer in the
    1.18   *    documentation and/or other materials provided with the distribution.
    1.19 - * 
    1.20 + *
    1.21   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    1.22   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.23   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.24 @@ -33,31 +33,42 @@
    1.25  import java.io.IOException;
    1.26  
    1.27  /**
    1.28 - *
    1.29 - * @author mike
    1.30 + * Handles all user issued actions in the application.
    1.31   */
    1.32  public final class ActionHandler implements ActionListener {
    1.33 -    
    1.34 +
    1.35      public static final String SAVE = "save";
    1.36      public static final String CHECK = "check";
    1.37      public static final String SOLVE = "solve";
    1.38 -    
    1.39 +
    1.40      public static final String NEW = "new";
    1.41      public static final String OPEN = "open";
    1.42      public static final String SAVE_AS = "save as";
    1.43      public static final String QUIT = "quit";
    1.44      public static final String ABOUT = "about";
    1.45 -    
    1.46 +
    1.47      private Field field;
    1.48      private Solver solver;
    1.49      private DocumentHandler doc;
    1.50 -    
    1.51 +
    1.52 +    /**
    1.53 +     * Constructs a new action handler instance.
    1.54 +     *
    1.55 +     * @param f a reference to the playing field
    1.56 +     */
    1.57      public ActionHandler(Field f) {
    1.58          field = f;
    1.59          solver = new Solver();
    1.60          doc = new DocumentHandler();
    1.61      }
    1.62 -    
    1.63 +
    1.64 +    /**
    1.65 +     * Prompts the user for a file name.
    1.66 +     * <p>
    1.67 +     * If the user chooses an existing file, they are asked whether the file should be overwritten.
    1.68 +     *
    1.69 +     * @return true if the user approves a chosen file name
    1.70 +     */
    1.71      private boolean chooseSaveFilename() {
    1.72          JFileChooser fc = new JFileChooser(".");
    1.73          fc.setMultiSelectionEnabled(false);
    1.74 @@ -81,7 +92,10 @@
    1.75              return false;
    1.76          }
    1.77      }
    1.78 -    
    1.79 +
    1.80 +    /**
    1.81 +     * Prompts the user for a file to open and, if approved, loads that file.
    1.82 +     */
    1.83      private void open() {
    1.84          JFileChooser fc = new JFileChooser(".");
    1.85          fc.setMultiSelectionEnabled(false);
    1.86 @@ -92,12 +106,22 @@
    1.87                  doc.load(field);
    1.88              } catch (IOException e) {
    1.89                  JOptionPane.showMessageDialog(field,
    1.90 -                    "Datei konnte nicht geladen werden: "+e.getMessage(),
    1.91 -                    "Sudoku", JOptionPane.ERROR_MESSAGE);
    1.92 +                        "Datei konnte nicht geladen werden: " + e.getMessage(),
    1.93 +                        "Sudoku", JOptionPane.ERROR_MESSAGE);
    1.94              }
    1.95          }
    1.96      }
    1.97 -    
    1.98 +
    1.99 +    /**
   1.100 +     * Attempts to save the Sudoku field to a file.
   1.101 +     * <p>
   1.102 +     * If necessary, the user is prompted for a file name.
   1.103 +     * <p>
   1.104 +     * The field must be solvable, otherwise it cannot be saved.
   1.105 +     *
   1.106 +     * @param rename true if the user shall always be prompted, even if a file name is already known
   1.107 +     * @return true if the user approves the chosen file name
   1.108 +     */
   1.109      private boolean save(boolean rename) {
   1.110          if (!doc.isFilenameSet() || rename) {
   1.111              if (!chooseSaveFilename()) {
   1.112 @@ -110,8 +134,8 @@
   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 +                        "Datei konnte nicht gespeichert werden: " + e.getMessage(),
   1.119 +                        "Sudoku", JOptionPane.ERROR_MESSAGE);
   1.120              }
   1.121              return true;
   1.122          } else {
   1.123 @@ -121,7 +145,10 @@
   1.124              return false;
   1.125          }
   1.126      }
   1.127 -    
   1.128 +
   1.129 +    /**
   1.130 +     * Checks the Sudoku field and displays the result as a dialog box.
   1.131 +     */
   1.132      private void check() {
   1.133          if (solver.check(field)) {
   1.134              JOptionPane.showMessageDialog(field, "Überprüfung erfolgreich!",
   1.135 @@ -131,14 +158,22 @@
   1.136                      "Sudoku", JOptionPane.WARNING_MESSAGE);
   1.137          }
   1.138      }
   1.139 -    
   1.140 +
   1.141 +    /**
   1.142 +     * Solves the field or displays an error dialog if the field is not solvable.
   1.143 +     */
   1.144      private void solve() {
   1.145          if (!solver.check(field) || !solver.solve(field)) {
   1.146              JOptionPane.showMessageDialog(field, "Das Feld ist nicht lösbar!",
   1.147                      "Sudoku", JOptionPane.WARNING_MESSAGE);
   1.148          }
   1.149      }
   1.150 -    
   1.151 +
   1.152 +    /**
   1.153 +     * Checks whether there are unsaved changes and asks the user to save the field.
   1.154 +     *
   1.155 +     * @return true if there are no unsaved changes or the user actively decides to continue - false, otherwise
   1.156 +     */
   1.157      private boolean saveUnsaved() {
   1.158          boolean proceed = false;
   1.159          if (field.isAnyCellModified()) {
   1.160 @@ -155,49 +190,49 @@
   1.161          } else {
   1.162              proceed = true;
   1.163          }
   1.164 -        
   1.165 +
   1.166          return proceed;
   1.167      }
   1.168  
   1.169      @Override
   1.170      public void actionPerformed(ActionEvent e) {
   1.171          switch (e.getActionCommand()) {
   1.172 -        case NEW:
   1.173 -            if (saveUnsaved()) {
   1.174 -                doc.clearFilename();
   1.175 -                field.clear();
   1.176 -            }
   1.177 -            break;
   1.178 -        case OPEN:
   1.179 -            open();
   1.180 -            break;
   1.181 -        case SAVE:
   1.182 -            save(false);
   1.183 -            break;
   1.184 -        case SAVE_AS:
   1.185 -            save(true);
   1.186 -            break;
   1.187 -        case CHECK:
   1.188 -            check();
   1.189 -            break;
   1.190 -        case SOLVE:
   1.191 -            solve();
   1.192 -            break;
   1.193 -        case QUIT:
   1.194 -            if (saveUnsaved()) {
   1.195 -                System.exit(0);
   1.196 -            }
   1.197 -            break;
   1.198 -        case ABOUT:
   1.199 -            JOptionPane.showMessageDialog(field,
   1.200 -                    "Sudoku - Copyright (c) 2013 Mike Becker\nwww.uap-core.de"+
   1.201 -                    "\nPublished under the BSD License",
   1.202 -                    "Sudoku", JOptionPane.INFORMATION_MESSAGE);
   1.203 -            break;
   1.204 -        default:
   1.205 -            throw new UnsupportedOperationException(
   1.206 -                    "unknown action: "+e.getActionCommand());
   1.207 +            case NEW:
   1.208 +                if (saveUnsaved()) {
   1.209 +                    doc.clearFilename();
   1.210 +                    field.clear();
   1.211 +                }
   1.212 +                break;
   1.213 +            case OPEN:
   1.214 +                open();
   1.215 +                break;
   1.216 +            case SAVE:
   1.217 +                save(false);
   1.218 +                break;
   1.219 +            case SAVE_AS:
   1.220 +                save(true);
   1.221 +                break;
   1.222 +            case CHECK:
   1.223 +                check();
   1.224 +                break;
   1.225 +            case SOLVE:
   1.226 +                solve();
   1.227 +                break;
   1.228 +            case QUIT:
   1.229 +                if (saveUnsaved()) {
   1.230 +                    System.exit(0);
   1.231 +                }
   1.232 +                break;
   1.233 +            case ABOUT:
   1.234 +                JOptionPane.showMessageDialog(field,
   1.235 +                        "Sudoku - Copyright (c) 2013 Mike Becker\nwww.uap-core.de" +
   1.236 +                                "\nPublished under the BSD License",
   1.237 +                        "Sudoku", JOptionPane.INFORMATION_MESSAGE);
   1.238 +                break;
   1.239 +            default:
   1.240 +                throw new UnsupportedOperationException(
   1.241 +                        "unknown action: " + e.getActionCommand());
   1.242          }
   1.243      }
   1.244 -    
   1.245 +
   1.246  }

mercurial