summaryrefslogtreecommitdiff
path: root/sources
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2008-06-03 12:23:16 +0000
committerPaul Buetow <paul@buetow.org>2008-06-03 12:23:16 +0000
commitb65cb84037cec3185aeed8b996455f8e97c17216 (patch)
treeae4f089ec5374af6cbd0bd8157e4bb988292652b /sources
parentecdeab2258fc12a3b16337eff49a9b42536d3076 (diff)
ok
Diffstat (limited to 'sources')
-rw-r--r--sources/prefs/VSDefaultPrefs.java1
-rw-r--r--sources/serialize/VSSerialize.java74
-rw-r--r--sources/simulator/VSSimulatorFrame.java30
3 files changed, 96 insertions, 9 deletions
diff --git a/sources/prefs/VSDefaultPrefs.java b/sources/prefs/VSDefaultPrefs.java
index b9a3635..bbf3c5f 100644
--- a/sources/prefs/VSDefaultPrefs.java
+++ b/sources/prefs/VSDefaultPrefs.java
@@ -70,6 +70,7 @@ public class VSDefaultPrefs extends VSPrefs {
initString("lang.cancel", "Abbrechen");
initString("lang.client", "Client");
initString("lang.clientrequest.start", "Clientanfrage starten");
+ initString("lang.dat", "VS-Sim. Speicherung");
initString("lang.serverrequest.start", "Serveranfrage starten");
initString("lang.close", "Schliessen");
initString("lang.colorchooser", "Farbauswahl");
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");
+ }
+ };
+ }
+
}
diff --git a/sources/simulator/VSSimulatorFrame.java b/sources/simulator/VSSimulatorFrame.java
index c538284..2b9042c 100644
--- a/sources/simulator/VSSimulatorFrame.java
+++ b/sources/simulator/VSSimulatorFrame.java
@@ -1,5 +1,4 @@
-/*
- * Copyright (c) 2008 Paul C. Buetow, vs@dev.buetow.org
+/* * Copyright (c) 2008 Paul C. Buetow, vs@dev.buetow.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@@ -145,13 +144,13 @@ public class VSSimulatorFrame extends VSFrame {
} else if (sourceText.equals(
finalPrefs.getString("lang.open"))) {
- pauseCurrentSimulator();
- resetCurrentSimulator();
+ //pauseCurrentSimulator();
+ //resetCurrentSimulator();
VSSerialize serialize = new VSSerialize();
VSSimulator simulator = serialize.openSimulator(
- VSSerialize.STANDARD_FILENAME,
VSSimulatorFrame.this);
- addSimulator(simulator);
+ if (simulator != null)
+ addSimulator(simulator);
} else if (sourceText.equals(
finalPrefs.getString("lang.save"))) {
@@ -159,10 +158,18 @@ public class VSSimulatorFrame extends VSFrame {
pauseCurrentSimulator();
resetCurrentSimulator();
VSSerialize serialize = new VSSerialize();
- serialize.saveSimulator(VSSerialize.STANDARD_FILENAME,
+ serialize.saveSimulator(VSSerialize.LAST_FILENAME,
currentSimulator);
} else if (sourceText.equals(
+ finalPrefs.getString("lang.saveas"))) {
+
+ pauseCurrentSimulator();
+ resetCurrentSimulator();
+ VSSerialize serialize = new VSSerialize();
+ serialize.saveSimulator(currentSimulator);
+
+ } else if (sourceText.equals(
finalPrefs.getString("lang.about"))) {
new VSAbout(finalPrefs, VSSimulatorFrame.this);
@@ -564,4 +571,13 @@ public class VSSimulatorFrame extends VSFrame {
return new ImageIcon(imageURL, descr);
}
+
+ /**
+ * Gets the prefs.
+ *
+ * @return the prefs
+ */
+ public VSPrefs getPrefs() {
+ return prefs;
+ }
}