Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Undo/Redo with 3.4 Commands
Undo/Redo with 3.4 Commands [message #480056] Thu, 13 August 2009 15:32 Go to next message
Rob is currently offline RobFriend
Messages: 27
Registered: July 2009
Junior Member
Hi all,

I used to have an old 3.2 RCP app. I had created my own type of editor,
which used the workbench operation history like so:

operationHistory =
editor.getEditorSite().getWorkbenchWindow().getWorkbench().g etOperationSupport().getOperationHistory();

In the apps ApplicationActionBarAdvisor, I used the undo and redo actions
like so:
undoAction = ActionFactory.UNDO.create(window);
register(undoAction);


Now, when we upgraded to 3.4 and the new menu/command/handler mechanism,
i'm adding the commands to the menu bar as follows (note I dont have
handlers for them)
CommandContributionItemParameter param = new
CommandContributionItemParameter(window, "undo",
"org.eclipse.ui.edit.undo", SWT.NONE);
CommandContributionItem undo = new CommandContributionItem(param);

Unfortunatley, the undo/redo commands don't work anymore. They aren't
updated when my editor operation history changes. Is there anything more I
need to do, to link the commands to the workbench operation history? Do
they not have default handlers to track the operation history?

I've googled my ass off over this, but i'm still not getting anywhere. Any
help would be greatly appreciated!

Thanks.
Re: Undo/Redo with 3.4 Commands [message #480094 is a reply to message #480056] Thu, 13 August 2009 19:14 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: phillipus.nowhere.net

Rob wrote:
> Hi all,
>
> I used to have an old 3.2 RCP app. I had created my own type of editor,
> which used the workbench operation history like so:
>
> operationHistory =
> editor.getEditorSite().getWorkbenchWindow().getWorkbench().g etOperationSupport().getOperationHistory();
>
>
> In the apps ApplicationActionBarAdvisor, I used the undo and redo
> actions like so:
> undoAction = ActionFactory.UNDO.create(window);
> register(undoAction);
>
>
> Now, when we upgraded to 3.4 and the new menu/command/handler mechanism,
> i'm adding the commands to the menu bar as follows (note I dont have
> handlers for them)
> CommandContributionItemParameter param = new
> CommandContributionItemParameter(window, "undo",
> "org.eclipse.ui.edit.undo", SWT.NONE);
> CommandContributionItem undo = new CommandContributionItem(param);
>
> Unfortunatley, the undo/redo commands don't work anymore. They aren't
> updated when my editor operation history changes. Is there anything more
> I need to do, to link the commands to the workbench operation history?
> Do they not have default handlers to track the operation history?
>
> I've googled my ass off over this, but i'm still not getting anywhere.
> Any help would be greatly appreciated!
>
> Thanks.
>

I use Undo/Redo in 3.5 but on a View. I also have the same global menus
as you in the ApplicationActionBarAdvisor.

Here's some code that might point you in the right direction as I use it
on my view:

public class MyView extends ViewPart {

private UndoActionHandler fUndoActionHandler;
private RedoActionHandler fRedoActionHandler;
private IUndoContext fUndoContext;

@Override
public void createPartControl(Composite parent) {
initializeOperationHistory();

createGlobalActions();
registerGlobalActionHandlers();
}

private void createGlobalActions() {
// Undo / Redo
fUndoActionHandler = new UndoActionHandler(getViewSite(),
fUndoContext);
fRedoActionHandler = new RedoActionHandler(getViewSite(),
fUndoContext);
}

private void registerGlobalActionHandlers() {
IActionBars actionBars = getViewSite().getActionBars();

// Register the global menu actions
actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId() ,
fUndoActionHandler);
actionBars.setGlobalActionHandler(ActionFactory.REDO.getId() ,
fRedoActionHandler);
}

@Override
public void setFocus() {
}

