Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » TreeViewer databinding question
TreeViewer databinding question [message #329615] Mon, 30 June 2008 09:21 Go to next message
Niklas Skeppstedt is currently offline Niklas SkeppstedtFriend
Messages: 2
Registered: July 2009
Junior Member
I have played with the new ObservableSetTreeContentProvider to add
databinding to our treeviewers. I got my inspiration from a snippet I
found(org.eclipse.jface.examples.databinding.snippets.Snippe t020TreeViewerWithSetFactory.java).
My problem is that in the example all the instances in the tree are of the
same class. Even the input. This makes everything very easy.
The example below is a reworked version of the snippet I went from.
It works with Person objects and it sets the input of the treeviewer to a
Person(which is never shown in the tree). In my case I would like to set
the input of the viewer to an object holding the root person, causing the
Person to be shown in the tree. From what I understand this can only be
achieved by having the two classes, Person and the class holding Person
objects inherit from same baseclass or implement same interface. Am I
missing soemthing here?

Snippet:

package niklas.testbench;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.map.IObservableMap;
import org.eclipse.core.databinding.observable.value.ComputedValue;
import org.eclipse.core.databinding.observable.value.IObservableVal ue;
import org.eclipse.core.databinding.observable.value.WritableValue;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.databinding.viewers.ObservableMapLabelProv ider;
import
org.eclipse.jface.databinding.viewers.ObservableSetTreeConte ntProvider;
import org.eclipse.jface.databinding.viewers.ViewersObservables;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.swt.widgets.TreeItem;

public class FamilyTreeApplication {

private Button pasteButton;
private Button copyButton;
private Shell shell;
private Button addChildPersonButton;
private Button removePersonButton;
private TreeViewer personViewer;
private Tree tree;
private Text personName;
private Text personAge;
private DataBindingContext m_bindingContext;

private Person input = createPerson("no name", "0");
private IObservableValue clipboard;
static int childCounter = 0;
static int ancestorCounter = 0;

/**
* Launch the application
*
* @param args
*/
public static void main(String[] args) {
Display display = Display.getDefault();
Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
public void run() {
try {
FamilyTreeApplication window = new FamilyTreeApplication();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Open the window
*/
public void open() {
final Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}

/**
* Create contents of the window
*/
protected void createContents() {
shell = new Shell();
final GridLayout gridLayout_1 = new GridLayout();
gridLayout_1.numColumns = 2;
shell.setLayout(gridLayout_1);
shell.setSize(535, 397);
shell.setText("Family Tree");

final Composite group = new Composite(shell, SWT.NONE);
final RowLayout rowLayout = new RowLayout();
rowLayout.marginTop = 0;
rowLayout.marginRight = 0;
rowLayout.marginLeft = 0;
rowLayout.marginBottom = 0;
rowLayout.pack = false;
group.setLayout(rowLayout);
group
.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false,
2, 1));

final Button addRootButton = new Button(group, SWT.NONE);
addRootButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
Set<Person> set = input.getChildren();
Person root = createPerson("Person " + (ancestorCounter++), null);
set.add(root);
input.setChildren(set);

personViewer.setSelection(new StructuredSelection(root));
personName.selectAll();
personName.setFocus();
}
});
addRootButton.setText("Add Ancestor");

addChildPersonButton = new Button(group, SWT.NONE);
addChildPersonButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
Person parent = getSelectedPerson();
Set<Person> set = new HashSet<Person>(parent.getChildren());
Person child = createPerson("Child " + (childCounter++), null);
set.add(child);
parent.setChildren(set);
personName.selectAll();
personName.setFocus();
}
});
addChildPersonButton.setText("Add Child");

removePersonButton = new Button(group, SWT.NONE);
removePersonButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
TreeItem selectedItem = personViewer.getTree().getSelection()[0];
Person person = (Person) selectedItem.getData();
TreeItem parentItem = selectedItem.getParentItem();
Person parent;
if (parentItem == null)
parent = input;
else
parent = (Person) parentItem.getData();

Set<Person> set = new HashSet<Person>(parent.getChildren());
set.remove(person);
parent.setChildren(set);
}
});
removePersonButton.setText("Remove");

copyButton = new Button(group, SWT.NONE);
copyButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
clipboard.setValue(getSelectedPerson());
}
});
copyButton.setText("Copy");

pasteButton = new Button(group, SWT.NONE);
pasteButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
Person copy = (Person) clipboard.getValue();
if (copy == null)
return;
Person parent = getSelectedPerson();
if (parent == null)
parent = input;

