Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Notification between EObjects (used for updating calculated fields)
Notification between EObjects (used for updating calculated fields) [message #429154] Fri, 10 April 2009 11:00 Go to next message
Cristian Spiescu is currently offline Cristian SpiescuFriend
Messages: 100
Registered: July 2009
Senior Member
Hello,

I have a class (called Proxy) that has a reference (unidirectional)
towards another object (called attachedLabel:Label). The Proxy object has
a "calculated" property - the method getLabelTitle(). When I call
proxy.getLabelTitle() it returns a result calculated from the label field
(proxy.attachedLabel).

I would like each Proxy object to be notified by it's associated Label
when it changed (in order for the Proxy object to send a notification to
update the display data).

I have found the following soloution:
* Proxy has an inner class that implements Adapter (Proxy.ad)
* when a Proxy.setLabel(), I attach the above adapter to it:
label.eAdapters().add(ad);
* ad.notifyChanged() does the job needed when the attached object is
updated

Is there another way of doing this?

Thank you in advance.
Best regards,
Cristi.
Re: Notification between EObjects (used for updating calculated fields) [message #429155 is a reply to message #429154] Fri, 10 April 2009 11:46 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Cristi,

That's the only way I can think of with what's available today.
Eventually when we support access listeners, there will be some other
interesting techniques we could apply...


Cristian wrote:
> Hello,
>
> I have a class (called Proxy) that has a reference (unidirectional)
> towards another object (called attachedLabel:Label). The Proxy object
> has a "calculated" property - the method getLabelTitle(). When I call
> proxy.getLabelTitle() it returns a result calculated from the label
> field (proxy.attachedLabel).
>
> I would like each Proxy object to be notified by it's associated Label
> when it changed (in order for the Proxy object to send a notification
> to update the display data).
>
> I have found the following soloution:
> * Proxy has an inner class that implements Adapter (Proxy.ad)
> * when a Proxy.setLabel(), I attach the above adapter to it:
> label.eAdapters().add(ad);
> * ad.notifyChanged() does the job needed when the attached object is
> updated
>
> Is there another way of doing this?
>
> Thank you in advance.
> Best regards,
> Cristi.
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Notification between EObjects (used for updating calculated fields) [message #429156 is a reply to message #429155] Fri, 10 April 2009 12:51 Go to previous messageGo to next message
Cristian Spiescu is currently offline Cristian SpiescuFriend
Messages: 100
Registered: July 2009
Senior Member
Dear Ed,

Thanks a lot for your answer. I have one more question regarding this
technique and regarding possible memory leaks.

I will add my adapter to the "listened" object within the setLabel method
like this:

if (oldLabel != newLabel) {
oldLabel.eAdapters().remove(myChangeListenerAdapter);
newLabel.eAdapters().add(myChangeListenerAdapter);
}

Is there another place when I should be careful to remove
myChangeListener?

* I guess that if I delete the listened object, the setter will be
automatically called with a null argument.
* What happens if I delete the main object (Proxy in my case)? I have
looked briefly at the code of DeleteCommand and I'm under the impression
that it doesn't notify in any way the deleted object (and there is no
"dispose" method). Does this means that I need to override the
DeleteCommand as well?
* Are there any other use cases which can lead to "memory leak"? I.e. one
or both of the objects don't exist anymore in the model, but they will be
still in memory (in a state that won't allow the garbage collector to
finally destroy them)?

Best regards,
Cristi.
Re: Notification between EObjects (used for updating calculated fields) [message #429157 is a reply to message #429156] Fri, 10 April 2009 13:29 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------040007070009010304000702
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

Cristi,

Comments below.

Cristian wrote:
> Dear Ed,
>
> Thanks a lot for your answer. I have one more question regarding this
> technique and regarding possible memory leaks.
Yes, that's definitely a cause for concern.
>
> I will add my adapter to the "listened" object within the setLabel
> method like this:
>
> if (oldLabel != newLabel) {
> oldLabel.eAdapters().remove(myChangeListenerAdapter);
> newLabel.eAdapters().add(myChangeListenerAdapter);
> }
>
> Is there another place when I should be careful to remove
> myChangeListener?
Is it a proxy resolving reference? If so, the getter will need attention.
> * I guess that if I delete the listened object, the setter will be
> automatically called with a null argument.
Yes.
> * What happens if I delete the main object (Proxy in my case)? I have
> looked briefly at the code of DeleteCommand and I'm under the
> impression that it doesn't notify in any way the deleted object (and
> there is no "dispose" method). Does this means that I need to override
> the DeleteCommand as well?
Just keep in mind that a delete can be undone too. Perhaps it would be
good for the adapter to have a weak reference to the Proxy and you could
set it up so that when the adapter is notified and the Proxy reference
has been collected, that the adapter removes itself. Or better yet,
though I've not played much with finalize, but I suppose the finalize
could be specialized to remove the adapter. I.e., do something that
will work purely at the model level... Probably we should have some
type of nice recipe in the wiki for something like this...

http://wiki.eclipse.org/EMF/Recipes

> * Are there any other use cases which can lead to "memory leak"? I.e.
> one or both of the objects don't exist anymore in the model, but they
> will be still in memory (in a state that won't allow the garbage
> collector to finally destroy them)?
I can't think of anything else at the moment. It will be nice to hear
back about your experience. Perhaps you'd be so kind as to create a new
recipe in the wiki so we can work on polishing it together...
>
> Best regards,
> Cristi.
>

--------------040007070009010304000702
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">
<title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
Cristi,<br>
<br>
Comments below.<br>
<br>
Cristian wrote:
<blockquote
cite="mid:36976b274b2c9b26ae32d4c14b7b8dbf$1@www.eclipse.org"
type="cite">Dear Ed,
<br>
<br>
Thanks a lot for your answer. I have one more question regarding this
technique and regarding possible memory leaks.
<br>
</blockquote>
Yes, that's definitely a cause for concern.<br>
<blockquote
cite="mid:36976b274b2c9b26ae32d4c14b7b8dbf$1@www.eclipse.org"
type="cite"><br>
I will add my adapter to the "listened" object within the setLabel
method like this:
<br>
<br>
if (oldLabel != newLabel) {
<br>
oldLabel.eAdapters().remove(myChangeListenerAdapter);
<br>
newLabel.eAdapters().add(myChangeListenerAdapter);
<br>
}
<br>
<br>
Is there another place when I should be careful to remove
myChangeListener? <br>
</blockquote>
Is it a proxy resolving reference? If so, the getter will need
attention.<br>
<blockquote
cite="mid:36976b274b2c9b26ae32d4c14b7b8dbf$1@www.eclipse.org"
type="cite">* I guess that if I delete the listened object, the setter
will be automatically called with a null argument.
<br>
</blockquote>
Yes.<br>
<blockquote
cite="mid:36976b274b2c9b26ae32d4c14b7b8dbf$1@www.eclipse.org"
type="cite">* What happens if I delete the main object (Proxy in my
case)? I have looked briefly at the code of DeleteCommand and I'm under
the impression that it doesn't notify in any way the deleted object
(and there is no "dispose" method). Does this means that I need to
override the DeleteCommand as well?
<br>
</blockquote>
Just keep in mind that a delete can be undone too.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Notification between EObjects (used for updating calculated fields) [message #429193 is a reply to message #429157] Mon, 13 April 2009 14:24 Go to previous messageGo to next message
Cristian Spiescu is currently offline Cristian SpiescuFriend
Messages: 100
Registered: July 2009
Senior Member
Dear Ed,

> I can't think of anything else at the moment. It will be nice to hear
> back about your experience. Perhaps you'd be so kind as to create a new
> recipe in the wiki so we can work on polishing it together...
I didn't know about this page. I have almost finished the implementation.
I will be glad to contribute with it. I get back when completly finished.

Regarding the DeleteCommand, I have seen that it cannot be replaced using
the standard ContentProvidar way if i use the AdapterFactoryEditingDomain.
Making a custom EditingDomain (based on AdapterFactoryEditingDomain for
example) and overriding the createCommand() method seems the only way to
replace the DeleteCommand. Am I right, or am I missing something?

Thanks.
Best regards,
Cristi.
Re: Notification between EObjects (used for updating calculated fields) [message #429195 is a reply to message #429193] Mon, 13 April 2009 18:03 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Cristi,

Yes, DeleteCommand is handled directly by the editing domain and would
be specialized there. The item providers only see the RemoveCommands.


Cristian wrote:
> Dear Ed,
>
>> I can't think of anything else at the moment. It will be nice to
>> hear back about your experience. Perhaps you'd be so kind as to
>> create a new recipe in the wiki so we can work on polishing it
>> together...
> I didn't know about this page. I have almost finished the
> implementation. I will be glad to contribute with it. I get back when
> completly finished.
>
> Regarding the DeleteCommand, I have seen that it cannot be replaced
> using the standard ContentProvidar way if i use the
> AdapterFactoryEditingDomain. Making a custom EditingDomain (based on
> AdapterFactoryEditingDomain for example) and overriding the
> createCommand() method seems the only way to replace the
> DeleteCommand. Am I right, or am I missing something?
>
> Thanks.
> Best regards,
> Cristi.
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:How to add validation button on my own EMF project
Next Topic:How to generate an EMF project with validation part?
Goto Forum:
  


Current Time: Fri Apr 26 17:01:56 GMT 2024

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

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

Back to the top