/*
* Initialize the workbench operation history for our undo context.
*/
private void initializeOperationHistory() {
// Create a unique undo context to represent this editor's undo
history
fUndoContext = new ObjectUndoContext(this);

// Set the undo limit for this context (could put this in Prefs)
int limit = 500;
IOperationHistory history =
PlatformUI.getWorkbench().getOperationSupport().getOperation History();
history.setLimit(fUndoContext, limit);
}
}
Re: Undo/Redo with 3.4 Commands [message #480173 is a reply to message #480094] Fri, 14 August 2009 09:24 Go to previous messageGo to next message
Rob is currently offline RobFriend
Messages: 27
Registered: July 2009
Junior Member
Phillipus wrote:

> Rob wrote:
>> Hi all,
>>
>> I used to have an old 3.2 RCP app. I had created my own type of editor,
>> which used the workbench operation history like so:
>>
>> operationHistory =
>>
editor.getEditorSite().getWorkbenchWindow().getWorkbench().g etOperationSupport().getOperationHistory();
>>
>>
>> In the apps ApplicationActionBarAdvisor, I used the undo and redo
>> actions like so:
>> undoAction = ActionFactory.UNDO.create(window);
>> register(undoAction);
>>
>>
>> Now, when we upgraded to 3.4 and the new menu/command/handler mechanism,
>> i'm adding the commands to the menu bar as follows (note I dont have
>> handlers for them)
>> CommandContributionItemParameter param = new
>> CommandContributionItemParameter(window, "undo",
>> "org.eclipse.ui.edit.undo", SWT.NONE);
>> CommandContributionItem undo = new CommandContributionItem(param);
>>
>> Unfortunatley, the undo/redo commands don't work anymore. They aren't
>> updated when my editor operation history changes. Is there anything more
>> I need to do, to link the commands to the workbench operation history?
>> Do they not have default handlers to track the operation history?
>>
>> I've googled my ass off over this, but i'm still not getting anywhere.
>> Any help would be greatly appreciated!
>>
>> Thanks.
>>

> I use Undo/Redo in 3.5 but on a View. I also have the same global menus
> as you in the ApplicationActionBarAdvisor.

> Here's some code that might point you in the right direction as I use it
> on my view:

> public class MyView extends ViewPart {

> private UndoActionHandler fUndoActionHandler;
> private RedoActionHandler fRedoActionHandler;
> private IUndoContext fUndoContext;

> @Override
> public void createPartControl(Composite parent) {
> initializeOperationHistory();

> createGlobalActions();
> registerGlobalActionHandlers();
> }

> private void createGlobalActions() {
> // Undo / Redo
> fUndoActionHandler = new UndoActionHandler(getViewSite(),
> fUndoContext);
> fRedoActionHandler = new RedoActionHandler(getViewSite(),
> fUndoContext);
> }

> private void registerGlobalActionHandlers() {
> IActionBars actionBars = getViewSite().getActionBars();

> // Register the global menu actions
> actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId() ,
> fUndoActionHandler);
> actionBars.setGlobalActionHandler(ActionFactory.REDO.getId() ,
> fRedoActionHandler);
> }

> @Override
> public void setFocus() {
> }

> /*
> * Initialize the workbench operation history for our undo context.
> */
> private void initializeOperationHistory() {
> // Create a unique undo context to represent this editor's undo
> history
> fUndoContext = new ObjectUndoContext(this);

> // Set the undo limit for this context (could put this in Prefs)
> int limit = 500;
> IOperationHistory history =
> PlatformUI.getWorkbench().getOperationSupport().getOperation History();
> history.setLimit(fUndoContext, limit);
> }
> }

Thank for the reply! You seem to be using the old actions mechanism for
your menu items. Are you adding Actions to your menu bar? I am using the
new commands mechanism that arrived in 3.4 - so i'm adding
CommandContributionItems to my menu bar.

I also have text editors in my app, and strangely, my undo/redo commands
are working for the text editors, but not for my custom editors. Anyone
any ideas?

Thanks!
Re: Undo/Redo with 3.4 Commands [message #480559 is a reply to message #480173] Mon, 17 August 2009 15:45 Go to previous messageGo to next message
Rob is currently offline RobFriend
Messages: 27
Registered: July 2009
Junior Member
Rob wrote:

> Phillipus wrote:

>> Rob wrote:
>>> Hi all,
>>>
>>> I used to have an old 3.2 RCP app. I had created my own type of editor,
>>> which used the workbench operation history like so:
>>>
>>> operationHistory =
>>>
>
editor.getEditorSite().getWorkbenchWindow().getWorkbench().g etOperationSupport().getOperationHistory();
>>>
>>>
>>> In the apps ApplicationActionBarAdvisor, I used the undo and redo
>>> actions like so:
>>> undoAction = ActionFactory.UNDO.create(window);
>>> register(undoAction);
>>>
>>>
>>> Now, when we upgraded to 3.4 and the new menu/command/handler mechanism,
>>> i'm adding the commands to the menu bar as follows (note I dont have
>>> handlers for them)
>>> CommandContributionItemParameter param = new
>>> CommandContributionItemParameter(window, "undo",
>>> "org.eclipse.ui.edit.undo", SWT.NONE);
>>> CommandContributionItem undo = new CommandContributionItem(param);
>>>
>>> Unfortunatley, the undo/redo commands don't work anymore. They aren't
>>> updated when my editor operation history changes. Is there anything more
>>> I need to do, to link the commands to the workbench operation history?
>>> Do they not have default handlers to track the operation history?
>>>
>>> I've googled my ass off over this, but i'm still not getting anywhere.
>>> Any help would be greatly appreciated!
>>>
>>> Thanks.
>>>

