Sun, 27 Jan 2013 15:03:57 +0100
file IO
--- a/src/de/uapcore/sudoku/ActionHandler.java Sat Jan 26 19:34:31 2013 +0100 +++ b/src/de/uapcore/sudoku/ActionHandler.java Sun Jan 27 15:03:57 2013 +0100 @@ -28,6 +28,9 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.io.File; +import java.io.IOException; +import javax.swing.JFileChooser; import javax.swing.JOptionPane; /** @@ -57,19 +60,60 @@ } private boolean chooseSaveFilename() { - // TODO: fileselector - return false; + 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 boolean save() { - if (!doc.isFilenameSet()) { + 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); - // TODO: save to file + 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, @@ -100,7 +144,7 @@ "Das Feld ist ungespeichert - jetzt speichern?", "Sudoku", JOptionPane.YES_NO_CANCEL_OPTION); if (result == JOptionPane.YES_OPTION) { - if (save()) { + if (save(false)) { proceed = true; } } else if (result == JOptionPane.NO_OPTION) { @@ -121,8 +165,14 @@ field.clear(); } break; + case OPEN: + open(); + break; case SAVE: - save(); + save(false); + break; + case SAVE_AS: + save(true); break; case CHECK: check();
--- a/src/de/uapcore/sudoku/DocumentHandler.java Sat Jan 26 19:34:31 2013 +0100 +++ b/src/de/uapcore/sudoku/DocumentHandler.java Sun Jan 27 15:03:57 2013 +0100 @@ -26,10 +26,15 @@ package de.uapcore.sudoku; +import java.io.BufferedReader; import java.io.BufferedWriter; +import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStreamReader; import java.io.OutputStreamWriter; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * @@ -43,7 +48,34 @@ if (!isFilenameSet()) { throw new IOException("no filename supplied"); } - // TODO: implement + 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.valueOf(c[i])); + } + row++; + } else { + break; + } + } + if (row != 9) { + throw new IOException("Kein Sudoku-Feld enthalten!"); + } + } } public void save(Field field) throws IOException {
--- a/src/de/uapcore/sudoku/SudokuTextField.java Sat Jan 26 19:34:31 2013 +0100 +++ b/src/de/uapcore/sudoku/SudokuTextField.java Sun Jan 27 15:03:57 2013 +0100 @@ -59,10 +59,12 @@ private void handle(KeyEvent e) { char c = e.getKeyChar(); if (!e.isAltDown() && !e.isControlDown() && - Character.isLetterOrDigit(c)) { + !Character.isISOControl(c)) { // Perform clean input check if (getText().length() > 0 && getSelectedText() == null) { - e.consume(); + if (c != KeyEvent.CHAR_UNDEFINED) { + e.consume(); + } } else { if (c < '1' || c > '9') { e.consume();