Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Undo/Redo not working properly with multiple documents
Undo/Redo not working properly with multiple documents [message #760364] Fri, 02 December 2011 02:53 Go to next message
Cedric Moonen is currently offline Cedric Moonen
Messages: 269
Registered: August 2009
Senior Member
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.
Re: Undo/Redo not working properly with multiple documents [message #760433 is a reply to message #760364] Fri, 02 December 2011 06:13 Go to previous messageGo to next message
Cedric Moonen is currently offline Cedric Moonen
Messages: 269
Registered: August 2009
Senior Member
I finally found a working solution: in the editor setFocus method, I add the following code:
	@Override
	public void setFocus() {
		undoRedoActionGroup.fillActionBars(getEditorSite().getActionBars());
	}
Re: Undo/Redo not working properly with multiple documents [message #761445 is a reply to message #760433] Tue, 06 December 2011 08:41 Go to previous message
Paul Webster is currently offline Paul Webster
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Just as an aside, you should not do anything in the part setFocus() method other than determine which control gets focus. It is called if you click on a part tab, not if you click in a control in the part.

PW


Previous Topic:How to use 'ASTParser' on RCP
Next Topic: How to control the minimized view docking place?
Goto Forum:
  


Current Time: Sat Jul 26 05:13:36 EDT 2014

Powered by FUDForum. Page generated in 0.01603 seconds