Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » [Databinding] Problems with DelegatingValueProperty
[Databinding] Problems with DelegatingValueProperty [message #488964] Wed, 30 September 2009 21:55 Go to next message
Heiko Ahlig is currently offline Heiko Ahlig
Messages: 62
Registered: July 2009
Member
This is a multi-part message in MIME format.
--------------000707060301000307080502
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

Hi,
in consquence of Matthews answer for my last question/ problems ->

http://www.eclipse.org/forums/index.php?t=msg&th=153839& amp;start=0&S=a3c7e77489cf06ebc8fb6630bff94d9b

I try to use the DelegatingValueProperty.class in a more complex scenario.

And now I have the next problems when I work there with abstract classes
and generic types.

In my example I try 2 versions to build a DelegatingValueProperty

1.Version:
------------------------------------
IValueProperty viewProp = new DelegatingValueProperty(){
IValueProperty
dataCompositeProp=BeanProperties.value(DataComposite.class, "basic");
IValueProperty personProp = BeanProperties.value(Person.class, "name");
IValueProperty carProp = BeanProperties.value(Car.class, "marke");
IValueProperty cityProp = BeanProperties.value(City.class, "name");

protected IValueProperty doGetDelegate(Object source) {
if (source instanceof DataComposite){
DataComposite comp=(DataComposite)source;
if(comp.getBasic() instanceof Person){
return dataCompositeProp.value(personProp);}
else if(comp.getBasic() instanceof Car){
return dataCompositeProp.value(carProp);}
else if(comp.getBasic() instanceof City){
return dataCompositeProp.value(cityProp);}
return null;
}
else if(source instanceof Person){
return personProp;}
else if(source instanceof Car){
return carProp;}
else if(source instanceof City){
return cityProp;}
return null;
}};

It doesn't give errors but it also shows not all Labels, but I don't
know if:
valuePro.value(detailValueProp)
it's useable.


My 2. version generate binding errors like:
java.lang.IllegalArgumentException: Could not find property with name
name in class class de.miku.jfacetree.SnippetProblemTreeViewer$MainEntity
at
org.eclipse.core.internal.databinding.beans.BeanPropertyHelp er.getPropertyDescriptor(BeanPropertyHelper.java:177)
at
org.eclipse.core.databinding.beans.BeanProperties.value(Bean Properties.java:116)

.......

2. Version:
------------------------------------
IValueProperty viewProp = new DelegatingValueProperty(){
IValueProperty
dataCompositeProp=BeanProperties.value(DataComposite.class, "basic");
IValueProperty personProp = BeanProperties.value(Person.class, "name");
IValueProperty carProp = BeanProperties.value(Car.class, "marke");
IValueProperty cityProp = BeanProperties.value(City.class, "name");

protected IValueProperty doGetDelegate(Object source) {
if (source instanceof DataComposite){
DataComposite comp=(DataComposite)source;
if(comp.getBasic() instanceof Person){
return BeanProperties.value(DataComposite.class,"basic.name");}
else if(comp.getBasic() instanceof Car){
return BeanProperties.value(DataComposite.class,"basic.marke");}
else if(comp.getBasic() instanceof City){
return BeanProperties.value(DataComposite.class, "basic.name");}
return null;
}
else if(source instanceof Person){
return personProp;}
else if(source instanceof Car){
return carProp;}
else if(source instanceof City){
return cityProp;}
return null;
}};


Do are my aporachs completely wrong, or it is only a small error?


Thanks in advance

Heiko



--------------000707060301000307080502
Content-Type: text/plain;
name="SnippetProblemTreeViewer.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="SnippetProblemTreeViewer.java"

package de.miku.jfacetree;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import org.eclipse.core.databinding.beans.BeanProperties;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.map.IObservableMap;
import org.eclipse.core.databinding.property.list.IListProperty;
import org.eclipse.core.databinding.property.list.MultiListProperty ;
import org.eclipse.core.databinding.property.value.DelegatingValueP roperty;
import org.eclipse.core.databinding.property.value.IValueProperty;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.databinding.viewers.ObservableListTreeCont entProvider;
import org.eclipse.jface.databinding.viewers.ObservableMapCellLabel Provider;
import org.eclipse.jface.layout.TreeColumnLayout;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.TreeViewerColumn;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
* @since 3.2
*
*/
public class SnippetProblemTreeViewer {
protected Shell shell;
private TreeViewer viewer;
private Composite composite;
private Model model;

/**
* Launch the application
*
* @param args
*/
public static void main(String[] args) {
try {
SnippetProblemTreeViewer window = new SnippetProblemTreeViewer();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Open the window
*/
public void open() {
final Display display = Display.getDefault();
Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
public void run() {
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
});
}

protected void createContents() {
shell = new Shell();
shell.setSize(509, 375);
shell.setText("SnippetProblemTreeViewer.java");
shell.setLayout(new GridLayout());
composite = new Composite(shell, SWT.NONE);
viewer = new TreeViewer(composite, SWT.FULL_SELECTION | SWT.MULTI
| SWT.BORDER);

bindUI();
}

// Minimal JavaBeans support
public abstract class AbstractModelObject {
private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(
this);

public void addPropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.addPropertyChangeListener(listener);
}

public void removePropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.removePropertyChangeListener(listener) ;
}

protected void firePropertyChange(String propertyName, Object oldValue,
Object newValue) {
propertyChangeSupport.firePropertyChange(propertyName, oldValue,
newValue);
}
}

public abstract class MainEntity extends AbstractModelObject {
// only dummy at this example
}

public class Car extends MainEntity {
private String marke;
private String sign;

public Car(String marke, String sign) {
super();
this.marke = marke;
this.sign = sign;
}

public String getMarke() {
return marke;
}

public void setMarke(String marke) {
firePropertyChange("marke", this.marke, this.marke = marke);
}

public String getSign() {
return sign;
}

public void setSign(String sign) {
firePropertyChange("sign", this.sign, this.sign = sign);
}
}

public class City extends MainEntity {
private String name;
private String zipCode;

public City(String zipCode, String name) {
super();
this.zipCode = zipCode;
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
firePropertyChange("name", this.name, this.name = name);
}

public String getZipCode() {
return zipCode;
}

public void setZipCode(String zipCode) {
firePropertyChange("zipCode", this.zipCode, this.zipCode = zipCode);
}
}

public class Person extends MainEntity {
private String name;
private int age;

public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}

public String getName() {
return name;
}

public void setName(String name) {
firePropertyChange("name", this.name, this.name = name);
}

public int getAge() {
return age;
}

public void setAge(int age) {
firePropertyChange("age", this.age, this.age = age);
}
}