Set<Person> set = new HashSet<Person>(parent.getChildren());
set.add(copy);
parent.setChildren(set);

personViewer.setSelection(new StructuredSelection(copy));
personName.selectAll();
personName.setFocus();
}
});
pasteButton.setText("Paste");

final Button refreshButton = new Button(group, SWT.NONE);
refreshButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
personViewer.refresh();
}
});
refreshButton.setText("Refresh");

personViewer = new TreeViewer(shell, SWT.FULL_SELECTION | SWT.BORDER);
//
final TreeColumn codeColumn = new TreeColumn(personViewer.getTree(),
SWT.NONE);
codeColumn.setWidth(100);
codeColumn.setText("Name"); //$NON-NLS-1$

final TreeColumn codeColumn2 = new TreeColumn(personViewer.getTree(),
SWT.NONE);
codeColumn2.setWidth(100);
codeColumn2.setText("Age"); //$NON-NLS-1$

personViewer.getTree().setHeaderVisible(true);

//

personViewer.setUseHashlookup(true);
personViewer.setComparator(new ViewerComparator());
tree = personViewer.getTree();
tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));

final Label nameLabel = new Label(shell, SWT.NONE);
nameLabel.setText("Name");

personName = new Text(shell, SWT.BORDER);
final GridData gd_personName = new GridData(SWT.FILL, SWT.CENTER, true,
false);
personName.setLayoutData(gd_personName);

final Label ageLabel = new Label(shell, SWT.NONE);
ageLabel.setText("Age");

personAge = new Text(shell, SWT.BORDER);
final GridData gd_personAge = new GridData(SWT.FILL, SWT.CENTER, true,
false);
personAge.setLayoutData(gd_personAge);
m_bindingContext = initDataBindings();
//
initExtraBindings(m_bindingContext);
}

private static Person createPerson(String name, String age) {
return new Person(name, age);
}

protected DataBindingContext initDataBindings() {
IObservableValue treeViewerSelectionObserveSelection = ViewersObservables
.observeSingleSelection(personViewer);
IObservableValue nameTextObserveWidget = SWTObservables.observeText(
personName, SWT.Modify);
IObservableValue ageTextObserveWidget = SWTObservables.observeText(
personAge, SWT.Modify);
IObservableValue treeViewerNameValueObserveDetailValue = BeansObservables
.observeDetailValue(Realm.getDefault(),
treeViewerSelectionObserveSelection,
Person.NAME_PROPERTY, java.lang.String.class);
IObservableValue treeViewerAgeValueObserveDetailValue = BeansObservables
.observeDetailValue(Realm.getDefault(),
treeViewerSelectionObserveSelection,
Person.AGE_PROPERTY, java.lang.String.class);
//
//
DataBindingContext bindingContext = new DataBindingContext();
//
bindingContext.bindValue(nameTextObserveWidget,
treeViewerNameValueObserveDetailValue, null, null);
bindingContext.bindValue(ageTextObserveWidget,
treeViewerAgeValueObserveDetailValue, null, null);
//
return bindingContext;
}

private Person getSelectedPerson() {
IStructuredSelection selection = (IStructuredSelection) personViewer
.getSelection();
if (selection.isEmpty())
return null;
return (Person) selection.getFirstElement();
}

private void initExtraBindings(DataBindingContext dbc) {
final IObservableValue personViewerSelection = ViewersObservables
.observeSingleSelection(personViewer);
IObservableValue personSelected = new ComputedValue(Boolean.TYPE) {
@Override
protected Object calculate() {
return Boolean
.valueOf(personViewerSelection.getValue() != null);
}
};
dbc.bindValue(SWTObservables.observeEnabled(addChildPersonBu tton),
personSelected, null, null);
dbc.bindValue(SWTObservables.observeEnabled(removePersonButt on),
personSelected, null, null);

clipboard = new WritableValue();
dbc.bindValue(SWTObservables.observeEnabled(copyButton),
personSelected, null, null);
dbc.bindValue(SWTObservables.observeEnabled(pasteButton),
new ComputedValue(Boolean.TYPE) {
@Override
protected Object calculate() {
return Boolean.valueOf(clipboard.getValue() != null);
}
}, null, null);

bindTree(personViewer,"children",Person.class, new
String[]{Person.NAME_PROPERTY, Person.AGE_PROPERTY});

personViewer.setInput(input);
}


