summaryrefslogtreecommitdiff
path: root/sources/simulator
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2008-06-01 19:28:00 +0000
committerPaul Buetow <paul@buetow.org>2008-06-01 19:28:00 +0000
commit00120474f64906e34fa79ce6ac8eede521c320d5 (patch)
tree88146a9507716ba40f10b01d55e14670a364f605 /sources/simulator
parent9a0fc6463ac1bca1ec05056fb4f84163d1e9fc26 (diff)
initial complete serialization support
Diffstat (limited to 'sources/simulator')
-rw-r--r--sources/simulator/VSSimulator.java72
-rw-r--r--sources/simulator/VSSimulatorCanvas.java82
-rw-r--r--sources/simulator/VSSimulatorFrame.java38
3 files changed, 181 insertions, 11 deletions
diff --git a/sources/simulator/VSSimulator.java b/sources/simulator/VSSimulator.java
index 3ae6a3c..d3456b1 100644
--- a/sources/simulator/VSSimulator.java
+++ b/sources/simulator/VSSimulator.java
@@ -25,6 +25,7 @@ package simulator;
import java.awt.*;
import java.awt.event.*;
+import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
@@ -44,7 +45,7 @@ import utils.*;
*
* @author Paul C. Buetow
*/
-public class VSSimulator extends JPanel {
+public class VSSimulator extends JPanel implements Serializable {
/** the serial version uid */
private static final long serialversionuid = 1l;
@@ -382,17 +383,32 @@ public class VSSimulator extends JPanel {
* @param simulatorFrame the simulator frame
*/
public VSSimulator(VSPrefs prefs, VSSimulatorFrame simulatorFrame) {
+ init(prefs, simulatorFrame);
+ }
+
+ /**
+ * inits the VSSimulator object.
+ *
+ * @param prefs the prefs
+ * @param simulatorFrame the simulator frame
+ */
+ private void init(VSPrefs prefs, VSSimulatorFrame simulatorFrame) {
this.prefs = prefs;
this.simulatorFrame = simulatorFrame;
- this.logging = new VSLogging();
this.simulatorNum = ++simulatorCounter;
this.menuItemStates = new VSMenuItemStates(false, false, false, true);
this.localTextFields = new ArrayList<String>();
this.globalTextFields = new ArrayList<String>();
+ /* Not null if init has been called from the deserialization */
+ if (logging == null)
+ this.logging = new VSLogging();
+
logging.logg(prefs.getString("lang.simulator.new"));
+
fillContentPane();
updateFromPrefs();
+
splitPaneH.setDividerLocation(
prefs.getInteger("div.window.splitsize"));
@@ -425,7 +441,10 @@ public class VSSimulator extends JPanel {
splitPaneH = new JSplitPane();
splitPaneV = new JSplitPane();
- simulatorCanvas = new VSSimulatorCanvas(prefs, this, logging);
+ /* Not null if init has been called from the deserialization */
+ if (simulatorCanvas == null)
+ simulatorCanvas = new VSSimulatorCanvas(prefs, this, logging);
+
taskManager = simulatorCanvas.getTaskManager();
logging.setSimulatorCanvas(simulatorCanvas);
@@ -1196,4 +1215,51 @@ public class VSSimulator extends JPanel {
public VSPrefs getPrefs() {
return prefs;
}
+
+ /**
+ * Write object.
+ *
+ * @param objectOutputStream the object output stream
+ *
+ * @throws IOException Signals that an I/O exception has occurred.
+ */
+ public synchronized void writeObject(ObjectOutputStream objectOutputStream)
+ throws IOException {
+ objectOutputStream.writeObject(prefs);
+ objectOutputStream.writeObject(simulatorCanvas);
+ }
+
+ /**
+ * Read object.
+ *
+ * @param objectInputStream the object input stream
+ *
+ * @throws IOException Signals that an I/O exception has occurred.
+ * @throws ClassNotFoundException the class not found exception
+ */
+ @SuppressWarnings("unchecked")
+ public synchronized void readObject(ObjectInputStream objectInputStream)
+ throws IOException, ClassNotFoundException {
+ if (VSDeserializationHelper.DEBUG)
+ System.out.println("Deserializing: VSSimulator");
+
+ VSDeserializationHelper.setObject("simulator", this);
+
+ VSSimulatorFrame simulatorFrame = (VSSimulatorFrame)
+ VSDeserializationHelper.getObject(
+ "simulatorFrame");
+
+ // TODO: Merge prefs!?!
+ VSPrefs prefs = (VSPrefs) objectInputStream.readObject();
+ VSDeserializationHelper.setObject("prefs", prefs);
+
+ this.logging = new VSLogging();
+ VSDeserializationHelper.setObject("logging", logging);
+
+ this.simulatorCanvas =
+ (VSSimulatorCanvas) objectInputStream.readObject();
+ VSDeserializationHelper.setObject("simulatorCanvas", simulatorCanvas);
+
+ init(prefs, simulatorFrame);
+ }
}
diff --git a/sources/simulator/VSSimulatorCanvas.java b/sources/simulator/VSSimulatorCanvas.java
index b0ac461..d6bc5d2 100644
--- a/sources/simulator/VSSimulatorCanvas.java
+++ b/sources/simulator/VSSimulatorCanvas.java
@@ -26,6 +26,7 @@ package simulator;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
+import java.io.*;
import java.util.*;
import javax.swing.*;
@@ -36,6 +37,7 @@ import events.implementations.*;
import events.internal.*;
import prefs.*;
import prefs.editors.*;
+import utils.*;
/**
* The class VSSimulatorCanvas. An instance of this object represents the
@@ -46,7 +48,9 @@ import prefs.editors.*;
*
* @author Paul C. Buetow
*/
-public class VSSimulatorCanvas extends Canvas implements Runnable {
+public class VSSimulatorCanvas extends Canvas
+ implements Runnable, Serializable {
+
/** The serial version uid */
private static final long serialVersionUID = 1L;
@@ -471,19 +475,39 @@ public class VSSimulatorCanvas extends Canvas implements Runnable {
*/
public VSSimulatorCanvas(VSPrefs prefs, VSSimulator simulator,
VSLogging logging) {
+ init(prefs, simulator, logging);
+ }
+
+ /**
+ * Instantiates inits the VSSimulatorCanvas object.
+ *
+ * @param prefs the prefs
+ * @param simulator the simulator
+ * @param logging the logging
+ */
+ private void init(VSPrefs prefs, VSSimulator simulator,
+ VSLogging logging) {
this.prefs = prefs;
this.simulator = simulator;
this.logging = logging;
- this.taskManager = new VSTaskManager(prefs, this);
this.messageLines = new LinkedList<VSMessageLine>();
this.messageLinesToRemove = new LinkedList<VSMessageLine>();
- this.processes = new ArrayList<VSProcess>();
- numProcesses = prefs.getInteger("sim.process.num");
- updateFromPrefs();
+ /* May be not null if called from deserialization */
+ if (taskManager == null)
+ this.taskManager = new VSTaskManager(prefs, this);
+
+ /* May be not null if called from deserialization */
+ if (processes == null) {
+ this.processes = new ArrayList<VSProcess>();
+
+ numProcesses = prefs.getInteger("sim.process.num");
+
+ for (int i = 0; i < numProcesses; ++i)
+ processes.add(createProcess(i));
+ }
- for (int i = 0; i < numProcesses; ++i)
- processes.add(createProcess(i));
+ updateFromPrefs();
final VSPrefs finalPrefs = prefs;
addMouseListener(new MouseListener() {
@@ -1520,4 +1544,48 @@ public class VSSimulatorCanvas extends Canvas implements Runnable {
simulator.addProcessAtIndex(processes.size()-1);
}
}
+
+ /**
+ * Write object.
+ *
+ * @param objectOutputStream the object output stream
+ *
+ * @throws IOException Signals that an I/O exception has occurred.
+ */
+ public synchronized void writeObject(ObjectOutputStream objectOutputStream)
+ throws IOException {
+ synchronized (processes) {
+ objectOutputStream.writeObject(processes);
+ }
+ objectOutputStream.writeObject(taskManager);
+ }
+
+ /**
+ * Read object.
+ *
+ * @param objectInputStream the object input stream
+ *
+ * @throws IOException Signals that an I/O exception has occurred.
+ * @throws ClassNotFoundException the class not found exception
+ */
+ @SuppressWarnings("unchecked")
+ public synchronized void readObject(ObjectInputStream objectInputStream)
+ throws IOException, ClassNotFoundException {
+ if (VSDeserializationHelper.DEBUG)
+ System.out.println("Deserializing: VSSimulatorCanvas");
+
+ VSPrefs prefs = (VSPrefs) VSDeserializationHelper.getObject("prefs");
+ VSSimulator simulator =
+ (VSSimulator) VSDeserializationHelper.getObject("simulator");
+ VSLogging logging =
+ (VSLogging) VSDeserializationHelper.getObject("logging");
+
+ this.processes = (ArrayList<VSProcess>) objectInputStream.readObject();
+ this.numProcesses = processes.size();
+
+ this.taskManager = (VSTaskManager) objectInputStream.readObject();
+ VSDeserializationHelper.setObject("taskManager", taskManager);
+
+ init(prefs, simulator, logging);
+ }
}
diff --git a/sources/simulator/VSSimulatorFrame.java b/sources/simulator/VSSimulatorFrame.java
index 890eb28..f5ef5af 100644
--- a/sources/simulator/VSSimulatorFrame.java
+++ b/sources/simulator/VSSimulatorFrame.java
@@ -25,6 +25,7 @@ package simulator;
import java.awt.*;
import java.awt.event.*;
+import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
@@ -41,7 +42,7 @@ import utils.*;
*
* @author Paul C. Buetow
*/
-public class VSSimulatorFrame extends VSFrame {
+public class VSSimulatorFrame extends VSFrame implements Serializable {
/** The serial version uid */
private static final long serialVersionUID = 1L;
@@ -507,4 +508,39 @@ public class VSSimulatorFrame extends VSFrame {
return new ImageIcon(imageURL, descr);
}
+
+ /**
+ * Write object.
+ *
+ * @param objectOutputStream the object output stream
+ *
+ * @throws IOException Signals that an I/O exception has occurred.
+ */
+ public synchronized void writeObject(ObjectOutputStream objectOutputStream)
+ throws IOException {
+ objectOutputStream.writeObject(currentSimulator);
+ }
+
+ /**
+ * Read object.
+ *
+ * @param objectInputStream the object input stream
+ *
+ * @throws IOException Signals that an I/O exception has occurred.
+ * @throws ClassNotFoundException the class not found exception
+ */
+ @SuppressWarnings("unchecked")
+ public synchronized void readObject(ObjectInputStream objectInputStream)
+ throws IOException, ClassNotFoundException {
+ if (VSDeserializationHelper.DEBUG)
+ System.out.println("Deserializing: VSSimulatorFrame");
+
+ VSDeserializationHelper.init();
+ VSDeserializationHelper.setObject("simulatorFrame", this);
+
+ currentSimulator = (VSSimulator) objectInputStream.readObject();
+
+ VSDeserializationHelper.destroy();
+ addSimulator(currentSimulator);
+ }
}