public class DataComposite<T extends MainEntity> extends
AbstractModelObject {

private T basic;
private List<Car> cars;
private List<Person> persons;
private List<City> cities;

public void setBasic(T basic) {
this.basic = basic;
}

public T getBasic() {
return basic;
}

public void setCars(List<Car> cars) {
firePropertyChange("cars", this.cars, this.cars = cars);
}

public List<Car> getCars() {
if (this.cars == null) {
this.cars = new ArrayList<Car>();
}
return new ArrayList<Car>(this.cars);
}

public void setPersons(List<Person> persons) {
firePropertyChange("persons", this.persons, this.persons = persons);
}

public List<Person> getPersons() {
if (this.persons == null) {
this.persons = new ArrayList<Person>();
}
return new ArrayList<Person>(persons);
}

public void setCities(List<City> cities) {
firePropertyChange("cities", this.cities, this.cities = cities);
}

public List<City> getCities() {
if (this.cities == null) {
this.cities = new ArrayList<City>();
}
return new ArrayList<City>(cities);
}
}

public class Model extends AbstractModelObject {
private List<DataComposite<? extends MainEntity>> result;

public List<DataComposite<? extends MainEntity>> getResult() {
if (result == null) {
result = new ArrayList<DataComposite<? extends MainEntity>>();
}
return new ArrayList<DataComposite<? extends MainEntity>>(result);
}

public void setResult(List<DataComposite<? extends MainEntity>> result) {
firePropertyChange("result", this.result, this.result = result);
}

public Model() {
init();
}

public void init() {
ArrayList<DataComposite<? extends MainEntity>> myList = new ArrayList<DataComposite<? extends MainEntity>>();
DataComposite<City> dci1 = new DataComposite<City>();
dci1.setBasic(getRandomCity());
dci1.setCars(getCarList());
myList.add(dci1);
DataComposite<City> dci2 = new DataComposite<City>();
dci2.setBasic(getRandomCity());
dci2.setPersons(getPersonList());
myList.add(dci2);
DataComposite<Person> dp = new DataComposite<Person>();
dp.setBasic(getRandomPerson());
dp.setCities(getCityList());
dp.setCars(getCarList());
myList.add(dp);

setResult(myList);
}

private Random random = new Random();

private Person[] persons = new Person[] { new Person("Heiko", 32),
new Person("Joachim", 56), new Person("Ellen", 52),
new Person("Uwe", 32), new Person("Albert", 80),
new Person("Irmtraud", 78), new Person("Thomas", 32),
new Person("Karsten", 56), new Person("Nadin", 52), };

private Person getRandomPerson() {
int x = random.nextInt(persons.length);
return persons[x];
}

private List<Person> getPersonList() {
return Arrays.asList(persons);
}

private Car[] cars = new Car[] { new Car("Volvo", "B-B-001"),
new Car("Opel", "Y-B-001"), new Car("VW", "BW-B-601"),
new Car("Audi", "BA-B-001"), new Car("Porsche", "WE-K-001"), };

private Car getRandomCar() {
int x = random.nextInt(cars.length);
return cars[x];
}

private List<Car> getCarList() {
return Arrays.asList(cars);
}

private City[] cities = new City[] { new City("14057", "Berlin"),
new City("06543", "Wippra"), new City("18004", "Leipzig"),
new City("55116", "Mainz"), new City("13123", "Berlin"), };

private City getRandomCity() {
int x = random.nextInt(cities.length);
return cities[x];
}

private List<City> getCityList() {
return Arrays.asList(cities);
}
}