private void bindTree(TreeViewer viewer, String childrenProperty, Class
childrenClass, String[] propertyNames) {
ObservableSetTreeContentProvider contentProvider = new
ObservableSetTreeContentProvider(
BeansObservables.setFactory(Realm.getDefault(), childrenProperty,
childrenClass), null);
viewer.setContentProvider(contentProvider);

IObservableMap[] viewerLabelProviderMaps = BeansObservables
.observeMaps(contentProvider.getKnownElements(), childrenClass,
propertyNames);
viewer.setLabelProvider(new ObservableMapLabelProvider(
viewerLabelProviderMaps));
}

static class Person {
public static final String AGE_PROPERTY = "age";
public static final String NAME_PROPERTY = "name";
/* package */PropertyChangeSupport changeSupport = new
PropertyChangeSupport(
this);
private String personName;
private String personAge;
private Set<Person> children;

public Person(String name, String age) {
this.personName = name;
this.personAge = age == null ? "0" : age;
children = new HashSet<Person>();
}

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

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

public String getName() {
return personName;
}

public void setName(String value) {
changeSupport.firePropertyChange(NAME_PROPERTY, this.personName,
this.personName = value);
}

public String getAge() {
return personAge;
}

public void setAge(String value) {
changeSupport.firePropertyChange(AGE_PROPERTY, this.personAge,
this.personAge = value);
}

public Set<Person> getChildren() {
if (children == null)
return null;
return new HashSet<Person>(children);
}

public void setChildren(Set<Person> set) {
if (set != null)
set = new HashSet<Person>(set);
changeSupport.firePropertyChange("children", this.children,
this.children = set);
}

public boolean hasListeners(String propertyName) {
return changeSupport.hasListeners(propertyName);
}
}

}
Re: TreeViewer databinding question [message #329618 is a reply to message #329615] Mon, 30 June 2008 09:45 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
No you aren't. This question and problem has arisen also last week (see
the EMF-Newsgroup for a discussion about this) where we (me and others
trying to adopt this new support) encountered this "shortcoming" of the
TreeSupport.

The problem is two fold:
------------------------
- ObservableMap & ObservableMapLabelProvider naturally assume that all
objects passed in hold the attributes defined which is for trees not
always correct

- The value one sets as an input get's part of the knowElements-map and
hence passed to the ObservableMap. This is the biggest problem as far
as I can tell currently although support for different ObservableMaps
for different tree-levels would also make the whole thing more usable

Tom

