Mon, 27 Jul 2020 12:08:23 +0200
fix: window cannot get smaller than the field
/* * 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 java.io.*; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * Implements load and save routines. */ public class DocumentHandler { private String filename; /** * Loads data into the specified field. * * @param field the field to populated with the loaded data * @throws IOException if loading fails or no file name has been set before * @see #setFilename(String) */ public void load(Field field) throws IOException { if (!isFilenameSet()) { throw new IOException("no filename supplied"); } int row = 0; try (BufferedReader in = new BufferedReader( new InputStreamReader(new FileInputStream(filename)))) { Pattern pat = Pattern.compile("^\\s*(?:[1-9_] ){8}[1-9_]\\s*$"); String line; while ((line = in.readLine()) != null) { if (line.matches("^\\s*$")) { continue; } Matcher m = pat.matcher(line); if (m.matches()) { String[] c = line.trim().split(" "); if (c.length != 9) { break; } for (int i = 0 ; i < 9 ; i++) { field.setCellValue(i, row, c[i].equals("_") ? 0 : Integer.parseInt(c[i])); } row++; } else { break; } } if (row != 9) { throw new IOException("Kein Sudoku-Feld enthalten!"); } } field.setAllCellsModified(false); } /** * Saves the specified field to a file. * * @param field the field to save * @throws IOException if saving fails or the file name has not been set before * @see #setFilename(String) */ public void save(Field field) throws IOException { if (!isFilenameSet()) { throw new IOException("no filename supplied"); } try (BufferedWriter out = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(filename)))) { for (int i = 0 ; i < 9 ; i++) { int[] row = field.getRow(i); for (int j = 0 ; j < 9 ; j++) { out.append(row[j] > 0 ? Character.forDigit(row[j], 10):'_'); out.append(j == 8 ? '\n': ' '); } } } } /** * Sets the file name for loading and saving data. * * @param filename the file name */ public void setFilename(String filename) { this.filename = filename; } /** * Clears the file name. */ public void clearFilename() { filename = null; } /** * Checks whether a file name has been set. * * @return true if a file name is known, false otherwise */ public boolean isFilenameSet() { return filename != null; } }