[Bprocessor-commit] model/src/net/sourceforge/bprocessor/model History.java,NONE,1.1
Status: Pre-Alpha
Brought to you by:
henryml
From: Michael L. <he...@us...> - 2006-01-11 10:20:11
|
Update of /cvsroot/bprocessor/model/src/net/sourceforge/bprocessor/model In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv7150/src/net/sourceforge/bprocessor/model Added Files: History.java Log Message: History of commands for handling undo/redo --- NEW FILE: History.java --- //--------------------------------------------------------------------------------- // $Id: History.java,v 1.1 2006/01/11 10:19:58 henryml Exp $ // // Copyright (c) 2005 The BProcessor Team (http://bprocessor.sourceforge.net) // Released under the Lesser GNU Public License v2.1 //--------------------------------------------------------------------------------- package net.sourceforge.bprocessor.model; import java.util.Stack; /** * The History for containing commands for undo and redo support */ public class History { /** The undo stack */ private Stack undoStack; /** The redo stack */ private Stack redoStack; /** A singular history instance */ private static History instance = new History(); /** * Return the singular history instance * @return The history */ public static History getInstance() { return instance; } /** * Constructor for History */ public History() { undoStack = new Stack(); redoStack = new Stack(); } /** * Apply a command, clear the redo stack and * push the new command on the undo stack. * @param command The command to apply */ public void apply(Command command) { command.apply(); redoStack.clear(); undoStack.push(command); } /** * Undo the command on top of undo stack and push * it on the redo stack */ public void undo() { if (!undoStack.isEmpty()) { Command top = (Command) undoStack.pop(); top.undo(); redoStack.push(top); } } /** * Redo the command on top of redo stack and push * it on the undo stack */ public void redo() { if (!redoStack.isEmpty()) { Command top = (Command) redoStack.pop(); top.redo(); undoStack.push(top); } } /** * Return the top of the undo stack or null if empty. * @return The top of the undo stack */ public Command undoTop() { if (!undoStack.isEmpty()) { return (Command) undoStack.peek(); } else { return null; } } /** * Return the top of the redo stack or null if empty. * @return The top of the redo stack */ public Command redoTop() { if (!redoStack.isEmpty()) { return (Command) redoStack.peek(); } else { return null; } } /** * Clear this History */ public void clear() { undoStack.clear(); redoStack.clear(); } } |