public class ModObservableMapCellLabelProvider extends
ObservableMapCellLabelProvider {

Image compositeImage = createCompositeImage();
Image personImage = createPersonImage();
Image carImage = createCarImage();
Image cityImage = createCityImage();

public ModObservableMapCellLabelProvider(IObservableMap attributeMap) {
super(attributeMap);
}

private Image createCarImage() {
Display display = Display.getCurrent();
Image catalogImage = new Image(display, 12, 12);
GC gc = new GC(catalogImage);
gc.setBackground(display.getSystemColor(SWT.COLOR_GREEN));
gc.fillArc(1, 1, 10, 10, 0, 360);
gc.dispose();
return catalogImage;
}

private Image createCityImage() {
Display display = Display.getCurrent();
Image catalogImage = new Image(display, 12, 12);
GC gc = new GC(catalogImage);
gc.setBackground(display.getSystemColor(SWT.COLOR_RED));
gc.fillArc(1, 1, 10, 10, 0, 360);
gc.dispose();
return catalogImage;
}

private Image createPersonImage() {
Display display = Display.getCurrent();
Image catalogImage = new Image(display, 12, 12);
GC gc = new GC(catalogImage);
gc.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
gc.fillPolygon(new int[] { 1, 10, 5, 1, 6, 1, 10, 10, 1, 10 });
gc.dispose();
return catalogImage;
}

private Image createCompositeImage() {
Display display = Display.getCurrent();
Image catalogImage = new Image(display, 12, 12);
GC gc = new GC(catalogImage);
gc.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
gc.fillPolygon(new int[] { 1, 10, 5, 1, 6, 1, 10, 10, 1, 10 });
gc.dispose();
return catalogImage;
}

@Override
public void update(ViewerCell cell) {
super.update(cell);
if (cell.getElement() instanceof DataComposite) {
cell.setImage(compositeImage);
} else if (cell.getElement() instanceof Person) {
cell.setImage(personImage);
} else if (cell.getElement() instanceof Car) {
cell.setImage(carImage);
} else if (cell.getElement() instanceof City) {
cell.setImage(cityImage);
}

}
}

