Eclipse GEF
2.1

org.eclipse.gef.commands
Class CommandStack

java.lang.Object
  |
  +--org.eclipse.gef.commands.CommandStack

public class CommandStack
extends Object

An implementation of a command stack. A command stack manages the executing, undoing, and redoing of Commands. Executed commands are pushed onto an undo stack. Commands that are undone are pushed onto a Redo stack. Whenever a new command is executed, the Redo stack is flushed.

A CommandStack contains a dirty property. This property can be used to determine when persisting the Commands' changes is required. The stack is dirty whenever the last executed or redone command is different than the command that was at the top of the undo stack when markSaveLocation() was last called. Upon construction, the undo stack is empty, and is not dirty.


Field Summary
protected  List listeners
          the list of CommandStackListeners
 
Constructor Summary
CommandStack()
          Constructs a command stack.
 
Method Summary
 void addCommandStackListener(CommandStackListener listener)
          Adds the specified listener.
 boolean canRedo()
           
 boolean canUndo()
           
 void dispose()
          This will dispose() all the commands in both the undo and redo stack.
 void execute(Command command)
          Executes the specified Command if it is executable.
 void flush()
          Flushes the entire stack and resets the save location to zero.
 Object[] getCommands()
           
 Command getRedoCommand()
          Peeks at the top of the redo stack.
 Command getUndoCommand()
          Peeks at the top of the undo stack.
 int getUndoLimit()
          Returns the undo limit.
 boolean isDirty()
          Returns true if the stack is dirty.
 void markSaveLocation()
          Marks the last executed or redone Command as the point at which the changes were saved.
protected  void notifyListeners()
          Sends notification to all CommandStackListeners.
 void redo()
          Calls redo on the Command at the top of the redo stack, and pushes that Command onto the undo stack.
 void removeCommandStackListener(CommandStackListener listener)
          Removes the first occurance of the specified listener.
 void setUndoLimit(int undoLimit)
          Sets the undo limit.
 void undo()
          Undoes the most recently executed (or redone) Command.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

listeners

protected List listeners
the list of CommandStackListeners

Constructor Detail

CommandStack

public CommandStack()
Constructs a command stack. By default, there is no undo limit.

Method Detail

addCommandStackListener

public void addCommandStackListener(CommandStackListener listener)
Adds the specified listener. Multiple adds will result in multiple notifications.

Parameters:
listener - the listener

canRedo

public boolean canRedo()
Returns:
true if it is appropriate to call redo().

canUndo

public boolean canUndo()
Returns:
true if undo() can be called

execute

public void execute(Command command)
Executes the specified Command if it is executable. Flushes the redo stack.

Parameters:
command - the Command to execute

dispose

public void dispose()
This will dispose() all the commands in both the undo and redo stack. Both stacks will be empty afterwards.


flush

public void flush()
Flushes the entire stack and resets the save location to zero. This method might be called when performing "revert to saved".


getCommands

public Object[] getCommands()
Returns:
an array containing all commands in the order they were executed

getRedoCommand

public Command getRedoCommand()
Peeks at the top of the redo stack. This is useful for describing to the User what will be redone. The returned Command has a label describing it.

Returns:
the top of the redo stack, which may be null

getUndoCommand

public Command getUndoCommand()
Peeks at the top of the undo stack. This is useful for describing to the User what will be undone. The returned Command has a label describing it.

Returns:
the top of the undo stack, which may be null

getUndoLimit

public int getUndoLimit()
Returns the undo limit. The undo limit is the maximum number of atomic operations that the User can undo. -1 is used to indicate no limit.

Returns:
the undo limit

isDirty

public boolean isDirty()
Returns true if the stack is dirty. The stack is dirty whenever the last executed or redone command is different than the command that was at the top of the undo stack when markSaveLocation() was last called.

Returns:
true if the stack is dirty

markSaveLocation

public void markSaveLocation()
Marks the last executed or redone Command as the point at which the changes were saved. Calculation of isDirty() will be based on this checkpoint.


notifyListeners

protected void notifyListeners()
Sends notification to all CommandStackListeners.


redo

public void redo()
Calls redo on the Command at the top of the redo stack, and pushes that Command onto the undo stack. This method should only be called when canUndo() returns true.


removeCommandStackListener

public void removeCommandStackListener(CommandStackListener listener)
Removes the first occurance of the specified listener.

Parameters:
listener - the listener

setUndoLimit

public void setUndoLimit(int undoLimit)
Sets the undo limit. The undo limit is the maximum number of atomic operations that the User can undo. -1 is used to indicate no limit.

Parameters:
undoLimit - the undo limit

undo

public void undo()
Undoes the most recently executed (or redone) Command. The Command is popped from the undo stack to and pushed onto the redo stack. This method should only be called when canUndo() returns true.


Eclipse GEF
2.1

Copyright (c) IBM Corp. and others 2000, 2003. All Rights Reserved.