>> I use Undo/Redo in 3.5 but on a View. I also have the same global menus
>> as you in the ApplicationActionBarAdvisor.

>> Here's some code that might point you in the right direction as I use it
>> on my view:

>> public class MyView extends ViewPart {

>> private UndoActionHandler fUndoActionHandler;
>> private RedoActionHandler fRedoActionHandler;
>> private IUndoContext fUndoContext;

>> @Override
>> public void createPartControl(Composite parent) {
>> initializeOperationHistory();

>> createGlobalActions();
>> registerGlobalActionHandlers();
>> }

>> private void createGlobalActions() {
>> // Undo / Redo
>> fUndoActionHandler = new UndoActionHandler(getViewSite(),
>> fUndoContext);
>> fRedoActionHandler = new RedoActionHandler(getViewSite(),
>> fUndoContext);
>> }

>> private void registerGlobalActionHandlers() {
>> IActionBars actionBars = getViewSite().getActionBars();

>> // Register the global menu actions
>> actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId() ,
>> fUndoActionHandler);
>> actionBars.setGlobalActionHandler(ActionFactory.REDO.getId() ,
>> fRedoActionHandler);
>> }

>> @Override
>> public void setFocus() {
>> }

>> /*
>> * Initialize the workbench operation history for our undo context.
>> */
>> private void initializeOperationHistory() {
>> // Create a unique undo context to represent this editor's undo
>> history
>> fUndoContext = new ObjectUndoContext(this);

>> // Set the undo limit for this context (could put this in Prefs)
>> int limit = 500;
>> IOperationHistory history =
>> PlatformUI.getWorkbench().getOperationSupport().getOperation History();
>> history.setLimit(fUndoContext, limit);
>> }
>> }

> Thank for the reply! You seem to be using the old actions mechanism for
> your menu items. Are you adding Actions to your menu bar? I am using the
> new commands mechanism that arrived in 3.4 - so i'm adding
> CommandContributionItems to my menu bar.

> I also have text editors in my app, and strangely, my undo/redo commands
> are working for the text editors, but not for my custom editors. Anyone
> any ideas?

> Thanks!
Still no progress made on this. Would anyone have any suggestions? I want
to keep undo/redo in my menu bar as command contributions and don't want
to revert back to Actions. It's still wierd that it works for a text
editor.

