Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » [Databinding] Bind a bean with observable values as properties to table
[Databinding] Bind a bean with observable values as properties to table [message #1209048] Mon, 25 November 2013 05:01 Go to next message
Leonid Ripeynih is currently offline Leonid Ripeynih
Messages: 91
Registered: February 2012
Member
Hi all! I have a simple, and probably stupid question, but I just can't figure it out myself.

Suppose, I have a bean, say like that:

public class Wrapper {

  public IObservableValue getColumn1Value() {
     // actually returns correct observable value
  }
  public IObservableValue getColumn2Value() }
    // actually returns correct observable value
  }
}


A table viwer, and an observable list of such beans:
TableViewer viewer = createTableViewer();
viewer.setContentProvider(new ObservableListContentProvider());
viewer.setLabelProvider(???);
viewer.setInput(getObservableListOfWrappers());


With simple LabelProvider it works fine on elements insertion/removal, but i really can't figure out how to bind Column1Value to column 1 and Column2Value to column 2, and make a values in columns updating when value changes.

What's the best way of doing it?

Thanks in advance!
Re: [Databinding] Bind a bean with observable values as properties to table [message #1277821 is a reply to message #1209048] Wed, 26 March 2014 10:05 Go to previous message
Nigel Westbury is currently offline Nigel Westbury
Messages: 16
Registered: July 2009
Junior Member
This is not a stupid question at all. By creating the wrapper around
your model you are effectively building data binding observables right
into the model. In general this makes the binding code simpler.

However the problem is that you need to create a ValueProperty on this
property. The data binding code provides support to obtain a
ValueProperty for a Bean property with a getter and setter but does not
provide a way of obtaining a ValueProperty when the source object
contains just a single method which returns the observable.

This can be done using the following class. Perhaps I should check this
in as there appears to be a general need for it. In your case S is
Wrapper and your implementation of getObservable simply calls the method
on the wrapper.

Nigel Westbury

/*******************************************************************************
* Copyright (c) 2014 Nigel Westbury and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Nigel Westbury - initial API and implementation

*******************************************************************************/

import org.eclipse.core.databinding.observable.IChangeListener;
import org.eclipse.core.databinding.observable.Realm;
import
org.eclipse.core.databinding.observable.value.AbstractObservableValue;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.databinding.observable.value.IValueChangeListener;
import org.eclipse.core.databinding.property.value.ValueProperty;

/**
* This class is here because we have some model objects that have
methods that
* return observables. That's really great if you want to bind to that
property
* of the model object. The problem is when you want to do a
master-detail on
* to the property or if you want a IValueProperty for providing labels
in a table.
* This class can be used to create a Property object that can
* then be used in these situations.
*
* @param <S> type of the source object
* @param <V> type of the value, being the generic type of the
IObservableValue
* that is the observable property value
* @author Nigel Westbury
*/
abstract class PropertyOnObservable<S, V> extends
ValueProperty<S, V> {

private Class<V> valueClass;

PropertyOnObservable(Class<V> valueClass) {
this.valueClass = valueClass;
}

@Override
public Object getValueType() {
return valueClass;
}

@Override
public IObservableValue<V> observe(Realm realm,
final S source) {
/*
* This is a bit hacky. It would be nice if we could simply return
* the observable directly. Unfortunately the caller owns the
returned observable
* and may well dispose it. We don't own this observable hence all
this code.
*/
return new AbstractObservableValue<V>() {

@Override
public Object getValueType() {
return valueClass;
}

@Override
protected V doGetValue() {
return getObservable(source).getValue();
}

@Override
protected void doSetValue(V value) {
getObservable(source).setValue(value);
}

@Override
public void addValueChangeListener(IValueChangeListener<V> listener) {
getObservable(source).addValueChangeListener(listener);
}

@Override
public void removeValueChangeListener(IValueChangeListener<V> listener) {
getObservable(source).removeValueChangeListener(listener);
}

@Override
public void addChangeListener(IChangeListener listener) {
getObservable(source).addChangeListener(listener);
}

@Override
public void removeChangeListener(IChangeListener listener) {
getObservable(source).removeChangeListener(listener);
}
};
}

protected abstract IObservableValue<V> getObservable(S source);

}

On 25/11/2013 10:01, Leonid Ripeynih wrote:
> Hi all! I have a simple, and probably stupid question, but I just can't
> figure it out myself.
> Suppose, I have a bean, say like that:
>
>
> public class Wrapper {
>
> public IObservableValue getColumn1Value() {
> // actually returns correct observable value
> }
> public IObservableValue getColumn2Value() }
> // actually returns correct observable value
> }
> }
>
>
> A table viwer, and an observable list of such beans:
>
> TableViewer viewer = createTableViewer();
> viewer.setContentProvider(new ObservableListContentProvider());
> viewer.setLabelProvider(???);
> viewer.setInput(getObservableListOfWrappers());
>
>
> With simple LabelProvider it works fine on elements insertion/removal,
> but i really can't figure out how to bind Column1Value to column 1 and
> Column2Value to column 2, and make a values in columns updating when
> value changes.
> What's the best way of doing it?
>
> Thanks in advance!
Previous Topic:Ctrl + A in SourceViewer returns empty TextSelection
Next Topic:Data binding, validation and verification
Goto Forum:
  


Current Time: Sat Aug 02 00:33:56 EDT 2014

Powered by FUDForum. Page generated in 0.01934 seconds