Niklas Skeppstedt schrieb:
> I have played with the new ObservableSetTreeContentProvider to add
> databinding to our treeviewers. I got my inspiration from a snippet I
> found(org.eclipse.jface.examples.databinding.snippets.Snippe t020TreeViewerWithSetFactory.java).
> My problem is that in the example all the instances in the tree are of
> the same class. Even the input. This makes everything very easy. The
> example below is a reworked version of the snippet I went from.
> It works with Person objects and it sets the input of the treeviewer to
> a Person(which is never shown in the tree). In my case I would like to
> set the input of the viewer to an object holding the root person,
> causing the Person to be shown in the tree. From what I understand this
> can only be achieved by having the two classes, Person and the class
> holding Person objects inherit from same baseclass or implement same
> interface. Am I missing soemthing here?
>
> Snippet:
>
> package niklas.testbench;
>
> import java.beans.PropertyChangeListener;
> import java.beans.PropertyChangeSupport;
> import java.util.HashSet;
> import java.util.Set;
>
> import org.eclipse.core.databinding.DataBindingContext;
> import org.eclipse.core.databinding.beans.BeansObservables;
> import org.eclipse.core.databinding.observable.Realm;
> import org.eclipse.core.databinding.observable.map.IObservableMap;
> import org.eclipse.core.databinding.observable.value.ComputedValue;
> import org.eclipse.core.databinding.observable.value.IObservableVal ue;
> import org.eclipse.core.databinding.observable.value.WritableValue;
> import org.eclipse.jface.databinding.swt.SWTObservables;
> import org.eclipse.jface.databinding.viewers.ObservableMapLabelProv ider;
> import
> org.eclipse.jface.databinding.viewers.ObservableSetTreeConte ntProvider;
> import org.eclipse.jface.databinding.viewers.ViewersObservables;
> import org.eclipse.jface.viewers.IStructuredSelection;
> import org.eclipse.jface.viewers.StructuredSelection;
> import org.eclipse.jface.viewers.TreeViewer;
> import org.eclipse.jface.viewers.ViewerComparator;
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.events.SelectionAdapter;
> import org.eclipse.swt.events.SelectionEvent;
> import org.eclipse.swt.layout.GridData;
> import org.eclipse.swt.layout.GridLayout;
> import org.eclipse.swt.layout.RowLayout;
> import org.eclipse.swt.widgets.Button;
> import org.eclipse.swt.widgets.Composite;
> import org.eclipse.swt.widgets.Display;
> import org.eclipse.swt.widgets.Label;
> import org.eclipse.swt.widgets.Shell;
> import org.eclipse.swt.widgets.Text;
> import org.eclipse.swt.widgets.Tree;
> import org.eclipse.swt.widgets.TreeColumn;
> import org.eclipse.swt.widgets.TreeItem;
>
> public class FamilyTreeApplication {
>
> private Button pasteButton;
> private Button copyButton;
> private Shell shell;
> private Button addChildPersonButton;
> private Button removePersonButton;
> private TreeViewer personViewer;
> private Tree tree;
> private Text personName;
> private Text personAge;
> private DataBindingContext m_bindingContext;
>
> private Person input = createPerson("no name", "0");
> private IObservableValue clipboard;
> static int childCounter = 0;
> static int ancestorCounter = 0;
>
> /**
> * Launch the application
> * * @param args
> */
> public static void main(String[] args) {
> Display display = Display.getDefault();
> Realm.runWithDefault(SWTObservables.getRealm(display), new
> Runnable() {
> public void run() {
> try {
> FamilyTreeApplication window = new
> FamilyTreeApplication();
> window.open();
> } catch (Exception e) {
> e.printStackTrace();
> }
> }
> });
> }
>
> /**
> * Open the window
> */
> public void open() {
> final Display display = Display.getDefault();
> createContents();
> shell.open();
> shell.layout();
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch())
> display.sleep();
> }
> }
>
> /**
> * Create contents of the window
> */
> protected void createContents() {
> shell = new Shell();
> final GridLayout gridLayout_1 = new GridLayout();
> gridLayout_1.numColumns = 2;
> shell.setLayout(gridLayout_1);
> shell.setSize(535, 397);
> shell.setText("Family Tree");
>
> final Composite group = new Composite(shell, SWT.NONE);
> final RowLayout rowLayout = new RowLayout();
> rowLayout.marginTop = 0;
> rowLayout.marginRight = 0;
> rowLayout.marginLeft = 0;
> rowLayout.marginBottom = 0;
> rowLayout.pack = false;
> group.setLayout(rowLayout);
> group
> .setLayoutData(new GridData(SWT.FILL, SWT.FILL, false,
> false,
> 2, 1));
>
> final Button addRootButton = new Button(group, SWT.NONE);
> addRootButton.addSelectionListener(new SelectionAdapter() {
> @Override
> public void widgetSelected(final SelectionEvent e) {
> Set<Person> set = input.getChildren();
> Person root = createPerson("Person " +
> (ancestorCounter++), null);
> set.add(root);
> input.setChildren(set);
>
> personViewer.setSelection(new StructuredSelection(root));
> personName.selectAll();
> personName.setFocus();
> }
> });
> addRootButton.setText("Add Ancestor");
>
> addChildPersonButton = new Button(group, SWT.NONE);
> addChildPersonButton.addSelectionListener(new SelectionAdapter() {
> @Override
> public void widgetSelected(final SelectionEvent e) {
> Person parent = getSelectedPerson();
> Set<Person> set = new
> HashSet<Person>(parent.getChildren());
> Person child = createPerson("Child " + (childCounter++),
> null);
> set.add(child);
> parent.setChildren(set);
> personName.selectAll();
> personName.setFocus();
> }
> });
> addChildPersonButton.setText("Add Child");
>
> removePersonButton = new Button(group, SWT.NONE);
> removePersonButton.addSelectionListener(new SelectionAdapter() {
> @Override
> public void widgetSelected(final SelectionEvent e) {
> TreeItem selectedItem =
> personViewer.getTree().getSelection()[0];
> Person person = (Person) selectedItem.getData();
> TreeItem parentItem = selectedItem.getParentItem();
> Person parent;
> if (parentItem == null)
> parent = input;
> else
> parent = (Person) parentItem.getData();
>
> Set<Person> set = new
> HashSet<Person>(parent.getChildren());
> set.remove(person);
> parent.setChildren(set);
> }
> });
> removePersonButton.setText("Remove");
>
> copyButton = new Button(group, SWT.NONE);
> copyButton.addSelectionListener(new SelectionAdapter() {
> @Override
> public void widgetSelected(final SelectionEvent e) {
> clipboard.setValue(getSelectedPerson());
> }
> });
> copyButton.setText("Copy");
>
> pasteButton = new Button(group, SWT.NONE);
> pasteButton.addSelectionListener(new SelectionAdapter() {
> @Override
> public void widgetSelected(final SelectionEvent e) {
> Person copy = (Person) clipboard.getValue();
> if (copy == null)
> return;
> Person parent = getSelectedPerson();
> if (parent == null)
> parent = input;
>
> Set<Person> set = new
> HashSet<Person>(parent.getChildren());
> set.add(copy);
> parent.setChildren(set);
>
> personViewer.setSelection(new StructuredSelection(copy));
> personName.selectAll();
> personName.setFocus();
> }
> });
> pasteButton.setText("Paste");
>
> final Button refreshButton = new Button(group, SWT.NONE);
> refreshButton.addSelectionListener(new SelectionAdapter() {
> @Override
> public void widgetSelected(final SelectionEvent e) {
> personViewer.refresh();
> }
> });
> refreshButton.setText("Refresh");
>
> personViewer = new TreeViewer(shell, SWT.FULL_SELECTION |
> SWT.BORDER);
> //
> final TreeColumn codeColumn = new
> TreeColumn(personViewer.getTree(),
> SWT.NONE);
> codeColumn.setWidth(100);
> codeColumn.setText("Name"); //$NON-NLS-1$
>
> final TreeColumn codeColumn2 = new
> TreeColumn(personViewer.getTree(),
> SWT.NONE);
> codeColumn2.setWidth(100);
> codeColumn2.setText("Age"); //$NON-NLS-1$
>
> personViewer.getTree().setHeaderVisible(true);
>
> //
>
> personViewer.setUseHashlookup(true);
> personViewer.setComparator(new ViewerComparator());
> tree = personViewer.getTree();
> tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true,
> 2, 1));
>
> final Label nameLabel = new Label(shell, SWT.NONE);
> nameLabel.setText("Name");
>
> personName = new Text(shell, SWT.BORDER);
> final GridData gd_personName = new GridData(SWT.FILL,
> SWT.CENTER, true,
> false);
> personName.setLayoutData(gd_personName);
>
> final Label ageLabel = new Label(shell, SWT.NONE);
> ageLabel.setText("Age");
>
> personAge = new Text(shell, SWT.BORDER);
> final GridData gd_personAge = new GridData(SWT.FILL, SWT.CENTER,
> true,
> false);
> personAge.setLayoutData(gd_personAge);
> m_bindingContext = initDataBindings();
> //
> initExtraBindings(m_bindingContext);
> }
>
> private static Person createPerson(String name, String age) {
> return new Person(name, age);
> }
>
> protected DataBindingContext initDataBindings() {
> IObservableValue treeViewerSelectionObserveSelection =
> ViewersObservables
> .observeSingleSelection(personViewer);
> IObservableValue nameTextObserveWidget =
> SWTObservables.observeText(
> personName, SWT.Modify);
> IObservableValue ageTextObserveWidget = SWTObservables.observeText(
> personAge, SWT.Modify);
> IObservableValue treeViewerNameValueObserveDetailValue =
> BeansObservables
> .observeDetailValue(Realm.getDefault(),
> treeViewerSelectionObserveSelection,
> Person.NAME_PROPERTY, java.lang.String.class);
> IObservableValue treeViewerAgeValueObserveDetailValue =
> BeansObservables
> .observeDetailValue(Realm.getDefault(),
> treeViewerSelectionObserveSelection,
> Person.AGE_PROPERTY, java.lang.String.class);
> //
> //
> DataBindingContext bindingContext = new DataBindingContext();
> //
> bindingContext.bindValue(nameTextObserveWidget,
> treeViewerNameValueObserveDetailValue, null, null);
> bindingContext.bindValue(ageTextObserveWidget,
> treeViewerAgeValueObserveDetailValue, null, null);
> //
> return bindingContext;
> }
>
> private Person getSelectedPerson() {
> IStructuredSelection selection = (IStructuredSelection)
> personViewer
> .getSelection();
> if (selection.isEmpty())
> return null;
> return (Person) selection.getFirstElement();
> }
>
> private void initExtraBindings(DataBindingContext dbc) {
> final IObservableValue personViewerSelection = ViewersObservables
> .observeSingleSelection(personViewer);
> IObservableValue personSelected = new ComputedValue(Boolean.TYPE) {
> @Override
> protected Object calculate() {
> return Boolean
> .valueOf(personViewerSelection.getValue() != null);
> }
> };
> dbc.bindValue(SWTObservables.observeEnabled(addChildPersonBu tton),
> personSelected, null, null);
> dbc.bindValue(SWTObservables.observeEnabled(removePersonButt on),
> personSelected, null, null);
>
> clipboard = new WritableValue();
> dbc.bindValue(SWTObservables.observeEnabled(copyButton),
> personSelected, null, null);
> dbc.bindValue(SWTObservables.observeEnabled(pasteButton),
> new ComputedValue(Boolean.TYPE) {
> @Override
> protected Object calculate() {
> return Boolean.valueOf(clipboard.getValue() !=
> null);
> }
> }, null, null);
>
> bindTree(personViewer,"children",Person.class, new
> String[]{Person.NAME_PROPERTY, Person.AGE_PROPERTY});
>
> personViewer.setInput(input);
> }
>
>
> private void bindTree(TreeViewer viewer, String childrenProperty,
> Class childrenClass, String[] propertyNames) {
> ObservableSetTreeContentProvider contentProvider = new
> ObservableSetTreeContentProvider(
> BeansObservables.setFactory(Realm.getDefault(),
> childrenProperty,
> childrenClass), null);
> viewer.setContentProvider(contentProvider);
>
> IObservableMap[] viewerLabelProviderMaps = BeansObservables
> .observeMaps(contentProvider.getKnownElements(),
> childrenClass,
> propertyNames);
> viewer.setLabelProvider(new ObservableMapLabelProvider(
> viewerLabelProviderMaps));
> }
>
> static class Person {
> public static final String AGE_PROPERTY = "age";
> public static final String NAME_PROPERTY = "name";
> /* package */PropertyChangeSupport changeSupport = new
> PropertyChangeSupport(
> this);
> private String personName;
> private String personAge;
> private Set<Person> children;
>
> public Person(String name, String age) {
> this.personName = name;
> this.personAge = age == null ? "0" : age;
> children = new HashSet<Person>();
> }
>
> public void addPropertyChangeListener(PropertyChangeListener
> listener) {
> changeSupport.addPropertyChangeListener(listener);
> }
>
> public void removePropertyChangeListener(PropertyChangeListener
> listener) {
> changeSupport.removePropertyChangeListener(listener);
> }
>
> public String getName() {
> return personName;
> }
>
> public void setName(String value) {
> changeSupport.firePropertyChange(NAME_PROPERTY,
> this.personName,
> this.personName = value);
> }
>
> public String getAge() {
> return personAge;
> }
>
> public void setAge(String value) {
> changeSupport.firePropertyChange(AGE_PROPERTY, this.personAge,
> this.personAge = value);
> }
>
> public Set<Person> getChildren() {
> if (children == null)
> return null;
> return new HashSet<Person>(children);
> }
>
> public void setChildren(Set<Person> set) {
> if (set != null)
> set = new HashSet<Person>(set);
> changeSupport.firePropertyChange("children", this.children,
> this.children = set);
> }
>
> public boolean hasListeners(String propertyName) {
> return changeSupport.hasListeners(propertyName);
> }
> }
>
> }
>


--
B e s t S o l u t i o n . at
------------------------------------------------------------ --------
Tom Schindl JFace-Committer
------------------------------------------------------------ --------
Re: TreeViewer databinding question [message #329809 is a reply to message #329618] Sat, 05 July 2008 20:18 Go to previous message
Boris Bokowski is currently offline Boris BokowskiFriend
Messages: 272
Registered: July 2009
Senior Member
> - ObservableMap & ObservableMapLabelProvider naturally assume that all
> objects passed in hold the attributes defined which is for trees not
> always correct

The current (beans, pojo, and EMF) implementations of ObservableMap make
this assumption. You could implement your own... (and contribute it back)
;-)

> - The value one sets as an input get's part of the knowElements-map and
> hence passed to the ObservableMap. This is the biggest problem as far
> as I can tell currently although support for different ObservableMaps
> for different tree-levels would also make the whole thing more usable

I believe this was fixed very recently, for 3.4.1 and 3.5.

Boris
Previous Topic:Annotation
Next Topic:Search values in Variables View in the debug perspective
Goto Forum:
  


Current Time: Sat Sep 28 07:21:49 GMT 2024

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

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

Back to the top