|
|
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 |
techteam 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;
}
}
|
|
|
Powered by
FUDForum. Page generated in 0.02811 seconds