How to work with the operation stack


Summary The EngineEditor singleton can create edit operations in an ArcGIS Engine application. Undo and redo capabilities are available by accessing the operation stack associated with a ToolbarControl that has been linked with the EngineEditor.

In this topic


Overview

Edit sessions, managed by the EngineEditor, requiring undo/redo capabilities must contain a ToolbarControl.  The IToolbarControl.OperationStack property provides access to the operation stack which stores graphic, edit and sketch operations. The IOperationStack.Undo and IOperationStack.Redo methods can then be used to sequentially undo and redo operations respectively.

Setup Steps

Edit and sketch operations will only be added to the operation stack after performing the following steps:
  1. Add a ToolbarControl to the application
  2. Instantiate the ControlsOperationStack class and associate it with the ToolbarControl.
  3. Link the EngineEditor to the ToolbarControl
  4. Start and edit session and enable undo/redo capabilities
1.  Add a ToolbarControl to the application
Add a ToolbarControl and buddy it with a MapControl.  The ToolbarControl can be hidden by setting the Visible property to False.
2. Instantiate the ControlsOperationStack class and associate it with the ToolbarControl.
Add the following code to the Form_Load event:
[Java]
IOperationStack operationStack = new ControlsOperationStack();
toolbarControl1.setOperationStackByRef(operationStack);
toolbarControl2.setOperationStackByRef(operationStack);
This step can be omitted for applications containing one MapControl and one ToolbarControl.  Applications containing a single MapControl buddied with multiple ToolbarControls should perform this step to ensure that operations will be added to a single operation stack. 
3.  Link the EngineEditor to the ToolbarControl
The EngineEditor must be linked to the ToolbarControl so that the operation stack contains edit and sketch operations. Do this either by adding a command from the ‘Feature Editing’ toolset to the ToolbarControl or by programmatically linking the EngineEditor to the ToolbarControl :
[Java]
Object tbr = toolbarControl.getObject();
IExtension engineEditorExt = engineEditor;
engineEditorExt.startup(tbr);
Care should be taken if the application contains multiple ToobarControls buddied to different MapControls. In this situation the IExtension.Startup method should be re-executed each time editing switches to a different MapControl, ensuring that the appropriate ToolbarControl is passed in.
4.  Start and edit session and enable undo/redo capabilities
Start an edit session and set IEngineEditor.EnableUndoRedo to true. For more information on edit sessions refer to How to create an edit session.
Attempting to call IEngineEditor.EnableUndoRedo prior to starting an edit session will cause an error.  Within an edit session all operations on the operation stack will be removed if IEngineEditor.EnableUndoRedo is set to False.

Using the operation stack

Edit operations will be added to the operation stack when the IEngineEditor.StopOperation method is called and sketch operations will be added when the IEngineSketchOperation.Finish method is called. See How to create an edit session and How to create a sketch operation for more information.
Sequentially undo or redo operations on the operation stack as follows:
[Java]
IOperationStack operationStack = toolbarControl.getOperationStack();
// To undo the previous operation on the stack
operationStack.undo();
// To redo the next operation on the stack
operationStack.redo();
The Undo and Redo methods do not remove operations from the operation stack, but rather move an internal pointer up and down the stack.
Individual operations can be accessed using the IOperationStack.Item property and then performed using IOperationStack.Do, IOperationStack.UndoOperation or IOperationStack.RedoOperation methods but this is not recommended for edit and sketch operations.


See Also:

How to create an edit session
How to create a sketch operation
How to listen to an edit event
Sample:Editing Custom Application




Additional Requirements
  • The code assumes that an instance of EngineEditor has been created at the class level:
  • IEngineEditor engineEditor = new EngineEditor();

Development licensing Deployment licensing
Engine Developer Kit Engine Runtime
ArcView
ArcEditor
ArcInfo