private void bindUI() {
IListProperty childrenProperty = new MultiListProperty(
new IListProperty[] {
/*
* Why doesn't work this?
*/
// BeanProperties.list(Model.class, "result"),
// BeanProperties.list(DataComposite.class,"persons"),
// BeanProperties.list(DataComposite.class,"cars"),
// BeanProperties.list(DataComposite.class, "cities")
BeanProperties.list("result"),
BeanProperties.list("persons"),
BeanProperties.list("cars"),
BeanProperties.list("cities") });

ObservableListTreeContentProvider contentProvider = new ObservableListTreeContentProvider(
childrenProperty.listFactory(), null);
viewer.setContentProvider(contentProvider);

TreeViewerColumn column = new TreeViewerColumn(viewer, SWT.LEFT);
TreeColumnLayout viewerLayout = new TreeColumnLayout();
viewerLayout.setColumnData(column.getColumn(),
new ColumnWeightData(100));
composite.setLayout(viewerLayout);
composite.setLayoutData(new GridData(GridData.FILL_BOTH));
Button reinitModelButton = new Button(shell, SWT.PUSH);
reinitModelButton.setText("Reinit Model");
reinitModelButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
model.init();
}
});

/*
* Version 1
*/
IValueProperty viewProp = new DelegatingValueProperty() {
IValueProperty dataCompositeProp = BeanProperties.value(
DataComposite.class, "basic");
IValueProperty personProp = BeanProperties.value(Person.class,
"name");
IValueProperty carProp = BeanProperties.value(Car.class, "marke");
IValueProperty cityProp = BeanProperties.value(City.class, "name");

protected IValueProperty doGetDelegate(Object source) {
if (source instanceof DataComposite) {
DataComposite comp = (DataComposite) source;
if (comp.getBasic() instanceof Person) {
return dataCompositeProp.value(personProp);
} else if (comp.getBasic() instanceof Car) {
return dataCompositeProp.value(carProp);
} else if (comp.getBasic() instanceof City) {
return dataCompositeProp.value(cityProp);
}
return null;
} else if (source instanceof Person) {
return personProp;
} else if (source instanceof Car) {
return carProp;
} else if (source instanceof City) {
return cityProp;
}

return null;
}
};

/*
* Version 2
*/
// IValueProperty viewProp = new DelegatingValueProperty(){
// IValueProperty
// dataCompositeProp=BeanProperties.value(DataComposite.class, "basic");
// IValueProperty personProp = BeanProperties.value(Person.class,
// "name");
// IValueProperty carProp = BeanProperties.value(Car.class, "marke");
// IValueProperty cityProp = BeanProperties.value(City.class, "name");
//
//
// protected IValueProperty doGetDelegate(Object source) {
// if (source instanceof DataComposite){
// DataComposite comp=(DataComposite)source;
// if(comp.getBasic() instanceof Person){
// return BeanProperties.value(DataComposite.class,"basic.name");
// }
// else if(comp.getBasic() instanceof Car){
// return BeanProperties.value(DataComposite.class,"basic.marke");
// }
// else if(comp.getBasic() instanceof City){
// return BeanProperties.value(DataComposite.class, "basic.name");
// }
// return null;
// }
// else if(source instanceof Person){
// return personProp;
// }
// else if(source instanceof Car){
// return carProp;
// }
// else if(source instanceof City){
// return cityProp;
// }
//
// return null;
// }
// };

contentProvider.getKnownElements();
IObservableMap details = viewProp.observeDetail(contentProvider
.getKnownElements());
column.setLabelProvider(new ModObservableMapCellLabelProvider(details));

model = new Model();
viewer.setInput(model);
}
}

