Package org.eclipse.gef.commands
Class CommandStack
java.lang.Object
org.eclipse.gef.commands.CommandStack
An implementation of a command stack. A stack manages the executing, undoing,
 and redoing of 
Commands. Executed commands are pushed onto a
 a stack for undoing later. Commands which 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 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.
 Initially, the undo stack is empty, and not dirty.
- 
Field SummaryFieldsModifier and TypeFieldDescriptionprotected List<CommandStackListener> Deprecated, for removal: This API element is subject to removal in a future version.static final intConstant indicating notification after a command has been executed.static final intConstant indicating notification after flushing the stack.static final intConstant indicating notification after marking the save location of the stack.static final intA bit-mask indicating notification after the command stack has changed.static final intConstant indicating notification after a command has been redone.static final intConstant indicating notification after a command has been undone.static final intConstant indicating notification prior to executing a command.static final intConstant indicating notification prior to flushing the stack.static final intConstant indicating notification prior to marking the save location of the stack.static final intA bit-mask indicating notification before the command stack is changed.static final intConstant indicating notification prior to redoing a command.static final intConstant indicating notification prior to undoing a command.
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionvoidAppends the listener to the list of command stack listeners.voidaddCommandStackListener(CommandStackListener listener) Deprecated, for removal: This API element is subject to removal in a future version.booleancanRedo()booleancanUndo()voiddispose()This willdispose()all the commands in both the undo and redo stack.voidExecutes the specified Command if possible.voidflush()Flushes the entire stack and resets the save location to zero.Object[]Peeks at the top of the redo stack.Peeks at the top of the undo stack.intReturns the undo limit.booleanisDirty()Returns true if the stack is dirty.voidMarks the last executed or redone Command as the point at which the changes were saved.protected voidDeprecated, for removal: This API element is subject to removal in a future version.UsenotifyListeners(Command, int)instead.protected voidnotifyListeners(Command command, int state) Notifies command stack event listeners that the command stack has changed to the specified state.voidredo()Calls redo on the Command at the top of the redo stack, and pushes that Command onto the undo stack.voidRemoves the first occurrence of the specified listener.voidDeprecated, for removal: This API element is subject to removal in a future version.UseCommandStackEventListenerinstead.voidsetUndoLimit(int undoLimit) Sets the undo limit.voidundo()Undoes the most recently executed (or redone) Command.
- 
Field Details- 
POST_EXECUTEpublic static final int POST_EXECUTEConstant indicating notification after a command has been executed.- See Also:
 
- 
POST_REDOpublic static final int POST_REDOConstant indicating notification after a command has been redone.- See Also:
 
- 
POST_UNDOpublic static final int POST_UNDOConstant indicating notification after a command has been undone.- See Also:
 
- 
POST_FLUSHpublic static final int POST_FLUSHConstant indicating notification after flushing the stack.- Since:
- 3.11
- See Also:
 
- 
POST_MARK_SAVEpublic static final int POST_MARK_SAVEConstant indicating notification after marking the save location of the stack.- Since:
- 3.11
- See Also:
 
