I implemented an undo/redo mechanism following a tutorial and some discussion on the forums.
My solution is working fine if I have one document open: my operations can be executed and undone/redone properly, everything working as expected. As soon as I open a second editor and switch back to the first editor, all my operations are lost. If I now execute an operation in my second editor and switch back to my first editor, I can undo this operation even if the focus is on the first editor.
So, it seems that switching to another editor doesn't change the "active" context in the OperationHistory.
My code is as follow:
Plugin.xml
<extension
point="org.eclipse.ui.menus">
<menuContribution
locationURI="menu:org.eclipse.ui.main.menu">
<menu
label="Edit">
<command
commandId="org.eclipse.ui.edit.undo"
label="Undo"
style="push">
</command>
<command
commandId="org.eclipse.ui.edit.redo"
label="Redo"
style="push">
</command>
</menu>
</menuContribution>
ApplicationActionBarAdvisor.java
@Override
protected void makeActions(IWorkbenchWindow window) {
register(ActionFactory.UNDO.create(window));
register(ActionFactory.REDO.create(window));
}
Editor class (createGlobalActionHandlers is called in my init method):
protected void createGlobalActionHandlers() {
undoContext = new ObjectUndoContext(this);
undoRedoActionGroup = new UndoRedoActionGroup(this.getSite(),
undoContext, true);
IActionBars actionBars = getEditorSite().getActionBars();
undoRedoActionGroup.fillActionBars(actionBars);
}
public void executeOperation(IUndoableOperation operation) {
try {
operation.addContext(undoContext);
OperationHistoryFactory.getOperationHistory().execute(operation,
null, null);
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Do I need to do something special in the setFocus (and eventually dispose) method in order to notify the OperationHistory that the context changed ? I though this was done automatically by the framework.