--------------000707060301000307080502--
Re: [Databinding] Problems with DelegatingValueProperty [message #489220 is a reply to message #488964] Thu, 01 October 2009 22:12 Go to previous messageGo to next message
Matthew Hall is currently offline Matthew Hall
Messages: 368
Registered: July 2009
Senior Member
Heiko Ahlig wrote:
> I try to use the DelegatingValueProperty.class in a more complex
scenario.
>
> And now I have the next problems when I work there with abstract classes
> and generic types.
>
> In my example I try 2 versions to build a DelegatingValueProperty

> Do are my aporachs completely wrong, or it is only a small error?

Here is the detail you're missing (from
DelegatingValueProperty.doGetDelegate javadoc):

* Implementers must ensure that repeated calls to this method with the same
* source object returns the same delegate instance.

I have one of two solutions to propose, depending on whether the
DataComposite.basic property is mutable.

Matthew
Re: [Databinding] Problems with DelegatingValueProperty -> Solved [message #489528 is a reply to message #489220] Sun, 04 October 2009 14:17 Go to previous messageGo to next message
Heiko Ahlig is currently offline Heiko Ahlig
Messages: 62
Registered: July 2009
Member
This is a multi-part message in MIME format.
--------------050607040209070206050008
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

Hi Matthew,
thanks for your quick answer,
now it works fine.

But why are the properties-APi nowhere to find?

Greetings
Heiko



Am 02.10.2009 00:12, schrieb Matthew Hall:
> Heiko Ahlig wrote:
> > I try to use the DelegatingValueProperty.class in a more complex
> scenario.
> >
> > And now I have the next problems when I work there with abstract classes
> > and generic types.
> >
> > In my example I try 2 versions to build a DelegatingValueProperty
>
> > Do are my aporachs completely wrong, or it is only a small error?
>
> Here is the detail you're missing (from
> DelegatingValueProperty.doGetDelegate javadoc):
>
> * Implementers must ensure that repeated calls to this method with the same
> * source object returns the same delegate instance.
>
> I have one of two solutions to propose, depending on whether the
> DataComposite.basic property is mutable.
>
> Matthew


--------------050607040209070206050008
Content-Type: text/plain;
name="SnippetProblemTreeViewer_Solved.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="SnippetProblemTreeViewer_Solved.java"

package de.miku.jfacetree;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import org.eclipse.core.databinding.beans.BeanProperties;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.map.IObservableMap;
import org.eclipse.core.databinding.property.list.IListProperty;
import org.eclipse.core.databinding.property.list.MultiListProperty ;
import org.eclipse.core.databinding.property.value.DelegatingValueP roperty;
import org.eclipse.core.databinding.property.value.IValueProperty;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.databinding.viewers.ObservableListTreeCont entProvider;
import org.eclipse.jface.databinding.viewers.ObservableMapCellLabel Provider;
import org.eclipse.jface.layout.TreeColumnLayout;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.TreeViewerColumn;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
* @since 3.2
*
*/
public class SnippetProblemTreeViewer_Solved {
protected Shell shell;
private TreeViewer viewer;
private Composite composite;
private Model model;

/**
* Launch the application
*
* @param args
*/
public static void main(String[] args) {
try {
SnippetProblemTreeViewer_Solved window = new SnippetProblemTreeViewer_Solved();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Open the window
*/
public void open() {
final Display display = Display.getDefault();
Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
public void run() {
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
});
}

protected void createContents() {
shell = new Shell();
shell.setSize(509, 375);
shell.setText("SnippetProblemTreeViewer.java");
shell.setLayout(new GridLayout());
composite = new Composite(shell, SWT.NONE);
viewer = new TreeViewer(composite, SWT.FULL_SELECTION | SWT.MULTI
| SWT.BORDER);

bindUI();
}

// Minimal JavaBeans support
public abstract class AbstractModelObject {
private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(
this);

public void addPropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.addPropertyChangeListener(listener);
}

public void removePropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.removePropertyChangeListener(listener) ;
}

protected void firePropertyChange(String propertyName, Object oldValue,
Object newValue) {
propertyChangeSupport.firePropertyChange(propertyName, oldValue,
newValue);
}
}

public abstract class MainEntity extends AbstractModelObject {
// only dummy at this example
}

