capint
11/21/2016 - 5:36 PM

Eclipse >> Undoable operation

Eclipse >> Undoable operation

//*** Perfom undo on a context
IOperationHistory operationHistory = workbench.getOperationSupport().getOperationHistory();
try {
	IStatus status = operationHistory.undo(myContext, progressMonitor, someInfo);
} catch (ExecutionException e) {
	// handle the exception 
}
//*** Undo Context describes the user context in which the original operation was performed
//*** Used to filter the history for a specific point of view from the Operation History
public Object execute(ExecutionEvent event) throws ExecutionException {
  IUndoableOperation operation = new DeltaInfoOperation(
                  editor.getSite().getShell());
  ...
  IWorkbench workbench = editor.getSite().getWorkbenchWindow().getWorkbench();
  IOperationHistory operationHistory = workbench.getOperationSupport().getOperationHistory();
  IUndoContext undoContext = workbench.getOperationSupport().getUndoContext();
  //*** Assign an undo context to an operation
  operation.addContext(undoContext);
  operationHistory.execute(operation, null, null);
  return null;
}
//*** Source: http://help.eclipse.org/neon/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Fguide%2FwrkAdv_undo.htm
//*** An operation can be made available for undo and redo by extending AbstractOperation (which implements IUndoableOperation)
class DeltaInfoOperation extends AbstractOperation {
	Shell shell;
	public DeltaInfoOperation(Shell shell) {
		super("Delta Operation");
		this.shell = shell;
	}
	public IStatus execute(IProgressMonitor monitor, IAdaptable info) {
        // Build the string buffer "buf"
        MessageDialog.openInformation(shell,
                        ContributionMessages.DeltaInfoHandler_shellTitle, buf
                                        .toString());
		return Status.OK_STATUS;
	}
	public IStatus undo(IProgressMonitor monitor, IAdaptable info) {
        // Build the string buffer "buf"
        MessageDialog.openInformation(shell,
                        ContributionMessages.DeltaInfoHandler_shellTitle,
                        "Undoing delta calculation");
		return Status.OK_STATUS;
	}
	public IStatus redo(IProgressMonitor monitor, IAdaptable info) {
        // Build the string buffer "buf"
        // simply re-calculate the delta
        MessageDialog.openInformation(shell,
                        ContributionMessages.DeltaInfoHandler_shellTitle, buf
                                        .toString());
		return Status.OK_STATUS;
	}
}
//*** The Operation Hisory keeps track of all of the undoable operations
//*** 2 ways to obtain OperationHistory:
IOperationHistory operationHistory = OperationHistoryFactory.getOperationHistory();
//*** Or
IWorkbench workbench = editor.getSite().getWorkbenchWindow().getWorkbench();
IOperationHistory operationHistory = workbench.getOperationSupport().getOperationHistory();