- 
POST_MASKpublic static final int POST_MASKA bit-mask indicating notification after the command stack has changed. This includes after a command has been undone, redone, or executed, as well as after it has been flushed or the save location has been marked.Usage 
 if ((commandStackEvent.getDetail() & CommandStack.POST_MASK) != 0) { // Do something, like: stopBatchingChanges(); }- See Also:
 
- 
PRE_EXECUTEpublic static final int PRE_EXECUTEConstant indicating notification prior to executing a command.- See Also:
 
- 
PRE_REDOpublic static final int PRE_REDOConstant indicating notification prior to redoing a command.- See Also:
 
- 
PRE_UNDOpublic static final int PRE_UNDOConstant indicating notification prior to undoing a command.- See Also:
 
- 
PRE_FLUSHpublic static final int PRE_FLUSHConstant indicating notification prior to flushing the stack.- Since:
- 3.11
- See Also:
 
- 
PRE_MARK_SAVEpublic static final int PRE_MARK_SAVEConstant indicating notification prior to marking the save location of the stack.- Since:
- 3.11
- See Also:
 
- 
PRE_MASKpublic static final int PRE_MASKA bit-mask indicating notification before the command stack is changed. This includes before a command has been undone, redone, or executed, and before the stack is being flushed or the save location is marked.Usage 
 if ((commandStackEvent.getDetail() & CommandStack.PRE_MASK) != 0) { // Do something, like: startBatchingChanges(); }- Since:
- 3.7 Had package visibility before.
- See Also:
 
- 
listenersDeprecated, for removal: This API element is subject to removal in a future version.This field should not be referenced, usenotifyListeners(). This field will be removed after the 2027-03 release.The list ofCommandStackListeners.
 
- 
- 
Constructor Details- 
CommandStackpublic CommandStack()Constructs a new command stack. By default, there is no undo limit, and isDirty() will returnfalse.
 
- 
- 
Method Details- 
addCommandStackEventListenerAppends the listener to the list of command stack listeners. Multiple adds result in multiple notifications.- Parameters:
- listener- the event listener
- Since:
- 3.1
 
- 
addCommandStackListener@Deprecated(since="3.11", forRemoval=true) public void addCommandStackListener(CommandStackListener listener) Deprecated, for removal: This API element is subject to removal in a future version.UseaddCommandStackEventListener(CommandStackEventListener)instead. This method will be removed after the 2027-03 release.Appends the listener to the list of command stack listeners. Multiple adds will result in multiple notifications.- Parameters:
- listener- the listener
 
- 
canRedopublic boolean canRedo()- Returns:
- trueif it is appropriate to call- redo().
 
- 
canUndopublic boolean canUndo()- Returns:
- trueif- undo()can be called
 
- 
disposepublic void dispose()This willdispose()all the commands in both the undo and redo stack. Both stacks will be empty afterwards.
- 
executeExecutes the specified Command if possible. Prior to executing the command, a CommandStackEvent forPRE_EXECUTEwill be fired to event listeners. Similarly, after attempting to execute the command, an event forPOST_EXECUTEwill be fired. If the execution of the command completely normally, stack listeners will receivestackChangednotification.If the command is nullor cannot be executed, nothing happens.- Parameters:
- command- the Command to execute
- See Also:
 
- 
flushpublic void flush()Flushes the entire stack and resets the save location to zero. This method might be called when performing "revert to saved".
- 
getCommands- Returns:
- an array containing all commands in the order they were executed
 
- 
getRedoCommandPeeks at the top of the redo stack. This is useful for describing to the User what will be redone. The returnedCommandhas a label describing it.- Returns:
- the top of the redo stack, which may be null
 
- 
getUndoCommandPeeks at the top of the undo stack. This is useful for describing to the User what will be undone. The returnedCommandhas a label describing it.- Returns:
- the top of the undo stack, which may be null
 
- 
getUndoLimitpublic int getUndoLimit()Returns the undo limit. The undo limit is the maximum number of atomic operations that the User can undo.-1is used to indicate no limit.- Returns:
- the undo limit
 
- 
isDirtypublic 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 whenmarkSaveLocation()was last called.- Returns:
- trueif the stack is dirty
 
- 
markSaveLocationpublic void markSaveLocation()Marks the last executed or redone Command as the point at which the changes were saved. Calculation ofisDirty()will be based on this checkpoint.
- 
notifyListenersDeprecated, for removal: This API element is subject to removal in a future version.UsenotifyListeners(Command, int)instead. This method will be removed after the 2027-03 release.Sends notification to allCommandStackListeners.
- 
notifyListenersNotifies command stack event listeners that the command stack has changed to the specified state.- Parameters:
- command- the command
- state- the current stack state
- Since:
- 3.2
 
- 
redopublic 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 whencanUndo()returnstrue.
- 
removeCommandStackEventListenerRemoves the first occurrence of the specified listener.- Parameters:
- listener- the listener
 
- 
removeCommandStackListener@Deprecated(since="3.11", forRemoval=true) public void removeCommandStackListener(CommandStackListener listener) Deprecated, for removal: This API element is subject to removal in a future version.UseCommandStackEventListenerinstead. This method will be removed after the 2027-03 release.Removes the first occurrence of the specified listener.- Parameters:
- listener- the listener
 
- 
setUndoLimitpublic void setUndoLimit(int undoLimit) Sets the undo limit. The undo limit is the maximum number of atomic operations that the User can undo.-1is used to indicate no limit.- Parameters:
- undoLimit- the undo limit
 
- 
undopublic 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 whencanUndo()returnstrue.
 
- 
notifyListeners().