public class Car extends MainEntity {
private String marke;
private String sign;

public Car(String marke, String sign) {
super();
this.marke = marke;
this.sign = sign;
}

public String getMarke() {
return marke;
}

public void setMarke(String marke) {
firePropertyChange("marke", this.marke, this.marke = marke);
}

public String getSign() {
return sign;
}

public void setSign(String sign) {
firePropertyChange("sign", this.sign, this.sign = sign);
}
}

public class City extends MainEntity {
private String name;
private String zipCode;

public City(String zipCode, String name) {
super();
this.zipCode = zipCode;
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
firePropertyChange("name", this.name, this.name = name);
}

public String getZipCode() {
return zipCode;
}

public void setZipCode(String zipCode) {
firePropertyChange("zipCode", this.zipCode, this.zipCode = zipCode);
}
}

public class Person extends MainEntity {
private String name;
private int age;

public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}

public String getName() {
return name;
}

public void setName(String name) {
firePropertyChange("name", this.name, this.name = name);
}

public int getAge() {
return age;
}

public void setAge(int age) {
firePropertyChange("age", this.age, this.age = age);
}
}

public class DataComposite<T extends MainEntity> extends
AbstractModelObject {

private T basic;
private List<Car> cars;
private List<Person> persons;
private List<City> cities;

public void setBasic(T basic) {
this.basic = basic;
}

public T getBasic() {
return basic;
}

public void setCars(List<Car> cars) {
firePropertyChange("cars", this.cars, this.cars = cars);
}

public List<Car> getCars() {
if (this.cars == null) {
this.cars = new ArrayList<Car>();
}
return new ArrayList<Car>(this.cars);
}

public void setPersons(List<Person> persons) {
firePropertyChange("persons", this.persons, this.persons = persons);
}

public List<Person> getPersons() {
if (this.persons == null) {
this.persons = new ArrayList<Person>();
}
return new ArrayList<Person>(persons);
}

public void setCities(List<City> cities) {
firePropertyChange("cities", this.cities, this.cities = cities);
}

public List<City> getCities() {
if (this.cities == null) {
this.cities = new ArrayList<City>();
}
return new ArrayList<City>(cities);
}
}

public class Model extends AbstractModelObject {
private List<DataComposite<? extends MainEntity>> result;

public List<DataComposite<? extends MainEntity>> getResult() {
if (result == null) {
result = new ArrayList<DataComposite<? extends MainEntity>>();
}
return new ArrayList<DataComposite<? extends MainEntity>>(result);
}

public void setResult(List<DataComposite<? extends MainEntity>> result) {
firePropertyChange("result", this.result, this.result = result);
}

public Model() {
init();
}

public void init() {
ArrayList<DataComposite<? extends MainEntity>> myList = new ArrayList<DataComposite<? extends MainEntity>>();
DataComposite<City> dci1 = new DataComposite<City>();
dci1.setBasic(getRandomCity());
dci1.setCars(getCarList());
myList.add(dci1);
DataComposite<City> dci2 = new DataComposite<City>();
dci2.setBasic(getRandomCity());
dci2.setPersons(getPersonList());
myList.add(dci2);
DataComposite<Person> dp = new DataComposite<Person>();
dp.setBasic(getRandomPerson());
dp.setCities(getCityList());
dp.setCars(getCarList());
myList.add(dp);

setResult(myList);
}

private Random random = new Random();

private Person[] persons = new Person[] { new Person("Heiko", 32),
new Person("Joachim", 56), new Person("Ellen", 52),
new Person("Uwe", 32), new Person("Albert", 80),
new Person("Irmtraud", 78), new Person("Thomas", 32),
new Person("Karsten", 56), new Person("Nadin", 52), };

private Person getRandomPerson() {
int x = random.nextInt(persons.length);
return persons[x];
}

private List<Person> getPersonList() {
return Arrays.asList(persons);
}

private Car[] cars = new Car[] { new Car("Volvo", "B-B-001"),
new Car("Opel", "Y-B-001"), new Car("VW", "BW-B-601"),
new Car("Audi", "BA-B-001"), new Car("Porsche", "WE-K-001"), };

private Car getRandomCar() {
int x = random.nextInt(cars.length);
return cars[x];
}

private List<Car> getCarList() {
return Arrays.asList(cars);
}

private City[] cities = new City[] { new City("14057", "Berlin"),
new City("06543", "Wippra"), new City("18004", "Leipzig"),
new City("55116", "Mainz"), new City("13123", "Berlin"), };

private City getRandomCity() {
int x = random.nextInt(cities.length);
return cities[x];
}

private List<City> getCityList() {
return Arrays.asList(cities);
}
}