Rob
Re: Undo/Redo with 3.4 Commands [message #659225 is a reply to message #480559] Fri, 11 March 2011 15:25 Go to previous messageGo to next message
Mathieu Garcia is currently offline Mathieu GarciaFriend
Messages: 14
Registered: March 2011
Location: Tououse, France
Junior Member
Re up this topic as I'm facing the same trouble with Helios.
After debugging for a while, it seems the command is first associated to the specific handler I defined, but the ActionFactory handler overrides my handler and mine is never called again.

Anyway, something strange is the fact that copy/paste actions work perfectly with the command/handler framework.

If someone can help, I will appreciate.
Mathieu


And for information, there's not conflict between handlers according to the logs.

[Updated on: Fri, 11 March 2011 15:40]

Report message to a moderator

Re: Undo/Redo with 3.4 Commands [message #659241 is a reply to message #659225] Fri, 11 March 2011 15:56 Go to previous messageGo to next message
Mathieu Garcia is currently offline Mathieu GarciaFriend
Messages: 14
Registered: March 2011
Location: Tououse, France
Junior Member
Other results:

For demonstration, I'm working in a view.

I was about to do the stuff this way:

createPartControl(...) {
...
IAction undoAction = new Action() {
			@Override
			public void run() {
				//do the undo operation
                                new UndoHandler().execute(null);
			}
		};
		getViewSite().getActionBars().setGlobalActionHandler(ActionFactory.UNDO.getId(), undoAction);

}

This works but I'm not satistied as I need to write an IAction.

So, I tried the following:
createPartControl(...) {
...
		final IHandlerService handlerService = (IHandlerService) getSite().getService(IHandlerService.class);
	
		IAction undoAction = new Action() {
			@Override
			public void run() {
				try {
					handlerService.executeCommand("org.eclipse.ui.edit.undo", null);
				} catch (Exception e) {
					e.printStackTrace();
				} 
			}
		};
		getViewSite().getActionBars().setGlobalActionHandler(ActionFactory.UNDO.getId(), undoAction);

}


The result was a stackoverflow error as the retarget action recalls my action!

So, I tried:
createPartControl(...) {
...
	final IHandlerService handlerService = (IHandlerService) getSite().getService(IHandlerService.class);
	
		IAction undoAction = new Action() {
			@Override
			public void run() {
				try {
					handlerService.activateHandler("org.eclipse.ui.edit.undo", new UndoHandler());
					handlerService.executeCommand("org.eclipse.ui.edit.undo", null);
				} catch (Exception e) {
					e.printStackTrace();
				} 
			}
		};
		getViewSite().getActionBars().setGlobalActionHandler(ActionFactory.UNDO.getId(), undoAction);
}

This works !

So, just for the fun, I tried something simpler:

createPartControl(...) {
...
        final IHandlerService handlerService = (IHandlerService) getSite().getService(IHandlerService.class);
	handlerService.activateHandler("org.eclipse.ui.edit.undo", new UndoHandler());

}


I don't understand why but this works as well: the undo items in the menu contributions become available and work fine!

Re: Undo/Redo with 3.4 Commands [message #661418 is a reply to message #659241] Thu, 24 March 2011 15:23 Go to previous messageGo to next message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2150
Registered: July 2009
Senior Member
You may get more response if you ask on the eclipse.platform newsgroup, as
all of the types that you mention live above the level of swt.

Grant


"Mathieu Garcia" <matg22@gmail.com> wrote in message
news:ildg9s$1f9$1@news.eclipse.org...
> Other results:
>
> For demonstration, I'm working in a view.
>
> I was about to do the stuff this way:
>
>
> createPartControl(...) {
> ..
> IAction undoAction = new Action() {
> @Override
> public void run() {
> //do the undo operation
> new UndoHandler().execute(null);
> }
> };
> getViewSite().getActionBars().setGlobalActionHandler(ActionF actory.UNDO.getId(),
> undoAction);
>
> }
>
> This works but I'm not satistied as I need to write an IAction.
>
> So, I tried the following:
>
> createPartControl(...) {
> ..
> final IHandlerService handlerService = (IHandlerService)
> getSite().getService(IHandlerService.class);
>
> IAction undoAction = new Action() {
> @Override
> public void run() {
> try {
> handlerService.executeCommand("org.eclipse.ui.edit.undo", null);
> } catch (Exception e) {
> e.printStackTrace();
> } }
> };
> getViewSite().getActionBars().setGlobalActionHandler(ActionF actory.UNDO.getId(),
> undoAction);
>
> }
>
>
> The result was a stackoverflow error as the retarget action recalls my
> action!
>
> So, I tried:
> createPartControl(...) {
> ..
> final IHandlerService handlerService = (IHandlerService)
> getSite().getService(IHandlerService.class);
>
> IAction undoAction = new Action() {
> @Override
> public void run() {
> try {
> handlerService.activateHandler("org.eclipse.ui.edit.undo", new
> UndoHandler());
> handlerService.executeCommand("org.eclipse.ui.edit.undo", null);
> } catch (Exception e) {
> e.printStackTrace();
> } }
> };
> getViewSite().getActionBars().setGlobalActionHandler(ActionF actory.UNDO.getId(),
> undoAction);
> }
>
> This works !
>
> So, just for the fun, I tried something simpler:
>
>
> createPartControl(...) {
> ..
> final IHandlerService handlerService = (IHandlerService)
> getSite().getService(IHandlerService.class);
> handlerService.activateHandler("org.eclipse.ui.edit.undo", new
> UndoHandler());
>
> }
>
>
> I don't understand why but this works as well: the undo items in the menu
> contributions become available and work fine!
>
>
Re: Undo/Redo with 3.4 Commands [message #700406 is a reply to message #661418] Sat, 23 July 2011 11:07 Go to previous message
Philipp M. Fischer is currently offline Philipp M. FischerFriend
Messages: 67
Registered: November 2010
Location: Germany
Member
Hey Mathieu,

Thanks that you kept your answers and findings up to date. I am currently facing the same problem. I am not having issues with cut copy paste, but undo and redo is just not working. Same story with the save command. On which application is your RCP based. mine is based on the "org.eclipse.ui.ide.workbench".

http://www.eclipse.org/forums/index.php/t/222670/

Still i have no answers at all. Can you remeber where you found the part that your ahndlers were overwritten again with the actions?

Cheers

Phil
Previous Topic:ComboViewer with checkboxes, is it possible?
Next Topic:i have two issue need you address
Goto Forum:
  


Current Time: Thu Apr 18 10:50:05 GMT 2024

Powered by FUDForum. Page generated in 0.02773 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top