Skip to main content



      Home
Home » Modeling » EMF » Notifying of changes to attributes of child objects in a list.
Notifying of changes to attributes of child objects in a list. [message #1067806] Wed, 10 July 2013 12:15 Go to next message
Eclipse UserFriend
Hi,

I have a situation where either I have my model or my thinking all wrong.

I have an object, Host. Host contains a list of HostName objects.

I have modified the HostItemProvider to generate a label based on the contents of the HostName objects.

I can feed this to a tree viewer and everything works as expected if I modify the HostName list (i.e. add, delete or move an item). However, I want to get notified if the content of an element in the list actually changes.

I assumed that change notifications would get passed up to the parent and then therefore to the provider but this is not happening.

It is clear that I need to glue the HostItemProvider to the contents of of the HostName list but I cannot figure out where to do this. I have tried stepping through the code when the AdapterFactory is called as well as reading through the various generated AdapterFactories and I am just as confused as before. Every idea I come up with seems to be a nasty kludge, in elegant and therefore probably wrong.

Can anyone give me a pointer where I should be looking?

BTW, this is with the Kepler release of EMF. The model was initially written in the Juno release but all the automated code has been re-generated.

TIA,
Jack
Re: Notifying of changes to attributes of child objects in a list. [message #1067817 is a reply to message #1067806] Wed, 10 July 2013 13:01 Go to previous messageGo to next message
Eclipse UserFriend
Jack,

Comments below.

On 10/07/2013 6:15 PM, Jack Mising name wrote:
> Hi,
>
> I have a situation where either I have my model or my thinking all wrong.
>
> I have an object, Host. Host contains a list of HostName objects.
>
> I have modified the HostItemProvider to generate a label based on the
> contents of the HostName objects.
So changes to those will need to produce updates for the label of the
containing Host...
>
> I can feed this to a tree viewer and everything works as expected if I
> modify the HostName list (i.e. add, delete or move an item). However,
> I want to get notified if the content of an element in the list
> actually changes.
>
> I assumed that change notifications would get passed up to the parent
> and then therefore to the provider but this is not happening.
No, an item provider only listens to the object its adapting so
HostItemProvider only gets notifications for changes to features of Host...
>
> It is clear that I need to glue the HostItemProvider to the contents
> of of the HostName list but I cannot figure out where to do this. I
> have tried stepping through the code when the AdapterFactory is called
> as well as reading through the various generated AdapterFactories and
> I am just as confused as before. Every idea I come up with seems to be
> a nasty kludge, in elegant and therefore probably wrong.
>
> Can anyone give me a pointer where I should be looking?
One similar example is
org.eclipse.emf.ecore.provider.EGenericTypeItemProvider.notifyChanged(Notification).
But in this case the contained generic type objects are shown in the
tree and to change them you'd need to have expanded the tree and in that
case their item provider is added as an adapter and receives
notification, which you'll see in the above method are propagate up the
tree to update the label of the containing generic type.

In your case, you do need adapters for the contained HostName
instances. So one approach you might use is to have the Host compute
its label (i.e., in getText) by using the item provider for the
contained HostName instances to compute their label and do the same kind
of trick in the HostNameItemprovider's notificationChange method to
produce a label update for the containing Host. A
AdapterFactoryItemDelegator can be useful for wrapping the
org.eclipse.emf.edit.provider.ItemProviderAdapter.getAdapterFactory()
within the HostItemProvider. You might have a peek at
org.eclipse.emf.edit.tree.provider.TreeNodeItemProvider.getText(Object)
to see what it does that's somewhat similar.
>
> BTW, this is with the Kepler release of EMF. The model was initially
> written in the Juno release but all the automated code has been
> re-generated.
>
> TIA,
> Jack
Re: Notifying of changes to attributes of child objects in a list. [message #1067936 is a reply to message #1067817] Thu, 11 July 2013 06:53 Go to previous messageGo to next message
Eclipse UserFriend
Thanks for the pointers, though I am still pretty confused as to what i should be doing. Are you saying I should be instantiating HostnameItemProviders within the HostNameImpl class?

While trying to figure out what to do with a AdapterFactoryItemDelegator, I came across this : http://wiki.eclipse.org/EMF/Recipes#Recipe:_Custom_Labels

I had to modify that so that the notifers were refering to the objects in the notifications rather than calling 'getTarget()' as otherwise only the last Host objectin the tree received change notifications. This works when I add the Hostname objects to the tree.

I have found that if I override setTarget in my HostItemProvider, I can adapt the HostName objects like so :-

public void setTarget(Notifier target) {
super.setTarget(target);
adapterFactory.adapt(((Host)target).getHostnames().get(0), IItemLabelProvider.class);
}

I know that doesn't cover the whole list and I will have to adapt any additions to the list but it appears to work as a PoC. Is this the right approach or am I just setting myself up for big trouble in the future?

Thanks,

Jack
Re: Notifying of changes to attributes of child objects in a list. [message #1067956 is a reply to message #1067936] Thu, 11 July 2013 08:22 Go to previous messageGo to next message
Eclipse UserFriend
Jack,

Comments below.

On 11/07/2013 12:53 PM, Jack Mising name wrote:
> Thanks for the pointers, though I am still pretty confused as to what
> i should be doing. Are you saying I should be instantiating
> HostnameItemProviders within the HostNameImpl class?
>
> While trying to figure out what to do with a
> AdapterFactoryItemDelegator, I came across this :
> http://wiki.eclipse.org/EMF/Recipes#Recipe:_Custom_Labels
>
> I had to modify that so that the notifers were refering to the objects
> in the notifications rather than calling 'getTarget()' as otherwise
> only the last Host objectin the tree received change notifications.
> This works when I add the Hostname objects to the tree.
>
> I have found that if I override setTarget in my HostItemProvider, I
> can adapt the HostName objects like so :-
>
> public void setTarget(Notifier target) {
> super.setTarget(target);
> adapterFactory.adapt(((Host)target).getHostnames().get(0),
> IItemLabelProvider.class);
Is there always one? Does your label only depend on the first one?
> }
>
> I know that doesn't cover the whole list and I will have to adapt any
> additions to the list but it appears to work as a PoC. Is this the
> right approach or am I just setting myself up for big trouble in the
> future?
It's similar to what I suggested; the above just does what the
AdapterFactoryItemDelegator for the adapterFactory would do. But I
would do the work in the getText method and create the delegator in the
constructor. You should also ensure that the notifyChanged method
handles the changes to the hostnames feature as a label update, so then
the getText will be called again with the hostnames list changes, and if
that method properly iterates over the list of host names to build the
label (using the delegator which attaches adapters as you did above)
that should do the trick...
> Thanks,
>
> Jack
Re: Notifying of changes to attributes of child objects in a list. [message #1068070 is a reply to message #1067956] Thu, 11 July 2013 18:09 Go to previous message
Eclipse UserFriend
Ed Merks wrote on Thu, 11 July 2013 13:22 :
>> public void setTarget(Notifier target) {
>> super.setTarget(target);
>> adapterFactory.adapt(((Host)target).getHostnames().get(0),
>> IItemLabelProvider.class);
>Is there always one? Does your label only depend on the first one?

It is actually based off a single, arbitrary entry in the list. Which one is defined elsewhere. While I was figuring out how to make the label behave how I wanted, it was easier to hard-code it to use the first entry.

Once I had that working, modifying it so that the selection can be arbitrary is easy but didn't warrant doing until I was happy with the bit I had no clue about Wink

It seems like what I am doing is not completely incorrect, so I think I will leave it that way until I finally get my head around the AdapterFactoryItemDelegator.

Many thanks for your time and help,

Jack
Previous Topic:Problem serialization of model to encrypted binary file
Next Topic:restriction of child elements
Goto Forum:
  


Current Time: Sat Jul 12 22:38:10 EDT 2025

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

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

Back to the top