public class ModObservableMapCellLabelProvider extends
ObservableMapCellLabelProvider {

Image compositeImage = createCompositeImage();
Image personImage = createPersonImage();
Image carImage = createCarImage();
Image cityImage = createCityImage();

public ModObservableMapCellLabelProvider(IObservableMap attributeMap) {
super(attributeMap);
}

private Image createCarImage() {
Display display = Display.getCurrent();
Image catalogImage = new Image(display, 12, 12);
GC gc = new GC(catalogImage);
gc.setBackground(display.getSystemColor(SWT.COLOR_GREEN));
gc.fillArc(1, 1, 10, 10, 0, 360);
gc.dispose();
return catalogImage;
}

private Image createCityImage() {
Display display = Display.getCurrent();
Image catalogImage = new Image(display, 12, 12);
GC gc = new GC(catalogImage);
gc.setBackground(display.getSystemColor(SWT.COLOR_RED));
gc.fillArc(1, 1, 10, 10, 0, 360);
gc.dispose();
return catalogImage;
}

private Image createPersonImage() {
Display display = Display.getCurrent();
Image catalogImage = new Image(display, 12, 12);
GC gc = new GC(catalogImage);
gc.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
gc.fillPolygon(new int[] { 1, 10, 5, 1, 6, 1, 10, 10, 1, 10 });
gc.dispose();
return catalogImage;
}

private Image createCompositeImage() {
Display display = Display.getCurrent();
Image catalogImage = new Image(display, 12, 12);
GC gc = new GC(catalogImage);
gc.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
gc.fillPolygon(new int[] { 1, 10, 5, 1, 6, 1, 10, 10, 1, 10 });
gc.dispose();
return catalogImage;
}

@Override
public void update(ViewerCell cell) {
super.update(cell);
if (cell.getElement() instanceof DataComposite) {
cell.setImage(compositeImage);
} else if (cell.getElement() instanceof Person) {
cell.setImage(personImage);
} else if (cell.getElement() instanceof Car) {
cell.setImage(carImage);
} else if (cell.getElement() instanceof City) {
cell.setImage(cityImage);
}

}
}

private void bindUI() {
IListProperty childrenProperty = new MultiListProperty(
new IListProperty[] {
BeanProperties.list("result"),
BeanProperties.list("persons"),
BeanProperties.list("cars"),
BeanProperties.list("cities") });

ObservableListTreeContentProvider contentProvider = new ObservableListTreeContentProvider(
childrenProperty.listFactory(), null);
viewer.setContentProvider(contentProvider);

TreeViewerColumn column = new TreeViewerColumn(viewer, SWT.LEFT);
TreeColumnLayout viewerLayout = new TreeColumnLayout();
viewerLayout.setColumnData(column.getColumn(),
new ColumnWeightData(100));
composite.setLayout(viewerLayout);
composite.setLayoutData(new GridData(GridData.FILL_BOTH));
Button reinitModelButton = new Button(shell, SWT.PUSH);
reinitModelButton.setText("Reinit Model");
reinitModelButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
model.init();
}
});

