From b65cb84037cec3185aeed8b996455f8e97c17216 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Tue, 3 Jun 2008 12:23:16 +0000 Subject: ok --- sources/serialize/VSSerialize.java | 74 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 72 insertions(+), 2 deletions(-) (limited to 'sources/serialize/VSSerialize.java') diff --git a/sources/serialize/VSSerialize.java b/sources/serialize/VSSerialize.java index 46b68d4..7591ea1 100644 --- a/sources/serialize/VSSerialize.java +++ b/sources/serialize/VSSerialize.java @@ -25,6 +25,7 @@ package serialize; import java.io.*; import java.util.HashMap; +import javax.swing.*; import prefs.*; import simulator.*; @@ -41,8 +42,8 @@ public final class VSSerialize { /** True if debugg mode of deserialization */ public static final boolean DEBUG = true; - /** The standard filename to save simulators to */ - public static final String STANDARD_FILENAME = "simulator.dat"; + /** The last filename used for saveing/opening*/ + public static String LAST_FILENAME = null; /** For temp object storage */ private static HashMap objects; @@ -158,6 +159,13 @@ public final class VSSerialize { * @param simulator The simulator */ public void saveSimulator(String filename, VSSimulator simulator) { + if (filename == null) { + saveSimulator(simulator); + return; + } + + LAST_FILENAME = filename; + try { FileOutputStream fileOutputStream = new FileOutputStream(filename); @@ -176,6 +184,25 @@ public final class VSSerialize { } } + /** + * Saves the given simulator to a file choosen by the file chooser. + * + * @param simulator The simulator + */ + public void saveSimulator(VSSimulator simulator) { + VSPrefs prefs = simulator.getPrefs(); + VSSimulatorFrame simulatorFrame = simulator.getSimulatorFrame(); + + JFileChooser fileChooser = new JFileChooser(); + fileChooser.setMultiSelectionEnabled(false); + fileChooser.addChoosableFileFilter(createFileFilter(prefs)); + + if (fileChooser.showOpenDialog(simulatorFrame) == + JFileChooser.APPROVE_OPTION) + saveSimulator(fileChooser.getSelectedFile().getName(), + simulator); + } + /** * Opens a simulator from the given filename. * @@ -211,4 +238,47 @@ public final class VSSerialize { return simulator; } + + /** + * Opens a simulator from a file selected from a file chooser. + * + * @param simulatorFrame The simulator frame + * + * @return The simulator object, and null if no success + */ + public VSSimulator openSimulator(VSSimulatorFrame simulatorFrame) { + VSPrefs prefs = simulatorFrame.getPrefs(); + + JFileChooser fileChooser = new JFileChooser(); + fileChooser.setMultiSelectionEnabled(false); + fileChooser.addChoosableFileFilter(createFileFilter(prefs)); + + if (fileChooser.showOpenDialog(simulatorFrame) == + JFileChooser.APPROVE_OPTION) + return openSimulator(fileChooser.getSelectedFile().getName(), + simulatorFrame); + + return null; + } + + /** + * Creates a file filter for the file choosers + * + * @param prefs The default prefs + */ + private javax.swing.filechooser.FileFilter createFileFilter( + final VSPrefs prefs) { + return new javax.swing.filechooser.FileFilter() { + public boolean accept(File file) { + if (file.isDirectory()) + return true; + return file.getName().toLowerCase().endsWith(".dat"); + } + + public String getDescription() { + return prefs.getString("lang.dat"); + } + }; + } + } -- cgit v1.2.3