Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Undo/Redo in EMF based RCP application
Undo/Redo in EMF based RCP application [message #425784] Mon, 08 December 2008 17:20 Go to next message
Christian Hauser is currently offline Christian HauserFriend
Messages: 189
Registered: July 2009
Senior Member
Hello all,

I have a small Eclipse RCP application based on a rather simple EMF
model. I have registered the UndoActionHandler and RedoActionHandler to
have Edit -> Undo/Redo menu items (not yet enabled, though).

Before I used an EMF model I would have used an IUndoContext (e.g.
ObjectUndoContext) and an IOperationHistory that executed
IUndoableOperation(s).

Now using EMF I thought that that might (should) be simpler, couldn't
find any hint so far, though. Is there a tutorial or explanation for
dummies that could enlighten me on how to have undo/redo support within
my RCP application based on the EMF model. I'd like the undo/redo
operations operate on the EMF model.

I'm happy for any hint/link/help on this as I'm still struggling with it.

Thanks in advance,
Christian
Re: Undo/Redo in EMF based RCP application [message #425789 is a reply to message #425784] Mon, 08 December 2008 18:30 Go to previous messageGo to next message
J F is currently offline J FFriend
Messages: 256
Registered: July 2009
Senior Member
Hi,
Assuming you have tried the library example (e.g.
http://help.eclipse.org/ganymede/index.jsp?topic=/org.eclips e.emf.doc/references/overview/EMF.html
and go all the way to generating and runnning an editor), I assume you
want to know how to override undo/redo and where to look.

The generated editor will have generated an action bar contributor, e.g.
LibraryActionBarContributor which will inherit from
EditingDomainActionBarContributor which defines the Undo/Redo actions.

These simply call undo/redo on the editor's command stack. Undo/redo on
the editor's command stack simply call undo/redo on the command at the top
of the stack.

So if you were looking for an action contribution you won't find it (I
think - Ed will correct me) you need to override the command creation for
the ItemProvider in the .edit plugin generated for your model, or override
EditingDomainActionBarContributor in your generated editor's actionbar
contributor.

Hope this helps, and I have caught your intent...
Re: Undo/Redo in EMF based RCP application [message #425808 is a reply to message #425789] Tue, 09 December 2008 08:31 Go to previous messageGo to next message
Christian Hauser is currently offline Christian HauserFriend
Messages: 189
Registered: July 2009
Senior Member
Hi JF

Thank you for your reply. I've seen that
EditingDomainActioBarContributor offers me undo/redo actions. However,
I'm using a view, not an editor. Is it possible to use the
EditingDomainActionBarContributor within a view, too?

Christian


JF wrote:
> Hi,
> Assuming you have tried the library example (e.g.
> http://help.eclipse.org/ganymede/index.jsp?topic=/org.eclips e.emf.doc/references/overview/EMF.html
> and go all the way to generating and runnning an editor), I assume you
> want to know how to override undo/redo and where to look.
>
> The generated editor will have generated an action bar contributor,
> e.g. LibraryActionBarContributor which will inherit from
> EditingDomainActionBarContributor which defines the Undo/Redo actions.
> These simply call undo/redo on the editor's command stack. Undo/redo
> on the editor's command stack simply call undo/redo on the command at
> the top of the stack.
> So if you were looking for an action contribution you won't find it (I
> think - Ed will correct me) you need to override the command creation
> for the ItemProvider in the .edit plugin generated for your model, or
> override EditingDomainActionBarContributor in your generated editor's
> actionbar contributor.
>
> Hope this helps, and I have caught your intent...
>
Re: Undo/Redo in EMF based RCP application [message #425811 is a reply to message #425808] Tue, 09 December 2008 08:52 Go to previous messageGo to next message
Eike Stepper is currently offline Eike StepperFriend
Messages: 6682
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------040501040008050307060401
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

Christian,

It's really long ago that I did it but it's possible.
Have a look at createPages() in the editor:

| Tree tree = *new *Tree(getContainer(), SWT.MULTI);
selectionViewer = *new *TreeViewer(tree);
setCurrentViewer(selectionViewer);

selectionViewer.setContentProvider(*new *AdapterFactoryContentProvider(adapterFactory));
selectionViewer.setLabelProvider(*new *CDOLabelProvider(adapterFactory, view, selectionViewer));
selectionViewer.setInput(viewerInput);

*new *AdapterFactoryTreeEditor(selectionViewer.getTree(), adapterFactory);
createContextMenuFor(selectionViewer);|


I think this code is not strictly EditorPart related so it should work
more or less in a ViewPart, too.

Cheers
/Eike

----
http://thegordian.blogspot.com



Christian Hauser schrieb:
> Hi JF
>
> Thank you for your reply. I've seen that
> EditingDomainActioBarContributor offers me undo/redo actions. However,
> I'm using a view, not an editor. Is it possible to use the
> EditingDomainActionBarContributor within a view, too?
>
> Christian
>
>
> JF wrote:
>> Hi,
>> Assuming you have tried the library example (e.g.
>> http://help.eclipse.org/ganymede/index.jsp?topic=/org.eclips e.emf.doc/references/overview/EMF.html
>> and go all the way to generating and runnning an editor), I assume
>> you want to know how to override undo/redo and where to look.
>>
>> The generated editor will have generated an action bar contributor,
>> e.g. LibraryActionBarContributor which will inherit from
>> EditingDomainActionBarContributor which defines the Undo/Redo actions.
>> These simply call undo/redo on the editor's command stack. Undo/redo
>> on the editor's command stack simply call undo/redo on the command at
>> the top of the stack.
>> So if you were looking for an action contribution you won't find it
>> (I think - Ed will correct me) you need to override the command
>> creation for the ItemProvider in the .edit plugin generated for your
>> model, or override EditingDomainActionBarContributor in your
>> generated editor's actionbar contributor.
>>
>> Hope this helps, and I have caught your intent...
>>

--------------040501040008050307060401
Content-Type: text/html; charset=ISO-8859-15
Content-Transfer-Encoding: 8bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-15"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Christian,<br>
<br>
It's really long ago that I did it but it's possible.<br>
Have a look at createPages() in the editor:<br>
<br>
<title></title>
<style type="text/css">
<!--code { font-family: Courier New, Courier; font-size: 10pt; margin: 0px; }-->
</style>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<!-- ======================================================== -->
<!-- = Java Sourcecode to HTML automatically converted code = --><!-- = Java2Html Converter 5.0 [2006-02-26] by Markus Gebhard markus@jave.de = -->
<!-- = Further information: http://www.java2html.de = -->
<div class="java" align="left">
<table bgcolor="#ffffff" border="0" cellpadding="3" cellspacing="0">
<tbody>
<tr>
<!-- start source code --> <td align="left" nowrap="nowrap"
valign="top"> <code><font color="#ffffff">


Re: Undo/Redo in EMF based RCP application [message #425814 is a reply to message #425811] Tue, 09 December 2008 10:38 Go to previous messageGo to next message
J F is currently offline J FFriend
Messages: 256
Registered: July 2009
Senior Member
Although you can re-use the EMF ActionBarContributor you might find it
easier in the end not to. If you look at EditingDomainActionBarContributor
you will see it is derived from IEditorActionBarContributor and makes use
of the activation for the contributor defined by setActiveEditor.
If you are only supplying a ViewPart to the Workbench and are not worried
about the current Editor or Model, then it might be better to re-use the
actions from EMF and derive your own menu contributions - after all you
are not sensitive to the EditorPart lifecycle. Or indeed bipass the EMF
actions altogether...
Most of what EditingDomainActionBarContributor does using the editor part
requires it to implement ISelectionProvider and IEditingDomainProvider, so
if you implement those things you should be okay.
I only offer this advice as I had the same problem, long ago... Perhaps Ed
will give the definitive word?
Re: Undo/Redo in EMF based RCP application [message #425828 is a reply to message #425814] Tue, 09 December 2008 14:12 Go to previous messageGo to next message
Christian Hauser is currently offline Christian HauserFriend
Messages: 189
Registered: July 2009
Senior Member
Thank you JF and Eike for your help

I have a CommandStackListener registered (CommandStackListenerImpl).
However, the method commandStackChanged() is never called. (see code
snippet below)

resourceSet = new ResourceSetImpl();

// Register the appropriate resource factory to handle all file
extensions.

resourceSet.getResourceFactoryRegistry().getExtensionToFacto ryMap().put(
Resource.Factory.Registry.DEFAULT_EXTENSION, new
XMIResourceFactoryImpl());

// Register the package to ensure it is available during loading.
resourceSet.getPackageRegistry().put(ModelPackage.eNS_URI,
ModelPackage.eINSTANCE);

// Create an adapter factory that yields item providers.
ComposedAdapterFactory adapterFactory = new ComposedAdapterFactory(
ComposedAdapterFactory.Descriptor.Registry.INSTANCE);

adapterFactory.addAdapterFactory(new
ResourceItemProviderAdapterFactory());
adapterFactory.addAdapterFactory(new
ModelItemProviderAdapterFactory());
adapterFactory.addAdapterFactory(new
ReflectiveItemProviderAdapterFactory());

// Create the command stack that will notify this editor as
commands are executed.
BasicCommandStack commandStack = new BasicCommandStack();

// Create the editing domain with a special command stack.
editingDomain = new AdapterFactoryEditingDomain(adapterFactory,
commandStack, resourceSet);

editingDomain.getCommandStack().addCommandStackListener(new
CommandStackListenerImpl());


If someone could enlighten me on why my CommandStackListener is not
working, I'd be very happy.

Thanks in advance,
Christian


JF wrote:
> Although you can re-use the EMF ActionBarContributor you might find it
> easier in the end not to. If you look at
> EditingDomainActionBarContributor you will see it is derived from
> IEditorActionBarContributor and makes use of the activation for the
> contributor defined by setActiveEditor.
> If you are only supplying a ViewPart to the Workbench and are not
> worried about the current Editor or Model, then it might be better to
> re-use the actions from EMF and derive your own menu contributions -
> after all you are not sensitive to the EditorPart lifecycle. Or indeed
> bipass the EMF actions altogether...
> Most of what EditingDomainActionBarContributor does using the editor
> part requires it to implement ISelectionProvider and
> IEditingDomainProvider, so if you implement those things you should be
> okay.
> I only offer this advice as I had the same problem, long ago...
> Perhaps Ed will give the definitive word?
>
Re: Undo/Redo in EMF based RCP application [message #425834 is a reply to message #425828] Tue, 09 December 2008 16:21 Go to previous messageGo to next message
J F is currently offline J FFriend
Messages: 256
Registered: July 2009
Senior Member
The code that you have given looks fine.
However I would need more than that to be positive about why the listener
is not being called. Where do you intend your EMF Commands to be created
and passed to the CommandStack?

At a guess that this is something to do with EMF actions...
The code you have supplied is for an Editor. The Editor is passed to the
ActionBarContributor when the Editor is activated. The Actions have their
editingDomain, and hence their CommandStack set from that Editor.

Furthermore EMF global undo/redo etc actions are only added to the actual
Actions in eclipse via the ActionHandler mechanism, which is called from
the EditorActionBar.init function or from the shareGlobalActions function
if you are attaching them via a View such as an Outline or PropertySheet.
The shareGlobalActions function is in turn called from setActionBars on
the Page class, which you will have in your View? If you look at how the
generated Editor's ContentOutlinePage and PropertySheetPage handle these
things, you could incorporate this into your code, bearing in mind that
you need to be an EditorPart to use the EMF ActionBarContributor as is.

Again, I'm just guessing at what you are trying to do ...
Re: Undo/Redo in EMF based RCP application [message #429069 is a reply to message #425808] Tue, 07 April 2009 12:10 Go to previous messageGo to next message
Eirik Gr is currently offline Eirik GrFriend
Messages: 6
Registered: July 2009
Junior Member
Hello

I see that this topic was discussed some time ago, but it gave me some
valuable tips converting the generated editor using IWorkbenchPart (which
both IViewPart and IEditorPart is derived from) instead of IEditorPart.

In my setting, it was not suitable to let the editing of the model be the
central part in th perspective. My preference was to use it as a
supporting view - a kind of model explorer - for a central grapical editor
manipulating time series from simulation runs.

To get started I went through the listing from Eike Stepper as far back as
to July 2005 (
http://dev.eclipse.org/mhonarc/newsLists/news.eclipse.tools. emf/msg10973.html
), where he pointed out that he got a NPE in the EMF code. The NPE was
most probably caused by an almost empty command stack. In my case all the
standard commands (copy, paste, undo/redo and delete) enabled through the
ItemProviderAdapter (and org.eclipse.emf.edit.command) produced a NPE. The
Crate Child and Sibling actions are handled in the supported
ActionBarContributor class generated by the editor and worked as expected.

The reason seemed to be the code filling the command stack with Undo/Redo,
among others commands, was never executed. In the original generated
editor code, this happens in setActiveEditor which call activate and
deactivate and indirectly update. All these members are in class
EditingDomainActionbarContributor. Note that setActiveEditor is also
overriden in the supporting ActionbarContributer class generated by the
editor. This is the most central part where IEditorPart is is used, so
substituting setActiveEditor with e.g. setActivePart supporting IViewPart
(through IWorbenchPart) views instead of editors is rather straight
forward.

In the listing provuded by Eike Stepper from July 2005 this is exactly
what is done in the supporting ActionBarContributor class. The member
starts out as follows:

public void setActivePart(IWorkbenchPart part)
{
// TODO activeEditorPart == super.activeEditor ???
activeEditorPart = part;

// Switch to the new selection provider.
// ...

TODO is actually to copy the code from super.setActiveEditor as questioned
in the code snippet above and adopt it to a view. This gives the following
code:

public void setActivePart(IWorkbenchPart part) {
// nested if clause copied from super.activeEditor
if (part != activeWorkbenchPart) {
if (activeWorkbenchPart != null) {
deactivate();
}
if (part instanceof IEditingDomainProvider) {
activeWorkbenchPart = part;
activate();
}
}
// Switch to the new selection provider.
// ...

As we can see from the code, activate gets called. This is the place where
the command stack actions get enabled. So copying and overriding activate,
deactivate and update from EditingDomainActionbarContributor to the
generated supporting ActionbarContibutor class and adopting IEditorPart
dependent code to IWorkbencpart code (as in setActivePart), the actions
should be enabled, and the editor in the view should behave as expected.

Thanks again for given me a starting point for getting a view enabled
model editor.

As I started out, my belief is a that a model based tree editor will in
many applications only be a supporting view for a main editor. This leads
me to suggest a functionality in EMF supporting both IViewPart and
IEditorPart by using IWorkbenchPart. There are most probably challenges
that I don't see from my restricted knowledge of EMF. But runnung an
editor as a "model explorer" view (IViewPart) without to much changes in
code using the original design I permit myself to suggest that the
decisions makers (who probably don't read this) evaluate this as a future
functionality in EMF.

Thanks Eirik
Re: Undo/Redo in EMF based RCP application [message #429070 is a reply to message #429069] Tue, 07 April 2009 12:47 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Eirik,

A bugzilla feature request with patches or design suggestions for the
types of changes you'd like to see would be most welcome. Even just a
sample application showing what you did to make a view part work would
be an excellent contribution toward incorporating something reusable in
the base framework.


Eirik Grønsund wrote:
> Hello
> I see that this topic was discussed some time ago, but it gave me some
> valuable tips converting the generated editor using IWorkbenchPart
> (which both IViewPart and IEditorPart is derived from) instead of
> IEditorPart.
> In my setting, it was not suitable to let the editing of the model be
> the central part in th perspective. My preference was to use it as a
> supporting view - a kind of model explorer - for a central grapical
> editor manipulating time series from simulation runs.
>
> To get started I went through the listing from Eike Stepper as far
> back as to July 2005 (
> http://dev.eclipse.org/mhonarc/newsLists/news.eclipse.tools. emf/msg10973.html
> ), where he pointed out that he got a NPE in the EMF code. The NPE was
> most probably caused by an almost empty command stack. In my case all
> the standard commands (copy, paste, undo/redo and delete) enabled
> through the ItemProviderAdapter (and org.eclipse.emf.edit.command)
> produced a NPE. The Crate Child and Sibling actions are handled in the
> supported ActionBarContributor class generated by the editor and
> worked as expected.
> The reason seemed to be the code filling the command stack with
> Undo/Redo, among others commands, was never executed. In the original
> generated editor code, this happens in setActiveEditor which call
> activate and deactivate and indirectly update. All these members are
> in class EditingDomainActionbarContributor. Note that setActiveEditor
> is also overriden in the supporting ActionbarContributer class
> generated by the editor. This is the most central part where
> IEditorPart is is used, so substituting setActiveEditor with e.g.
> setActivePart supporting IViewPart (through IWorbenchPart) views
> instead of editors is rather straight forward.
>
> In the listing provuded by Eike Stepper from July 2005 this is exactly
> what is done in the supporting ActionBarContributor class. The member
> starts out as follows:
>
> public void setActivePart(IWorkbenchPart part)
> {
> // TODO activeEditorPart == super.activeEditor ???
> activeEditorPart = part;
>
> // Switch to the new selection provider.
> // ...
>
> TODO is actually to copy the code from super.setActiveEditor as
> questioned in the code snippet above and adopt it to a view. This
> gives the following code:
>
> public void setActivePart(IWorkbenchPart part) {
> // nested if clause copied from super.activeEditor
> if (part != activeWorkbenchPart) {
> if (activeWorkbenchPart != null) {
> deactivate();
> }
> if (part instanceof IEditingDomainProvider) {
> activeWorkbenchPart = part;
> activate();
> }
> }
> // Switch to the new selection provider.
> // ...
>
> As we can see from the code, activate gets called. This is the place
> where the command stack actions get enabled. So copying and overriding
> activate, deactivate and update from EditingDomainActionbarContributor
> to the generated supporting ActionbarContibutor class and adopting
> IEditorPart dependent code to IWorkbencpart code (as in
> setActivePart), the actions should be enabled, and the editor in the
> view should behave as expected.
> Thanks again for given me a starting point for getting a view enabled
> model editor.
>
> As I started out, my belief is a that a model based tree editor will
> in many applications only be a supporting view for a main editor. This
> leads me to suggest a functionality in EMF supporting both IViewPart
> and IEditorPart by using IWorkbenchPart. There are most probably
> challenges that I don't see from my restricted knowledge of EMF. But
> runnung an editor as a "model explorer" view (IViewPart) without to
> much changes in code using the original design I permit myself to
> suggest that the decisions makers (who probably don't read this)
> evaluate this as a future functionality in EMF.
>
> Thanks Eirik
>
>
>
>
>
>
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Undo/Redo in EMF based RCP application [message #429072 is a reply to message #429070] Tue, 07 April 2009 13:33 Go to previous message
Eirik Gr is currently offline Eirik GrFriend
Messages: 6
Registered: July 2009
Junior Member
Ed Merks wrote:

> Eirik,

> A bugzilla feature request with patches or design suggestions for the
> types of changes you'd like to see would be most welcome. Even just a
> sample application showing what you did to make a view part work would
> be an excellent contribution toward incorporating something reusable in
> the base framework.


> Eirik Grønsund wrote:
>> Hello
>> I see that this topic was discussed some time ago, but it gave me some
>> valuable tips converting the generated editor using IWorkbenchPart
>> (which both IViewPart and IEditorPart is derived from) instead of
>> IEditorPart.
>> In my setting, it was not suitable to let the editing of the model be
>> the central part in th perspective. My preference was to use it as a
>> supporting view - a kind of model explorer - for a central grapical
>> editor manipulating time series from simulation runs.
>>
>> To get started I went through the listing from Eike Stepper as far
>> back as to July 2005 (
>>
http://dev.eclipse.org/mhonarc/newsLists/news.eclipse.tools. emf/msg10973.html
>> ), where he pointed out that he got a NPE in the EMF code. The NPE was
>> most probably caused by an almost empty command stack. In my case all
>> the standard commands (copy, paste, undo/redo and delete) enabled
>> through the ItemProviderAdapter (and org.eclipse.emf.edit.command)
>> produced a NPE. The Crate Child and Sibling actions are handled in the
>> supported ActionBarContributor class generated by the editor and
>> worked as expected.
>> The reason seemed to be the code filling the command stack with
>> Undo/Redo, among others commands, was never executed. In the original
>> generated editor code, this happens in setActiveEditor which call
>> activate and deactivate and indirectly update. All these members are
>> in class EditingDomainActionbarContributor. Note that setActiveEditor
>> is also overriden in the supporting ActionbarContributer class
>> generated by the editor. This is the most central part where
>> IEditorPart is is used, so substituting setActiveEditor with e.g.
>> setActivePart supporting IViewPart (through IWorbenchPart) views
>> instead of editors is rather straight forward.
>>
>> In the listing provuded by Eike Stepper from July 2005 this is exactly
>> what is done in the supporting ActionBarContributor class. The member
>> starts out as follows:
>>
>> public void setActivePart(IWorkbenchPart part)
>> {
>> // TODO activeEditorPart == super.activeEditor ???
>> activeEditorPart = part;
>>
>> // Switch to the new selection provider.
>> // ...
>>
>> TODO is actually to copy the code from super.setActiveEditor as
>> questioned in the code snippet above and adopt it to a view. This
>> gives the following code:
>>
>> public void setActivePart(IWorkbenchPart part) {
>> // nested if clause copied from super.activeEditor
>> if (part != activeWorkbenchPart) {
>> if (activeWorkbenchPart != null) {
>> deactivate();
>> }
>> if (part instanceof IEditingDomainProvider) {
>> activeWorkbenchPart = part;
>> activate();
>> }
>> }
>> // Switch to the new selection provider.
>> // ...
>>
>> As we can see from the code, activate gets called. This is the place
>> where the command stack actions get enabled. So copying and overriding
>> activate, deactivate and update from EditingDomainActionbarContributor
>> to the generated supporting ActionbarContibutor class and adopting
>> IEditorPart dependent code to IWorkbencpart code (as in
>> setActivePart), the actions should be enabled, and the editor in the
>> view should behave as expected.
>> Thanks again for given me a starting point for getting a view enabled
>> model editor.
>>
>> As I started out, my belief is a that a model based tree editor will
>> in many applications only be a supporting view for a main editor. This
>> leads me to suggest a functionality in EMF supporting both IViewPart
>> and IEditorPart by using IWorkbenchPart. There are most probably
>> challenges that I don't see from my restricted knowledge of EMF. But
>> runnung an editor as a "model explorer" view (IViewPart) without to
>> much changes in code using the original design I permit myself to
>> suggest that the decisions makers (who probably don't read this)
>> evaluate this as a future functionality in EMF.
>>
>> Thanks Eirik
>>
>>
>>
>>
>>
>>
>>
Hello and thanks for a quick response

I am new in this portal, and my posting should have been a reply to
Christian Hauser from Dec 09 2008 and my statement about "decision makers
don't read this" was inappropiate and obviously wrong. Sorry about that.

I will revise and see if I can streamline the code and send a poposal.

Thanks, Eirik
Previous Topic:[Validation] Validate a certain feature
Next Topic:saving default value literal in XML resource
Goto Forum:
  


Current Time: Wed Apr 24 16:56:45 GMT 2024

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

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

Back to the top