/*
* Version 1
*/
IValueProperty viewProp = new DelegatingValueProperty() {
IValueProperty dataCompositeProp = BeanProperties.value(DataComposite.class, "basic");
IValueProperty personProp = BeanProperties.value(Person.class,"name");
IValueProperty basicPersonProp=dataCompositeProp.value(personProp);
IValueProperty carProp = BeanProperties.value(Car.class, "marke");
IValueProperty basicCarProp=dataCompositeProp.value(carProp);
IValueProperty cityProp = BeanProperties.value(City.class, "name");
IValueProperty basicCityProp=dataCompositeProp.value(cityProp);

protected IValueProperty doGetDelegate(Object source) {
if (source instanceof DataComposite) {
DataComposite comp = (DataComposite) source;
if (comp.getBasic() instanceof Person) {
return basicPersonProp;
} else if (comp.getBasic() instanceof Car) {
return basicCarProp;
} else if (comp.getBasic() instanceof City) {
return basicCityProp;
}
return null;
} else if (source instanceof Person) {
return personProp;
} else if (source instanceof Car) {
return carProp;
} else if (source instanceof City) {
return cityProp;
}

return null;
}
};

/*
* Version 2
*/
// IValueProperty viewProp = new DelegatingValueProperty(){
// IValueProperty
// dataCompositeProp=BeanProperties.value(DataComposite.class, "basic");
// IValueProperty personProp = BeanProperties.value(Person.class,
// "name");
// IValueProperty carProp = BeanProperties.value(Car.class, "marke");
// IValueProperty cityProp = BeanProperties.value(City.class, "name");
//
//
// protected IValueProperty doGetDelegate(Object source) {
// if (source instanceof DataComposite){
// DataComposite comp=(DataComposite)source;
// if(comp.getBasic() instanceof Person){
// return BeanProperties.value(DataComposite.class,"basic.name");
// }
// else if(comp.getBasic() instanceof Car){
// return BeanProperties.value(DataComposite.class,"basic.marke");
// }
// else if(comp.getBasic() instanceof City){
// return BeanProperties.value(DataComposite.class, "basic.name");
// }
// return null;
// }
// else if(source instanceof Person){
// return personProp;
// }
// else if(source instanceof Car){
// return carProp;
// }
// else if(source instanceof City){
// return cityProp;
// }
//
// return null;
// }
// };

contentProvider.getKnownElements();
IObservableMap details = viewProp.observeDetail(contentProvider
.getKnownElements());
column.setLabelProvider(new ModObservableMapCellLabelProvider(details));

model = new Model();
viewer.setInput(model);
}
}

--------------050607040209070206050008--
Re: [Databinding] Problems with DelegatingValueProperty -> Solved [message #489569 is a reply to message #489528] Mon, 05 October 2009 04:13 Go to previous message
Matthew Hall is currently offline Matthew Hall
Messages: 368
Registered: July 2009
Senior Member
That was my mistake--I neglected to update the javadoc build files in
time for the Galileo release. This has been fixed in the 3.5.1
maintenance release (the fix is also in 3.6M2). If you update to one of
those versions you will be able to access the javadocs for these new
classes in the built-in help system.

Matthew

Heiko Ahlig wrote:
> Hi Matthew,
> thanks for your quick answer,
> now it works fine.
>
> But why are the properties-APi nowhere to find?
>
> Greetings
> Heiko
>
>
>
> Am 02.10.2009 00:12, schrieb Matthew Hall:
>> Heiko Ahlig wrote:
>> > I try to use the DelegatingValueProperty.class in a more complex
>> scenario.
>> >
>> > And now I have the next problems when I work there with abstract
>> classes
>> > and generic types.
>> >
>> > In my example I try 2 versions to build a DelegatingValueProperty
>>
>> > Do are my aporachs completely wrong, or it is only a small error?
>>
>> Here is the detail you're missing (from
>> DelegatingValueProperty.doGetDelegate javadoc):
>>
>> * Implementers must ensure that repeated calls to this method with the
>> same
>> * source object returns the same delegate instance.
>>
>> I have one of two solutions to propose, depending on whether the
>> DataComposite.basic property is mutable.
>>
>> Matthew
>
Previous Topic:How to drag to reorder TabItem in a TabFolder?
Next Topic:Drag and Drop with view does not function
Goto Forum:
  


Current Time: Mon Oct 20 22:56:33 GMT 2014

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

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