Undoable version of the process manager based on the JCommands framework.

JAVA PRIMER
JCommands: A Flexible Undo Framework for Java

Andreas Bäcker
Listing 3. Undoable version of the process manager based on the JCommands framework.


class ProcessManager extends java.util.Observable {

	// The notification message identifiers
	public static final int ADDED   = 0;
	public static final int REMOVED = 1;

	// The collection of process descriptors
	private Vector processes;

	// Returns a command that adds a process
	// descriptor and notifies observers
	public Command makeAddProcessDesc(ProcessDesc p) {
		Notification nc = new Notification(this);
		nc.setArgs(ADDED, REMOVED, p);
		return new VectorAppend("Add Process", processes, p, nc);
	}

	// Returns a command that removes a process
	// descriptor and notifies observers
	public Command makeRemoveProcessDesc(ProcessDesc p) {
		Notification nc = new Notification(this);
		nc.setArgs(REMOVED, ADDED, p);
		return new VectorRemove("Delete Process", processes, p, nc);
	}

	// Returns a command that removes a
	// collection of process descriptors
	public Command makeRemoveManyProcesses(Collection c) {
		String msg = "Remove" + c.size() + "Processes";
		MacroCommand m = new MacroCommand(msg);
		Iterator it = c.iterator();
		while(it.hasNext())
			m.addLast(makeRemoveProcessDesc((ProcessDesc)it.next());
		return mc;
	}
}