|
|
|
|
|
Re: Table column and composition relation binding [message #1485974 is a reply to message #1485860] |
Mon, 24 November 2014 18:12 |
Thomas Schindl Messages: 6651 Registered: July 2009 |
Senior Member |
|
|
I was more after how you are using the stuff to setup TableColumns but
let me see if I understand the problem domain appropriately.
We have a model:
Person
+ friends : List<Person>
+ name: String
and we want a table to display:
--------------------------
| Name | Friends |
==========================
| Tom | Mike, Steve |
--------------------------
| Steve | Mike, Tom |
--------------------------
| Mike | Steve, Tom |
--------------------------
Is that correct? So we want the Friends columns to update:
* if the list of friends changes
* if one of the names in of the displayed friends changes
Is this correct?
Tom
On 24.11.14 17:02, David Ponzo wrote:
> Well , I hoped some code of you :)
>
> However you can fin below my implementation of the need I was expecting
> to find in e(fx). I hope it will make my purpose more clear :
>
>
>
> import java.util.ArrayList;
> import java.util.List;
>
> import javafx.beans.Observable;
> import javafx.beans.binding.Bindings;
> import javafx.beans.binding.StringExpression;
> import javafx.beans.property.Property;
> import javafx.beans.value.ObservableValue;
> import javafx.util.Callback;
>
> import org.eclipse.core.databinding.conversion.IConverter;
> import org.eclipse.core.databinding.observable.value.IObservableValue;
> import org.eclipse.core.databinding.property.value.IValueProperty;
> import org.eclipse.emf.ecore.util.EObjectContainmentEList;
> import org.eclipse.fx.core.databinding.AdapterFactory;
>
> // TODO: Auto-generated Javadoc
> /**
> * The Class CallBackMultiRelationWithCriteria.
> *
> * @param <T> the generic type
> */
> public class CallBackMultiRelationWithCriteria<T> implements
> Callback<Object, Object> {
>
> /** The master value property. */
> private final IValueProperty masterValueProperty;
>
> /** The detail value property. */
> private final IValueProperty detailValueProperty;
>
> /** The converter. */
> private final IConverter converter;
>
> /**
> * Instantiates a new call back.
> *
> * @param masterValueProperty the master value property
> * @param detailValueProperty the detail value property
> * @param converter the converter
> */
> public CallBackMultiRelationWithCriteria(IValueProperty
> masterValueProperty, IValueProperty detailValueProperty, IConverter
> converter) {
> this.masterValueProperty = masterValueProperty;
> this.detailValueProperty = detailValueProperty;
> this.converter = converter;
> }
>
> /**
> * {@inheritDoc}
> */
> @SuppressWarnings({ "unchecked" })
> @Override
> public ObservableValue<T> call(Object valueSource) {
> final IObservableValue masterValueObs =
> this.masterValueProperty.observe(valueSource);
>
> // When the attribute is a reference with a multiplicity >1
> final Object masterValue = masterValueObs.getValue();
>
> if (masterValue instanceof EObjectContainmentEList<?>) {
>
> final EObjectContainmentEList<?> e =
> (EObjectContainmentEList<?>) masterValue;
>
> final List<Observable> dependencies = new ArrayList<>();
> // We wrap the EMF property into a javafx
> // property for each element of the list
> for (final Object value : e) {
> final IObservableValue observableValue =
> this.detailValueProperty.observe(value);
> final ObservableValue<T> tmp =
> AdapterFactory.adapt(observableValue);
> dependencies.add(tmp);
> }
>
> // When the previous EMF property change, the javafx
> // property change and so the String expression is
> // recompute.The new computation consist in a new
> // conversion and the addition of a delimiter
> final StringExpression se = Bindings.createStringBinding(() -> {
> final StringBuilder sb = new StringBuilder();
> for (int i = 0; i < e.size(); i++) {
>
> final Object containement = e.get(i);
>
> if (containement instanceof Property) {
> sb.append(((Property<?>) containement).getValue());
> }
> else {
> final T value = ((T) containement);
> final String valueConvert =
> this.converter.convert(value).toString();
> sb.append(valueConvert);
> }
>
> if ((i + 1) < e.size()) {
> sb.append("\n ");
> }
> }
> return sb.toString();
> }, dependencies.toArray(new Observable[dependencies.size()]));
>
> return (ObservableValue<T>) se;
> }
>
> return null;
> }
>
> }
>
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03405 seconds