Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » [Databinding] Howto add a single childs to the databinding properties for child nodes of a Treeviewe
[Databinding] Howto add a single childs to the databinding properties for child nodes of a Treeviewe [message #1070139] Wed, 17 July 2013 09:56 Go to next message
techteam is currently offline techteamFriend
Messages: 55
Registered: September 2010
Member
Hi,

I have a complex (Bean) model like:

MainObject
- childSingleA
- grandChildMany1
- grandChildMany2
- ...
- childManyA
- childManyB
- childSingleB
- grandChildSingle1
- grandChildMany3
- ...
- ...

All the childs must be shown inside a TreeViewer.

A good approach to get all the childs from a class, I think, is to work
with a combination of DelegetingListProperty and MultiListProperty.

This works well, with all multi child relations.

But how can I also add my single childs into the list of child nodes of
the treeviewer?

Greetings
Heiko
Re: [Databinding] Howto add a single childs to the databinding properties for child nodes of a Treev [message #1070148 is a reply to message #1070139] Wed, 17 July 2013 10:22 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
In the factory you can return a WritableList and do the list updateing
yourself.

Tom

On 17.07.13 11:56, Heiko wrote:
> Hi,
>
> I have a complex (Bean) model like:
>
> MainObject
> - childSingleA
> - grandChildMany1
> - grandChildMany2
> - ...
> - childManyA
> - childManyB
> - childSingleB
> - grandChildSingle1
> - grandChildMany3
> - ...
> - ...
>
> All the childs must be shown inside a TreeViewer.
>
> A good approach to get all the childs from a class, I think, is to work
> with a combination of DelegetingListProperty and MultiListProperty.
>
> This works well, with all multi child relations.
>
> But how can I also add my single childs into the list of child nodes of
> the treeviewer?
>
> Greetings
> Heiko
>
>
Re: [Databinding] Howto add a single childs to the databinding properties for child nodes of a Treev [message #1070200 is a reply to message #1070148] Wed, 17 July 2013 12:42 Go to previous message
techteam is currently offline techteamFriend
Messages: 55
Registered: September 2010
Member
Hi Tom,
thanks for your fast answer. I have seen such factory example with an
own factory implementation in one of your older blog posts, but it was
not my preferred way.

Even after my post I look into the IListProperty implementations and
based on these, I build my own "SingeltonListProperty", and it works
well at the moment.

Maybe this is a good extension to the existing classes, or should one of
the consisting expand this possibility.

But you're the professional, what do you say?

Greetings
Heiko



Am 17.07.2013 12:22, schrieb Tom Schindl:
> In the factory you can return a WritableList and do the list updateing
> yourself.
>
> Tom
>
> On 17.07.13 11:56, Heiko wrote:
>> Hi,
>>
>> I have a complex (Bean) model like:
>>
>> MainObject
>> - childSingleA
>> - grandChildMany1
>> - grandChildMany2
>> - ...
>> - childManyA
>> - childManyB
>> - childSingleB
>> - grandChildSingle1
>> - grandChildMany3
>> - ...
>> - ...
>>
>> All the childs must be shown inside a TreeViewer.
>>
>> A good approach to get all the childs from a class, I think, is to work
>> with a combination of DelegetingListProperty and MultiListProperty.
>>
>> This works well, with all multi child relations.
>>
>> But how can I also add my single childs into the list of child nodes of
>> the treeviewer?
>>
>> Greetings
>> Heiko
>>
>>
>


package org.eclipse.core.databinding.property.list;

import java.beans.PropertyDescriptor;
import java.util.Collections;
import java.util.List;

import org.eclipse.core.databinding.observable.Diffs;
import org.eclipse.core.databinding.observable.IDiff;
import org.eclipse.core.databinding.observable.list.ListDiff;
import org.eclipse.core.databinding.property.INativePropertyListener;
import org.eclipse.core.databinding.property.ISimplePropertyListener;
import org.eclipse.core.internal.databinding.beans.BeanPropertyHelper;
import org.eclipse.core.internal.databinding.beans.BeanPropertyListener;

@SuppressWarnings("restriction")
public class SingletonListProperty extends SimpleListProperty
{
private final PropertyDescriptor propertyDescriptor;
private final Class elementType;

/**
* @param propertyDescriptor
* @param elementType
*/
public SingletonListProperty(Class beanClass, String propertyName) {

this.propertyDescriptor = BeanPropertyHelper.getPropertyDescriptor(beanClass, propertyName);
this.elementType = propertyDescriptor.getPropertyType();

}

public Object getElementType() {
return elementType;
}

protected List doGetList(Object source) {
return asList(BeanPropertyHelper.readProperty(source,
propertyDescriptor));
}

private List asList(Object propertyValue) {
if (propertyValue == null)
return Collections.EMPTY_LIST;
return Collections.singletonList(propertyValue);
}

protected void doSetList(Object source, List list, ListDiff diff) {
doSetList(source, list);
}

protected void doSetList(Object source, List list) {
BeanPropertyHelper.writeProperty(source, propertyDescriptor,
convertListToBeanPropertyType(list));
}

private Object convertListToBeanPropertyType(List list) {
if(null == list || list.isEmpty())
return null;
return list.get(0);
}

public INativePropertyListener adaptListener(
final ISimplePropertyListener listener) {
return new BeanPropertyListener(this, propertyDescriptor, listener) {
protected IDiff computeDiff(Object oldValue, Object newValue) {
return Diffs
.computeListDiff(asList(oldValue), asList(newValue));
}
};
}

public String toString() {
String s = BeanPropertyHelper.propertyName(propertyDescriptor);
if (elementType != null)
s += "<" + BeanPropertyHelper.shortClassName(elementType) + ">"; //$NON-NLS-1$//$NON-NLS-2$
return s;
}
}
Previous Topic:TableViewer Sorting and MultiSelectionBinding
Next Topic:How to make CheckBoxCellEditor read by Screen Reader
Goto Forum:
  


Current Time: Fri Apr 26 09:03:27 GMT 2024

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

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

Back to the top