diff options
| -rw-r--r-- | sources/core/VSProcess.java | 70 | ||||
| -rw-r--r-- | sources/core/VSTask.java | 76 | ||||
| -rw-r--r-- | sources/core/VSTaskManager.java | 88 | ||||
| -rw-r--r-- | sources/events/VSAbstractEvent.java | 24 | ||||
| -rw-r--r-- | sources/prefs/VSPrefs.java | 85 | ||||
| -rw-r--r-- | sources/prefs/editors/VSEditorTable.java | 29 | ||||
| -rw-r--r-- | sources/protocols/VSAbstractProtocol.java | 21 | ||||
| -rw-r--r-- | sources/serialize/VSSerializable.java | 52 | ||||
| -rw-r--r-- | sources/serialize/VSSerialize.java | 83 | ||||
| -rw-r--r-- | sources/simulator/VSSimulator.java | 62 | ||||
| -rw-r--r-- | sources/simulator/VSSimulatorCanvas.java | 61 | ||||
| -rw-r--r-- | sources/simulator/VSSimulatorFrame.java | 76 | ||||
| -rw-r--r-- | sources/utils/VSSerialize.java | 171 |
13 files changed, 398 insertions, 500 deletions
diff --git a/sources/core/VSProcess.java b/sources/core/VSProcess.java index 023e017..bf4fa98 100644 --- a/sources/core/VSProcess.java +++ b/sources/core/VSProcess.java @@ -32,6 +32,7 @@ import events.*; import events.implementations.*; import prefs.*; import protocols.*; +import serialize.*; import simulator.*; import utils.*; @@ -41,7 +42,7 @@ import utils.*; * * @author Paul C. Buetow */ -public class VSProcess extends VSPrefs implements Serializable { +public class VSProcess extends VSPrefs implements VSSerializable { /** The data serialization id. */ private static final long serialVersionUID = 1L; @@ -378,7 +379,7 @@ public class VSProcess extends VSPrefs implements Serializable { * returns a non-negative value. The random crash task uses the simulaion's * global time for its scheduling. */ - public void createRandomCrashTask() { + public synchronized void createRandomCrashTask() { if (!isCrashed) { VSTaskManager taskManager = simulatorCanvas.getTaskManager(); long crashTime = getARandomCrashTime(); @@ -715,7 +716,7 @@ public class VSProcess extends VSPrefs implements Serializable { /** * Increases the process' lamport time. */ - public void increaseLamportTime() { + public synchronized void increaseLamportTime() { setLamportTime(getLamportTime()+1); } @@ -724,7 +725,7 @@ public class VSProcess extends VSPrefs implements Serializable { * * @param time the lamport time to use as its update reference. */ - public void updateLamportTime(long time) { + public synchronized void updateLamportTime(long time) { final long lamportTime = getLamportTime() + 1; if (time > lamportTime) @@ -841,7 +842,7 @@ public class VSProcess extends VSPrefs implements Serializable { * * @param message the message to send. */ - public void sendMessage(VSMessage message) { + public synchronized void sendMessage(VSMessage message) { StringBuffer buffer = new StringBuffer(); buffer.append(prefs.getString("lang.message.sent")); buffer.append("; "); @@ -873,7 +874,7 @@ public class VSProcess extends VSPrefs implements Serializable { /* (non-Javadoc) * @see prefs.VSPrefs#toString() */ - public String toString() { + public synchronized String toString() { StringBuffer buffer = new StringBuffer(); buffer.append(prefs.getString("lang.process.id")); buffer.append(": "); @@ -898,7 +899,7 @@ public class VSProcess extends VSPrefs implements Serializable { * * @return the extended string representation */ - public String toStringFull() { + public synchronized String toStringFull() { StringBuffer buffer = new StringBuffer(); buffer.append(toString()); buffer.append("; paused: "); @@ -946,7 +947,7 @@ public class VSProcess extends VSPrefs implements Serializable { * * @param index the index the process has to get removed. */ - public void removedAProcessAtIndex(int index) { + public synchronized void removedAProcessAtIndex(int index) { if (index < processNum) --processNum; @@ -960,7 +961,7 @@ public class VSProcess extends VSPrefs implements Serializable { * Called by the simulator canvas if a process has been added to the * simulator. */ - public void addedAProcess() { + public synchronized void addedAProcess() { vectorTime.add(new Long(0)); for (VSVectorTime vectorTime : vectorTimeHistory) vectorTime.add(new Long(0)); @@ -991,7 +992,8 @@ public class VSProcess extends VSPrefs implements Serializable { * * @return the protocol object */ - public VSAbstractProtocol getProtocolObject(String protocolClassname) { + public synchronized VSAbstractProtocol getProtocolObject( + String protocolClassname) { VSAbstractProtocol protocol = null; if (!objectExists(protocolClassname)) { @@ -1010,42 +1012,42 @@ public class VSProcess extends VSPrefs implements Serializable { } /* (non-Javadoc) - * @see prefs.VSPrefs#writeObject() + * @see serialize.VSSerializable#serialize(serialize.VSSerialize, + * java.io.ObjectOutputStream) */ - public synchronized void writeObject(ObjectOutputStream objectOutputStream) + public synchronized void serialize(VSSerialize serialize, + ObjectOutputStream objectOutputStream) throws IOException { - super.writeObject(objectOutputStream); - objectOutputStream.writeObject(new Integer(processNum)); - objectOutputStream.writeObject(protocolsToReset); + super.serialize(serialize, objectOutputStream); + objectOutputStream.writeObject(new Integer(protocolsToReset.size())); + for (VSAbstractProtocol protocol : protocolsToReset) { + objectOutputStream.writeObject(protocol.getClassname()); + protocol.serialize(serialize, objectOutputStream); + } } /* (non-Javadoc) - * @see prefs.VSPrefs#readObject() + * @see serialize.VSSerializable#deserialize(serialize.VSSerialize, + * java.io.ObjectInputStream) */ @SuppressWarnings("unchecked") - public synchronized void readObject(ObjectInputStream objectInputStream) + public synchronized void deserialize(VSSerialize serialize, + ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException { - super.readObject(objectInputStream); + super.deserialize(serialize, objectInputStream); - if (VSDeserializationHelper.DEBUG) + if (VSSerialize.DEBUG) System.out.println("Deserializing: VSProcess"); - VSLogging logging = (VSLogging) - VSDeserializationHelper.getObject("logging"); - VSSimulatorCanvas simulatorCanvas = (VSSimulatorCanvas) - VSDeserializationHelper.getObject( - "simulatorCanvas"); - VSPrefs prefs = (VSPrefs) VSDeserializationHelper.getObject("prefs"); - - Integer processNum = (Integer) objectInputStream.readObject(); - this.protocolsToReset = (ArrayList<VSAbstractProtocol>) - objectInputStream.readObject(); + int numProtocols = ((Integer) + objectInputStream.readObject()).intValue(); - for (VSAbstractProtocol protocol : protocolsToReset) - setObject(protocol.getClassname(), protocol); + for (int i = 0; i < numProtocols; ++i) { + String protocolClassname = (String) objectInputStream.readObject(); + VSAbstractProtocol protocol = getProtocolObject(protocolClassname); + protocol.deserialize(serialize, objectInputStream); + } - VSDeserializationHelper.setObject(processNum.intValue(), - "process", this); - init(prefs, processNum.intValue(), simulatorCanvas, logging); + serialize.setObject(processNum, "process", this); } } diff --git a/sources/core/VSTask.java b/sources/core/VSTask.java index d27a9a4..a903868 100644 --- a/sources/core/VSTask.java +++ b/sources/core/VSTask.java @@ -30,6 +30,7 @@ import events.implementations.*; import events.internal.*; import prefs.VSPrefs; import protocols.VSAbstractProtocol; +import serialize.*; import utils.*; /** @@ -41,7 +42,7 @@ import utils.*; * * @author Paul C. Buetow */ -public class VSTask implements Comparable, Serializable { +public class VSTask implements Comparable, VSSerializable { /** The serial version uid */ private static final long serialVersionUID = 1L; @@ -95,6 +96,17 @@ public class VSTask implements Comparable, Serializable { } /** + * Instantiates a new task during a deserialization. + * + * @param serialize the serialize object + * @param process the object input stream + */ + public VSTask(VSSerialize serialize, ObjectInputStream objectInputStream) + throws IOException, ClassNotFoundException { + deserialize(serialize, objectInputStream); + } + + /** * Inits the task * * @param taskTime the task time @@ -338,51 +350,59 @@ public class VSTask implements Comparable, Serializable { return 0; } - /** - * Write object. - * - * @param objectOutputStream the object output stream - * - * @throws IOException Signals that an I/O exception has occurred. + /* (non-Javadoc) + * @see serialize.VSSerializable#serialize(serialize.VSSerialize, + * java.io.ObjectOutputStream) */ - public synchronized void writeObject(ObjectOutputStream objectOutputStream) + public synchronized void serialize(VSSerialize serialize, + ObjectOutputStream objectOutputStream) throws IOException { - objectOutputStream.writeObject(event); objectOutputStream.writeObject(new Integer(process.getProcessNum())); + objectOutputStream.writeObject(event.getClassname()); + objectOutputStream.writeObject(new Integer(event.getID())); + event.serialize(serialize, objectOutputStream); objectOutputStream.writeObject(new Integer(taskNum)); objectOutputStream.writeObject(new Long(taskTime)); objectOutputStream.writeObject(new Boolean(isGlobalTimed)); objectOutputStream.writeObject(new Boolean(isProgrammed)); } - /** - * 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 + /* (non-Javadoc) + * @see serialize.VSSerializable#deserialize(serialize.VSSerialize, + * java.io.ObjectInputStream) */ @SuppressWarnings("unchecked") - public synchronized void readObject(ObjectInputStream objectInputStream) + public synchronized void deserialize(VSSerialize serialize, + ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException { - if (VSDeserializationHelper.DEBUG) + if (VSSerialize.DEBUG) System.out.println("Deserializing: VSTask"); - this.event = (VSAbstractEvent) objectInputStream.readObject(); - Integer processNum = (Integer) objectInputStream.readObject(); - Integer taskNum = (Integer) objectInputStream.readObject(); - Long taskTime = (Long) objectInputStream.readObject(); + int processNum = ((Integer) objectInputStream.readObject()).intValue(); + VSProcess process = (VSProcess) + serialize.getObject(processNum, "process"); + + String eventClassname = (String) objectInputStream.readObject(); + int eventID = ((Integer) objectInputStream.readObject()).intValue(); + + VSAbstractEvent event = (VSAbstractEvent) + serialize.getObject(eventID, "event"); + + if (event == null) { + event = VSRegisteredEvents. + createEventInstanceByClassname(eventClassname, process); + + serialize.setObject(eventID, "event", event); + } + + int taskNum = ((Integer) objectInputStream.readObject()).intValue(); + long taskTime = ((Long) objectInputStream.readObject()).longValue(); Boolean isGlobalTimed = (Boolean) objectInputStream.readObject(); Boolean isProgrammed = (Boolean) objectInputStream.readObject(); - VSDeserializationHelper.setObject(taskNum.intValue(), "task", this); - - VSProcess process = (VSProcess) VSDeserializationHelper.getObject( - processNum.intValue(), "process"); + serialize.setObject(taskNum, "task", this); - init(taskTime.longValue(), process, event, - !isGlobalTimed.booleanValue()); + init(taskTime, process, event, !isGlobalTimed.booleanValue()); this.isProgrammed = isProgrammed.booleanValue(); } diff --git a/sources/core/VSTaskManager.java b/sources/core/VSTaskManager.java index a63db51..cbbde56 100644 --- a/sources/core/VSTaskManager.java +++ b/sources/core/VSTaskManager.java @@ -27,6 +27,7 @@ import java.io.*; import java.util.*; import prefs.*; +import serialize.*; import simulator.*; import utils.*; @@ -38,7 +39,7 @@ import utils.*; * * @author Paul C. Buetow */ -public class VSTaskManager implements Serializable { +public class VSTaskManager implements VSSerializable { /** The seriao version uid */ private static final long serialVersionUID = 1L; @@ -493,80 +494,59 @@ public class VSTaskManager implements Serializable { return descr + ")"; } - /** - * Write object. - * - * @param objectOutputStream the object output stream - * - * @throws IOException Signals that an I/O exception has occurred. + /* (non-Javadoc) + * @see serialize.VSSerializable#serialize(serialize.VSSerialize, + * java.io.ObjectOutputStream) */ - public synchronized void writeObject(ObjectOutputStream objectOutputStream) + public synchronized void serialize(VSSerialize serialize, + ObjectOutputStream objectOutputStream) throws IOException { - VSPriorityQueue<VSTask> temp = new VSPriorityQueue<VSTask>(); + + ArrayList<VSTask> tasks = new ArrayList<VSTask>(); for (VSTask task : fullfilledProgrammedTasks) - if (task.isGlobalTimed()) - temp.add(task); + tasks.add(task); - for (VSTask task : globalTasks) - temp.add(task); + for (VSTask task : this.globalTasks) + tasks.add(task); ArrayList<VSProcess> processes = simulatorCanvas.getProcesses(); - HashMap<Integer, ArrayList<VSTask>> map = - new HashMap<Integer, ArrayList<VSTask>>(); synchronized (processes) { for (VSProcess process : processes) { - VSPriorityQueue<VSTask> tasks = process.getTasks(); - ArrayList<VSTask> tasks_ = new ArrayList<VSTask>(); - for (VSTask task : tasks) - tasks_.add(task); - map.put(new Integer(process.getProcessNum()), tasks_); + VSPriorityQueue<VSTask> localTasks = process.getTasks(); + ArrayList<VSTask> tasks_ = new ArrayList<VSTask>(); + for (VSTask task : localTasks) + tasks.add(task); } } - objectOutputStream.writeObject(temp); - objectOutputStream.writeObject(map); + objectOutputStream.writeObject(new Integer(tasks.size())); + for (VSTask task : tasks) + task.serialize(serialize, objectOutputStream); } - /** - * 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 + /* (non-Javadoc) + * @see serialize.VSSerializable#deserialize(serialize.VSSerialize, + * java.io.ObjectInputStream) */ @SuppressWarnings("unchecked") - public synchronized void readObject(ObjectInputStream objectInputStream) + public synchronized void deserialize(VSSerialize serialize, + ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException { - if (VSDeserializationHelper.DEBUG) + if (VSSerialize.DEBUG) System.out.println("Deserializing: VSTaskManager"); - VSPrefs prefs = (VSPrefs) VSDeserializationHelper.getObject("prefs"); - VSSimulatorCanvas simulatorCanvas = (VSSimulatorCanvas) - VSDeserializationHelper.getObject( - "prefs"); - this.globalTasks = (PriorityQueue<VSTask>) - objectInputStream.readObject(); - - HashMap<Integer, ArrayList<VSTask>> map = - (HashMap<Integer, ArrayList<VSTask>>) - objectInputStream.readObject(); - - Set<Integer> set = map.keySet(); - for (Integer processNum : set) { - VSProcess process = (VSProcess) VSDeserializationHelper.getObject( - processNum.intValue(), - "process"); + int numTasks = ((Integer) objectInputStream.readObject()).intValue(); + globalTasks.clear(); - ArrayList<VSTask> tasks = (ArrayList<VSTask>) map.get(processNum); - VSPriorityQueue<VSTask> tasks_ = process.getTasks(); - - for (VSTask task: tasks) - tasks_.add(task); - } + ArrayList<VSProcess> processes = simulatorCanvas.getProcesses(); + synchronized (processes) { + for (VSProcess process : processes) + process.getTasks().clear(); + } - init(prefs, simulatorCanvas); + for (int i = 0; i < numTasks; ++i) + addTask(new VSTask(serialize, objectInputStream)); } } diff --git a/sources/events/VSAbstractEvent.java b/sources/events/VSAbstractEvent.java index 9a4062e..34e2b87 100644 --- a/sources/events/VSAbstractEvent.java +++ b/sources/events/VSAbstractEvent.java @@ -27,6 +27,7 @@ import java.io.*; import core.VSProcess; import prefs.VSPrefs; +import serialize.*; import utils.*; /** @@ -165,11 +166,13 @@ abstract public class VSAbstractEvent extends VSPrefs { abstract public void onStart(); /* (non-Javadoc) - * @see prefs.VSPrefs#writeObject() + * @see serialize.VSSerializable#serialize(serialize.VSSerialize, + * java.io.ObjectOutputStream) */ - public synchronized void writeObject(ObjectOutputStream objectOutputStream) + public synchronized void serialize(VSSerialize serialize, + ObjectOutputStream objectOutputStream) throws IOException { - super.writeObject(objectOutputStream); + super.serialize(serialize, objectOutputStream); objectOutputStream.writeObject(new Integer(super.getID())); //objectOutputStream.writeObject(new Integer(process.getProcessNum())); objectOutputStream.writeObject(eventShortname); @@ -177,22 +180,23 @@ abstract public class VSAbstractEvent extends VSPrefs { } /* (non-Javadoc) - * @see prefs.VSPrefs#readObject() + * @see serialize.VSSerializable#deserialize(serialize.VSSerialize, + * java.io.ObjectInputStream) */ @SuppressWarnings("unchecked") - public synchronized void readObject(ObjectInputStream objectInputStream) + public synchronized void deserialize(VSSerialize serialize, + ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException { - super.readObject(objectInputStream); + super.deserialize(serialize, objectInputStream); - if (VSDeserializationHelper.DEBUG) + if (VSSerialize.DEBUG) System.out.println("Deserializing: VSAbstractEvent"); - Integer id = (Integer) objectInputStream.readObject(); + int id = ((Integer) objectInputStream.readObject()).intValue(); //Integer processNum = (Integer) objectInputStream.readObject(); this.eventShortname = (String) objectInputStream.readObject(); this.eventClassname = (String) objectInputStream.readObject(); - VSDeserializationHelper.setObject(id.intValue(), - "event", this); + serialize.setObject(id, "event", this); } } diff --git a/sources/prefs/VSPrefs.java b/sources/prefs/VSPrefs.java index 6143e68..de1c249 100644 --- a/sources/prefs/VSPrefs.java +++ b/sources/prefs/VSPrefs.java @@ -27,10 +27,12 @@ import java.awt.Color; import java.io.*; import java.util.*; +import serialize.*; + /** * The class VSPrefs. */ -public class VSPrefs implements Serializable { +public class VSPrefs implements VSSerializable { /** The Constant BOOLEAN_PREFIX. */ public static final String BOOLEAN_PREFIX = "Boolean: "; @@ -930,14 +932,17 @@ public class VSPrefs implements Serializable { */ public void fillWithDefaults() {} - /** - * Write object. - * - * @param objectOutputStream the object output stream - * - * @throws IOException Signals that an I/O exception has occurred. + @SuppressWarnings("unchecked") + public synchronized void readObject(ObjectInputStream objectInputStream) + throws IOException, ClassNotFoundException { + } + + /* (non-Javadoc) + * @see serialize.VSSerializable#serialize(serialize.VSSerialize, + * java.io.ObjectOutputStream) */ - public synchronized void writeObject(ObjectOutputStream objectOutputStream) + public synchronized void serialize(VSSerialize serialize, + ObjectOutputStream objectOutputStream) throws IOException { objectOutputStream.writeObject(booleanPrefs); objectOutputStream.writeObject(colorPrefs); @@ -949,64 +954,28 @@ public class VSPrefs implements Serializable { objectOutputStream.writeObject(units); } - /** - * 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 + /* (non-Javadoc) + * @see serialize.VSSerializable#deserialize(serialize.VSSerialize, + * java.io.ObjectInputStream) */ @SuppressWarnings("unchecked") - public synchronized void readObject(ObjectInputStream objectInputStream) + public synchronized void deserialize(VSSerialize serialize, + ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException { booleanPrefs = (HashMap<String,Boolean>) objectInputStream.readObject(); colorPrefs = (HashMap<String,Color>) objectInputStream.readObject(); descriptionPrefs = new HashMap<String,String>(); floatPrefs = (HashMap<String,Float>) objectInputStream.readObject(); integerPrefs = (HashMap<String,Integer>) objectInputStream.readObject(); - vectorPrefs = (HashMap<String,Vector<Integer>>) objectInputStream.readObject(); + vectorPrefs = (HashMap<String,Vector<Integer>>) + objectInputStream.readObject(); longPrefs = (HashMap<String,Long>) objectInputStream.readObject(); restrictions = new HashMap<String,VSPrefsRestriction>(); stringPrefs = (HashMap<String,String>) objectInputStream.readObject(); units = (HashMap<String,String>) objectInputStream.readObject(); + objectPrefs.clear(); } - /* - public void saveFile(String filename) { - try { - FileOutputStream fileOutputStream = new FileOutputStream(filename); - ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream); - objectOutputStream.writeObject(this); - - } catch (IOException e) { - e.printStackTrace(); - } - } - */ - - /* - public static VSPrefs openFile(String filename) { - VSPrefs prefs = null; - - try { - FileInputStream fileInputStream = new FileInputStream(filename); - ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); - prefs = (VSPrefs) objectInputStream.readObject(); - - prefs.fillDefaultStrings(); - prefs.fillDefaultIntegers(); - prefs.fillDefaultFloats(); - prefs.fillDefaultColors(); - - } catch (Exception e) { - e.printStackTrace(); - } - - return prefs; - } - */ - /** * Copy integers. * @@ -1016,8 +985,10 @@ public class VSPrefs implements Serializable { public void copyIntegers(VSPrefs copyInto, String[] keys) { for (String key : keys) copyInto.initInteger(key, - getInteger(key), getDescription(INTEGER_PREFIX + key), - (VSPrefsRestriction.VSIntegerPrefRestriction) getRestriction(INTEGER_PREFIX + key), + getInteger(key), + getDescription(INTEGER_PREFIX + key), + (VSPrefsRestriction.VSIntegerPrefRestriction) + getRestriction(INTEGER_PREFIX + key), getUnit(INTEGER_PREFIX + key)); } @@ -1067,7 +1038,8 @@ public class VSPrefs implements Serializable { */ public void copyColors(VSPrefs copyInto, String[] keys) { for (String key : keys) - copyInto.initColor(key, getColor(key), getDescription(COLOR_PREFIX + key)); + copyInto.initColor(key, getColor(key), + getDescription(COLOR_PREFIX + key)); } /** @@ -1078,7 +1050,8 @@ public class VSPrefs implements Serializable { */ public void copyBooleans(VSPrefs copyInto, String[] keys) { for (String key : keys) - copyInto.initBoolean(key, getBoolean(key), getDescription(BOOLEAN_PREFIX + key)); + copyInto.initBoolean(key, getBoolean(key), + getDescription(BOOLEAN_PREFIX + key)); } /* (non-Javadoc) diff --git a/sources/prefs/editors/VSEditorTable.java b/sources/prefs/editors/VSEditorTable.java index ba5727c..770ebdd 100644 --- a/sources/prefs/editors/VSEditorTable.java +++ b/sources/prefs/editors/VSEditorTable.java @@ -26,6 +26,7 @@ package prefs.editors; import java.util.*; import java.awt.*; +import java.io.*; import javax.swing.*; import javax.swing.table.*; import javax.swing.text.*; @@ -36,7 +37,7 @@ import prefs.*; /** * The class VSEditorTable. */ -public class VSEditorTable extends JTable { +public class VSEditorTable extends JTable { /* implements Serializable */ private static final long serialVersionUID = 1L; /** The Constant MIN_ROWS. */ @@ -54,7 +55,7 @@ public class VSEditorTable extends JTable { /** * The class VSNode. */ - private class VSNode { + private class VSNode implements Serializable { /** The key. */ private String key; @@ -117,6 +118,30 @@ public class VSEditorTable extends JTable { public boolean isSeparator() { return comp == null; } + + /** + * 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 { + } + + /** + * 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 { + } } /** diff --git a/sources/protocols/VSAbstractProtocol.java b/sources/protocols/VSAbstractProtocol.java index 3398b2c..30e664c 100644 --- a/sources/protocols/VSAbstractProtocol.java +++ b/sources/protocols/VSAbstractProtocol.java @@ -29,6 +29,7 @@ import java.util.ArrayList; import core.*; import events.*; import events.internal.*; +import serialize.*; import utils.*; /** @@ -384,26 +385,30 @@ abstract public class VSAbstractProtocol extends VSAbstractEvent { } /* (non-Javadoc) - * @see prefs.VSPrefs#writeObject() + * @see serialize.VSSerializable#serialize(serialize.VSSerialize, + * java.io.ObjectOutputStream) */ - public synchronized void writeObject(ObjectOutputStream objectOutputStream) + public synchronized void serialize(VSSerialize serialize, + ObjectOutputStream objectOutputStream) throws IOException { - super.writeObject(objectOutputStream); + super.serialize(serialize, objectOutputStream); objectOutputStream.writeObject(new Boolean(hasOnServerStart)); } /* (non-Javadoc) - * @see prefs.VSPrefs#readObject() + * @see serialize.VSSerializable#deserialize(serialize.VSSerialize, + * java.io.ObjectInputStream) */ @SuppressWarnings("unchecked") - public synchronized void readObject(ObjectInputStream objectInputStream) + public synchronized void deserialize(VSSerialize serialize, + ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException { - super.readObject(objectInputStream); + super.deserialize(serialize, objectInputStream); - if (VSDeserializationHelper.DEBUG) + if (VSSerialize.DEBUG) System.out.println("Deserializing: VSAbstractProtocol"); this.hasOnServerStart = ((Boolean) - objectInputStream.readObject()).booleanValue(); + objectInputStream.readObject()).booleanValue(); } } diff --git a/sources/serialize/VSSerializable.java b/sources/serialize/VSSerializable.java new file mode 100644 index 0000000..a4affe9 --- /dev/null +++ b/sources/serialize/VSSerializable.java @@ -0,0 +1,52 @@ +/* + * 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 + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * All icons of the icons/ folder are under a Creative Commons + * Attribution-Noncommercial-Share Alike License a CC-by-nc-sa. + * + * The icon's homepage is http://code.google.com/p/ultimate-gnome/ + */ + +package serialize; + +import java.io.*; + +/** + * The Interface VSSerializable, + * + * @author Paul C. Buetow + */ +public interface VSSerializable { + /** + * Serializes + * + * @param serialize The serialize object + * @param objectOutputStream The object output stream + */ + public void serialize(VSSerialize serialize, + ObjectOutputStream objectOutputStream) + throws IOException; + /** + * Deserializes + * + * @param serialize The serialize object + * @param objectOutputStream The object input stream + */ + public void deserialize(VSSerialize serialize, + ObjectInputStream objectInputStream) + throws IOException, ClassNotFoundException; +} diff --git a/sources/serialize/VSSerialize.java b/sources/serialize/VSSerialize.java index b3bd18b..b659879 100644 --- a/sources/serialize/VSSerialize.java +++ b/sources/serialize/VSSerialize.java @@ -21,12 +21,13 @@ * The icon's homepage is http://code.google.com/p/ultimate-gnome/ */ -package utils; +package serialize; import java.io.*; import java.util.HashMap; -import simulator.VSSimulator; +import prefs.*; +import simulator.*; /** * The class VSSerialize, this static class helps do deserialize @@ -47,21 +48,34 @@ public final class VSSerialize { /** For temp object storage */ private static HashMap<String,Object> objects; + /** Holds the current VSSerialize object */ + private static VSSerialize serialize; + /** * Creates the VSSerialize object. */ public VSSerialize() { - init(); + init(); } /** * Initializes the helper. */ private void init() { + this.serialize = this; objects = new HashMap<String,Object>(); } /** + * Gets the current VSSerialize object. + * + * @return The current serialize object + */ + public static VSSerialize getSerialize() { + return serialize; + } + + /** * Sets an object. * * @param key The object key @@ -120,47 +134,52 @@ public final class VSSerialize { return object; } - /** - * Saves the given simulator to the given filename. - * - * @param filename The filename - * @param simulator The simulator - */ + /** + * Saves the given simulator to the given filename. + * + * @param filename The filename + * @param simulator The simulator + */ public void saveSimulator(String filename, VSSimulator simulator) { try { - FileOutputStream fileOutputStream = - new FileOutputStream(filename); - ObjectOutputStream objectOutputStream = - new ObjectOutputStream(fileOutputStream); + FileOutputStream fileOutputStream = + new FileOutputStream(filename); + ObjectOutputStream objectOutputStream = + new ObjectOutputStream(fileOutputStream); - objectOutputStream.writeObject(simulator.getPrefs()); - simulator.serialize(this); + VSPrefs prefs = simulator.getPrefs(); + prefs.serialize(this, objectOutputStream); + simulator.serialize(this, objectOutputStream); } catch (IOException e) { e.printStackTrace(); } } - /** - * Opens a simulator from the given filename. - * - * @param filename The filename. - * @param simulatorFrame The simulator frame - * - * @return The simulator object, and null if no success - */ + /** + * Opens a simulator from the given filename. + * + * @param filename The filename. + * @param simulatorFrame The simulator frame + * + * @return The simulator object, and null if no success + */ public VSSimulator openSimulator(String filename, - VSSimulatorFrame simulatorFrame) { + VSSimulatorFrame simulatorFrame) { + VSSimulator simulator = null; try { - FileInputStream fileInputStream = - new FileInputStream(filename); - ObjectInputStream objectInputStream = - new ObjectInputStream(fileInputStream); - - VSPrefs prefs = (VSPrefs) objectInputStream.readObject(); - simulator = new VSSimulator(prefs, simulatorFrame); - simulator.deserialize(this); + FileInputStream fileInputStream = + new FileInputStream(filename); + ObjectInputStream objectInputStream = + new ObjectInputStream(fileInputStream); + + VSPrefs prefs = new VSPrefs(); + prefs.deserialize(this, objectInputStream); + this.setObject("prefs", prefs); + + simulator = new VSSimulator(prefs, simulatorFrame); + simulator.deserialize(this, objectInputStream); } catch (Exception e) { e.printStackTrace(); diff --git a/sources/simulator/VSSimulator.java b/sources/simulator/VSSimulator.java index d3456b1..f376719 100644 --- a/sources/simulator/VSSimulator.java +++ b/sources/simulator/VSSimulator.java @@ -36,6 +36,7 @@ import events.*; import events.internal.*; import prefs.*; import prefs.editors.*; +import serialize.*; import utils.*; /** @@ -45,7 +46,7 @@ import utils.*; * * @author Paul C. Buetow */ -public class VSSimulator extends JPanel implements Serializable { +public class VSSimulator extends JPanel implements VSSerializable { /** the serial version uid */ private static final long serialversionuid = 1l; @@ -1063,10 +1064,14 @@ public class VSSimulator extends JPanel implements Serializable { public void updateTaskManagerTable() { VSProcess process = getSelectedProcess(); boolean allProcesses = process == null; + taskManagerLocalModel.set(process, - VSTaskManagerTableModel.LOCAL, allProcesses); + VSTaskManagerTableModel.LOCAL, + allProcesses); + taskManagerGlobalModel.set(process, - VSTaskManagerTableModel.GLOBAL, allProcesses); + VSTaskManagerTableModel.GLOBAL, + allProcesses); } /** @@ -1216,50 +1221,31 @@ public class VSSimulator extends JPanel implements Serializable { return prefs; } - /** - * Write object. - * - * @param objectOutputStream the object output stream - * - * @throws IOException Signals that an I/O exception has occurred. + /* (non-Javadoc) + * @see serialize.VSSerializable#serialize(serialize.VSSerialize, + * java.io.ObjectOutputStream) */ - public synchronized void writeObject(ObjectOutputStream objectOutputStream) + public synchronized void serialize(VSSerialize serialize, + ObjectOutputStream objectOutputStream) throws IOException { - objectOutputStream.writeObject(prefs); - objectOutputStream.writeObject(simulatorCanvas); + simulatorCanvas.serialize(serialize, objectOutputStream); } - /** - * 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 + /* (non-Javadoc) + * @see serialize.VSSerializable#deserialize(serialize.VSSerialize, + * java.io.ObjectInputStream) */ @SuppressWarnings("unchecked") - public synchronized void readObject(ObjectInputStream objectInputStream) + public synchronized void deserialize(VSSerialize serialize, + ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException { - if (VSDeserializationHelper.DEBUG) + if (VSSerialize.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); + serialize.setObject("simulator", this); + serialize.setObject("logging", logging); + simulatorCanvas.deserialize(serialize, objectInputStream); - this.simulatorCanvas = - (VSSimulatorCanvas) objectInputStream.readObject(); - VSDeserializationHelper.setObject("simulatorCanvas", simulatorCanvas); - - init(prefs, simulatorFrame); + updateTaskManagerTable(); } } diff --git a/sources/simulator/VSSimulatorCanvas.java b/sources/simulator/VSSimulatorCanvas.java index d6bc5d2..cfc6549 100644 --- a/sources/simulator/VSSimulatorCanvas.java +++ b/sources/simulator/VSSimulatorCanvas.java @@ -37,6 +37,7 @@ import events.implementations.*; import events.internal.*; import prefs.*; import prefs.editors.*; +import serialize.*; import utils.*; /** @@ -49,7 +50,7 @@ import utils.*; * @author Paul C. Buetow */ public class VSSimulatorCanvas extends Canvas - implements Runnable, Serializable { + implements Runnable, VSSerializable { /** The serial version uid */ private static final long serialVersionUID = 1L; @@ -1545,47 +1546,45 @@ public class VSSimulatorCanvas extends Canvas } } - /** - * Write object. - * - * @param objectOutputStream the object output stream - * - * @throws IOException Signals that an I/O exception has occurred. + /* (non-Javadoc) + * @see serialize.VSSerializable#serialize(serialize.VSSerialize, + * java.io.ObjectOutputStream) */ - public synchronized void writeObject(ObjectOutputStream objectOutputStream) + public synchronized void serialize(VSSerialize serialize, + ObjectOutputStream objectOutputStream) throws IOException { synchronized (processes) { - objectOutputStream.writeObject(processes); + objectOutputStream.writeObject(new Integer(numProcesses)); + for (VSProcess process : processes) + process.serialize(serialize, objectOutputStream); } - objectOutputStream.writeObject(taskManager); + + taskManager.serialize(serialize, objectOutputStream); } - /** - * 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 + /* (non-Javadoc) + * @see serialize.VSSerializable#deserialize(serialize.VSSerialize, + * java.io.ObjectInputStream) */ @SuppressWarnings("unchecked") - public synchronized void readObject(ObjectInputStream objectInputStream) + public synchronized void deserialize(VSSerialize serialize, + ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException { - if (VSDeserializationHelper.DEBUG) + if (VSSerialize.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); + synchronized (processes) { + numProcesses = ((Integer) + objectInputStream.readObject()).intValue(); + + processes.clear(); + for (int i = 0; i < numProcesses; ++i) { + VSProcess process = createProcess(i); + process.deserialize(serialize, objectInputStream); + processes.add(process); + } + } - init(prefs, simulator, logging); + taskManager.deserialize(serialize, objectInputStream); } } diff --git a/sources/simulator/VSSimulatorFrame.java b/sources/simulator/VSSimulatorFrame.java index f5ef5af..aff8787 100644 --- a/sources/simulator/VSSimulatorFrame.java +++ b/sources/simulator/VSSimulatorFrame.java @@ -33,6 +33,7 @@ import javax.swing.event.*; import core.*; import prefs.*; import prefs.editors.*; +import serialize.*; import utils.*; /** @@ -42,7 +43,7 @@ import utils.*; * * @author Paul C. Buetow */ -public class VSSimulatorFrame extends VSFrame implements Serializable { +public class VSSimulatorFrame extends VSFrame { /** The serial version uid */ private static final long serialVersionUID = 1L; @@ -143,6 +144,22 @@ public class VSSimulatorFrame extends VSFrame implements Serializable { dispose(); } else if (sourceText.equals( + finalPrefs.getString("lang.open"))) { + + VSSerialize serialize = new VSSerialize(); + VSSimulator simulator = serialize.openSimulator( + VSSerialize.STANDARD_FILENAME, + VSSimulatorFrame.this); + addSimulator(simulator); + + } else if (sourceText.equals( + finalPrefs.getString("lang.save"))) { + + VSSerialize serialize = new VSSerialize(); + serialize.saveSimulator(VSSerialize.STANDARD_FILENAME, + currentSimulator); + + } else if (sourceText.equals( finalPrefs.getString("lang.about"))) { new VSAbout(finalPrefs, VSSimulatorFrame.this); @@ -253,6 +270,28 @@ public class VSSimulatorFrame extends VSFrame implements Serializable { menuItem.addActionListener(actionListener); menuFile.add(menuItem); + menuFile.addSeparator(); + + menuItem = new JMenuItem(prefs.getString("lang.open")); + menuItem.setAccelerator(KeyStroke.getKeyStroke( + prefs.getInteger("keyevent.open"), + ActionEvent.ALT_MASK)); + menuItem.addActionListener(actionListener); + menuFile.add(menuItem); + + menuItem = new JMenuItem(prefs.getString("lang.save")); + menuItem.setAccelerator(KeyStroke.getKeyStroke( + prefs.getInteger("keyevent.save"), + ActionEvent.ALT_MASK)); + menuItem.addActionListener(actionListener); + menuFile.add(menuItem); + + menuItem = new JMenuItem(prefs.getString("lang.saveas")); + menuItem.setAccelerator(KeyStroke.getKeyStroke( + prefs.getInteger("keyevent.saveas"), + ActionEvent.ALT_MASK)); + menuItem.addActionListener(actionListener); + menuFile.add(menuItem); menuFile.addSeparator(); @@ -508,39 +547,4 @@ public class VSSimulatorFrame extends VSFrame implements Serializable { 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); - } } diff --git a/sources/utils/VSSerialize.java b/sources/utils/VSSerialize.java deleted file mode 100644 index b3bd18b..0000000 --- a/sources/utils/VSSerialize.java +++ /dev/null @@ -1,171 +0,0 @@ -/* - * 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 - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - * All icons of the icons/ folder are under a Creative Commons - * Attribution-Noncommercial-Share Alike License a CC-by-nc-sa. - * - * The icon's homepage is http://code.google.com/p/ultimate-gnome/ - */ - -package utils; - -import java.io.*; -import java.util.HashMap; - -import simulator.VSSimulator; - -/** - * The class VSSerialize, this static class helps do deserialize - * a saved simulator! - * - * @author Paul C. Buetow - */ -public final class VSSerialize { - /** The serial version uid */ - private static final long serialVersionUID = 1L; - - /** 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"; - - /** For temp object storage */ - private static HashMap<String,Object> objects; - - /** - * Creates the VSSerialize object. - */ - public VSSerialize() { - init(); - } - - /** - * Initializes the helper. - */ - private void init() { - objects = new HashMap<String,Object>(); - } - - /** - * Sets an object. - * - * @param key The object key - * @param object The object itself - */ - public void setObject(String key, Object object) { - objects.put(key, object); - } - - /** - * Sets an object. - * - * @param num The object number - * @param key The object key - * @param object The object itself - */ - public void setObject(int num, String key, Object object) { - objects.put(key + ":" + num, object); - } - - /** - * Gets an object. - * - * @param num The object number - * @param key The object key - * - * @return The object itself - */ - public Object getObject(int num, String key) { - Object object = objects.get(key + ":" + num); - - if (object == null) { - System.err.println("No such deserialization helper key " - + key + ":" + num); - System.exit(1); - } - - return object; - } - - /** - * Gets an object. - * - * @param key The object key - * - * @return The object itself - */ - public Object getObject(String key) { - Object object = objects.get(key); - - if (object == null) { - System.err.println("No such deserialization helper key " + key); - System.exit(1); - } - - return object; - } - - /** - * Saves the given simulator to the given filename. - * - * @param filename The filename - * @param simulator The simulator - */ - public void saveSimulator(String filename, VSSimulator simulator) { - try { - FileOutputStream fileOutputStream = - new FileOutputStream(filename); - ObjectOutputStream objectOutputStream = - new ObjectOutputStream(fileOutputStream); - - objectOutputStream.writeObject(simulator.getPrefs()); - simulator.serialize(this); - - } catch (IOException e) { - e.printStackTrace(); - } - } - - /** - * Opens a simulator from the given filename. - * - * @param filename The filename. - * @param simulatorFrame The simulator frame - * - * @return The simulator object, and null if no success - */ - public VSSimulator openSimulator(String filename, - VSSimulatorFrame simulatorFrame) { - - try { - FileInputStream fileInputStream = - new FileInputStream(filename); - ObjectInputStream objectInputStream = - new ObjectInputStream(fileInputStream); - - VSPrefs prefs = (VSPrefs) objectInputStream.readObject(); - simulator = new VSSimulator(prefs, simulatorFrame); - simulator.deserialize(this); - - } catch (Exception e) { - e.printStackTrace(); - } - - return simulator; - } -} |
