|
Re: Notifying of changes to attributes of child objects in a list. [message #1067817 is a reply to message #1067806] |
Wed, 10 July 2013 17:01 |
Ed Merks Messages: 33133 Registered: July 2009 |
Senior Member |
|
|
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
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
|
Re: Notifying of changes to attributes of child objects in a list. [message #1067956 is a reply to message #1067936] |
Thu, 11 July 2013 12:22 |
Ed Merks Messages: 33133 Registered: July 2009 |
Senior Member |
|
|
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
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
|
Powered by
FUDForum. Page generated in 0.02129 seconds