Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Notify changes on any models
icon5.gif  Notify changes on any models [message #994153] Wed, 26 December 2012 15:00 Go to next message
Stefan Weiser is currently offline Stefan WeiserFriend
Messages: 55
Registered: February 2012
Member
Hello!

I'd like to identify changes on models of emf, so I recognize when EObjects are added, changed, removed ... I'll get the model as Resource out of the workspace.

Now I have the problem that I don't know how to implement that. I've found the documentation of INotifyChangedListener but don't know how to use it correctly and I don't find any samples or tutorials for it.

Any hints would be nice and might be helpful.
Re: Notify changes on any models [message #994238 is a reply to message #994153] Wed, 26 December 2012 19:59 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
Stefan,

Try using an EContentAdapter. Add it to the ResourceSet's eAdapters.

On 26/12/2012 4:00 PM, Stefan Weiser wrote:
> Hello!
>
> I'd like to identify changes on models of emf, so I recognize when
> EObjects are added, changed, removed ... I'll get the model as
> Resource out of the workspace.
> Now I have the problem that I don't know how to implement that. I've
> found the documentation of
> http://download.eclipse.org/modeling/emf/emf/javadoc/2.6.0/org/eclipse/emf/edit/provider/class-use/INotifyChangedListener.htmlbut
> don't know how to use it correctly and I don't find any samples or
> tutorials for it.
> Any hints would be nice and might be helpful.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Notify changes on any models [message #994790 is a reply to message #994153] Fri, 28 December 2012 09:14 Go to previous messageGo to next message
Stefan Weiser is currently offline Stefan WeiserFriend
Messages: 55
Registered: February 2012
Member
Thx for the hint Ed Merks!

In combination I've found these 2 tutorials:

I'm able to load any model and get an EObject. That's really nice.
But when I add an EContentAdapter no Notification appears, if I'll edit the model in the editor.

My code:
	private EContentAdapter eContentAdapter = new EContentAdapter() {

		/**
		 * {@inheritDoc}
		 */
		@Override
		public void notifyChanged(
				org.eclipse.emf.common.notify.Notification notification) {
			super.notifyChanged(notification);
			System.out
					.println("Notfication received from the data model. Data model has changed!!!");

		};

	};

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void selectionChanged(IWorkbenchPart part, ISelection selection) {
		if (selection instanceof IStructuredSelection) {
			Object obj = ((IStructuredSelection) selection).getFirstElement();
			if (obj != null) {
				IFile emfResourceFile = (IFile) Platform.getAdapterManager()
						.getAdapter(obj, IFile.class);
				if (emfResourceFile != null) {

					ResourceSet resourceSet = new ResourceSetImpl();
					Resource resource = resourceSet.getResource(
							URI.createURI(emfResourceFile.getFullPath()
									.toString()), true);
					EObject eObject = resource.getContents().get(0);
					eObject.eAdapters().add(eContentAdapter);
					System.out.println(eObject);
				}
			}
		}

	};
Re: Notify changes on any models [message #994800 is a reply to message #994790] Fri, 28 December 2012 10:00 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
Stefan,

Try adding it to the resource set rather adding it only to the first
root object of the first resource.

ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.eAdapters().add(eContentAdapter);

Then you should see notifications for all changes to all objects
contained by the resource set (including notifications from the resource
set and from its resources).

If you still have a problem, use the debugger to see why the object your
changing doesn't have the content adapter attached.


On 28/12/2012 10:15 AM, Stefan Weiser wrote:
> Thx for the hint Ed Merks!
>
> In combination I've found these 2 tutorials:
>
> http://www.vogella.com/articles/EclipseEMFPersistence/article.html
> http://www.vogella.com/articles/EclipseEMFNotification/article.html
>
> I'm able to load any model and get an EObject. That's really nice.
> But when I add an EContentAdapter no Notification appears, if I'll
> edit the model in the editor.
> My code:
>
> private EContentAdapter eContentAdapter = new EContentAdapter() {
>
> /**
> * {@inheritDoc}
> */
> @Override
> public void notifyChanged(
> org.eclipse.emf.common.notify.Notification
> notification) {
> super.notifyChanged(notification);
> System.out
> .println("Notfication received from the data
> model. Data model has changed!!!");
>
> };
>
> };
>
> /**
> * {@inheritDoc}
> */
> @Override
> public void selectionChanged(IWorkbenchPart part, ISelection
> selection) {
> if (selection instanceof IStructuredSelection) {
> Object obj = ((IStructuredSelection)
> selection).getFirstElement();
> if (obj != null) {
> IFile emfResourceFile = (IFile)
> Platform.getAdapterManager()
> .getAdapter(obj, IFile.class);
> if (emfResourceFile != null) {
>
> ResourceSet resourceSet = new ResourceSetImpl();
> Resource resource = resourceSet.getResource(
> URI.createURI(emfResourceFile.getFullPath()
> .toString()), true);
> EObject eObject = resource.getContents().get(0);
> eObject.eAdapters().add(eContentAdapter);
> System.out.println(eObject);
> }
> }
> }
>
> };
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Notify changes on any models [message #994827 is a reply to message #994800] Fri, 28 December 2012 11:31 Go to previous messageGo to next message
Stefan Weiser is currently offline Stefan WeiserFriend
Messages: 55
Registered: February 2012
Member
Hello Ed Merks,

thank you for your hints.

Now I can identify changes made by code but not changes in a running instance of Eclipse.

Adding it to the ResourceSet doesn't really solve the problem I've got. The ResourceSet seems to be a snapshot from the selected part and doesn't recognize further changes afterwards. Or I don't use it in a correct way.

So what can I do, if I'd like to notify changes I'm doing on the models in an editor in an Eclipse-instance. Snapshots of the models doesn't really help me for my task. I hope my problem is understandable.
Re: Notify changes on any models [message #994832 is a reply to message #994827] Fri, 28 December 2012 11:47 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
Stefan,

Comments below.

On 28/12/2012 12:31 PM, Stefan Weiser wrote:
> Hello Ed Merks,
> thank you for your hints.
>
> Now I can identify changes made by code but not changes in a running
> instance of Eclipse.
>
> Adding it to the ResourceSet doesn't really solve the problem I've
> got. The ResourceSet seems to be a snapshot from the selected part and
> doesn't recognize further changes afterwards.
I have no idea what you're doing in the code you showed. Yes of course
it's just local objects that will be garbage collected unless you keep
some reference to it...
> Or I don't use it in a correct way.
> So what can I do, if I'd like to notify changes I'm doing on the
> models in an editor in an Eclipse-instance.
In the generated editor, you can add such a content adapter to the
editing domain's resource set, e.g., by specializing the createModel method.
> Snapshots of the models doesn't really help me for my task. I hope my
> problem is understandable.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Notify changes on any models [message #994839 is a reply to message #994832] Fri, 28 December 2012 12:22 Go to previous messageGo to next message
Stefan Weiser is currently offline Stefan WeiserFriend
Messages: 55
Registered: February 2012
Member
Ed Merks wrote on Fri, 28 December 2012 06:47
Stefan,

Comments below.

On 28/12/2012 12:31 PM, Stefan Weiser wrote:
> Hello Ed Merks,
> thank you for your hints.
>
> Now I can identify changes made by code but not changes in a running
> instance of Eclipse.
>
> Adding it to the ResourceSet doesn't really solve the problem I've
> got. The ResourceSet seems to be a snapshot from the selected part and
> doesn't recognize further changes afterwards.
I have no idea what you're doing in the code you showed. Yes of course
it's just local objects that will be garbage collected unless you keep
some reference to it...
> Or I don't use it in a correct way.
> So what can I do, if I'd like to notify changes I'm doing on the
> models in an editor in an Eclipse-instance.
In the generated editor, you can add such a content adapter to the
editing domain's resource set, e.g., by specializing the createModel method.
> Snapshots of the models doesn't really help me for my task. I hope my
> problem is understandable.

OK, then I think my approach won't work in manner I'd like to. It seem that there are 2 ways of possible implementations:

  • An editor-specific implementation, which identifies changes on models.
  • Implementing a ResourceChangeListener on the workspace, which is not EMF specific.

I hoped that it is possible to use the INotifyChangedListener but I didn't find any API usable for my task.
Re: Notify changes on any models [message #994842 is a reply to message #994839] Fri, 28 December 2012 12:30 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
Stefan,

Listening to the workspace is the only way to know about changes that
are actually saved. You can't in general know about changes happening
internal to editors, and each editor generally maintains it's own local
instances. Generated EMF editors do implement IEditingDomainProvider
which provides access to the editing domain and hence the resource set.



On 28/12/2012 1:22 PM, Stefan Weiser wrote:
> Ed Merks wrote on Fri, 28 December 2012 06:47
>> Stefan,
>>
>> Comments below.
>>
>> On 28/12/2012 12:31 PM, Stefan Weiser wrote:
>> > Hello Ed Merks,
>> > thank you for your hints.
>> >
>> > Now I can identify changes made by code but not changes in a
>> running > instance of Eclipse.
>> >
>> > Adding it to the ResourceSet doesn't really solve the problem I've
>> > got. The ResourceSet seems to be a snapshot from the selected part
>> and > doesn't recognize further changes afterwards.
>> I have no idea what you're doing in the code you showed. Yes of
>> course it's just local objects that will be garbage collected unless
>> you keep some reference to it...
>> > Or I don't use it in a correct way.
>> > So what can I do, if I'd like to notify changes I'm doing on the >
>> models in an editor in an Eclipse-instance.
>> In the generated editor, you can add such a content adapter to the
>> editing domain's resource set, e.g., by specializing the createModel
>> method.
>> > Snapshots of the models doesn't really help me for my task. I hope
>> my > problem is understandable.
>
> OK, then I think my approach won't work in manner I'd like to. It seem
> that there are 2 ways of possible implementations:
>
> An editor-specific implementation, which identifies changes on models.
> Implementing a ResourceChangeListener on the workspace, which is not
> EMF specific.
>
> I hoped that it is possible to use the INotifyChangedListener but I
> didn't find any API usable for my task.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Notify changes on any models [message #994916 is a reply to message #994842] Fri, 28 December 2012 16:23 Go to previous messageGo to next message
Stefan Weiser is currently offline Stefan WeiserFriend
Messages: 55
Registered: February 2012
Member
Ed Merks wrote on Fri, 28 December 2012 07:30
Stefan,

Listening to the workspace is the only way to know about changes that
are actually saved. You can't in general know about changes happening
internal to editors, and each editor generally maintains it's own local
instances. Generated EMF editors do implement IEditingDomainProvider
which provides access to the editing domain and hence the resource set.

Hello Ed Merks,

this is very helpful, so I need not to try something any more, which is not possible. I will try to focus on the EMF editors to achieve a solution.

Thx for your patience and your help.
Re: Notify changes on any models [message #1352519 is a reply to message #994916] Tue, 13 May 2014 13:56 Go to previous messageGo to next message
Wenjing Yuan is currently offline Wenjing YuanFriend
Messages: 7
Registered: May 2014
Junior Member
Hi Stefan,

I just saw your questions about detecting the changes in Eclipse running instance,
right now I encounter the same problem, I have the editingDomain in hand, so how could I detect any changes including editing the attributes in this domain by using notification or EAdapter perhaps? Any hints would be appreciated.
Re: Notify changes on any models [message #1354134 is a reply to message #1352519] Wed, 14 May 2014 05:59 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
Wenjing,

So none of the hints in this thread have helped?

On 13/05/2014 4:51 PM, Wenjing Yuan wrote:
> Hi Stefan,
>
> I just saw your questions about detecting the changes in Eclipse
> running instance,
> right now I encounter the same problem, I have the editingDomain in
> hand, so how could I detect any changes including editing the
> attributes in this domain by using notification or EAdapter perhaps?
> Any hints would be appreciated.


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:Import of Ecore model without regenerating ecore model code
Next Topic:[Net4j] TimeoutException while activating Client-Protocol
Goto Forum:
  


Current Time: Sat Apr 20 00:05:23 GMT 2024

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

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

Back to the top