diff options
| author | Paul Buetow <paul@buetow.org> | 2008-06-03 12:23:16 +0000 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2008-06-03 12:23:16 +0000 |
| commit | b65cb84037cec3185aeed8b996455f8e97c17216 (patch) | |
| tree | ae4f089ec5374af6cbd0bd8157e4bb988292652b /sources/serialize/VSSerialize.java | |
| parent | ecdeab2258fc12a3b16337eff49a9b42536d3076 (diff) | |
ok
Diffstat (limited to 'sources/serialize/VSSerialize.java')
| -rw-r--r-- | sources/serialize/VSSerialize.java | 74 |
1 files changed, 72 insertions, 2 deletions
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<String,Object> 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); @@ -177,6 +185,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. * * @param filename The 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"); + } + }; + } + } |
