--- 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();