Home » Eclipse Projects » JFace » Bind selected Observable to TreeViewer
Bind selected Observable to TreeViewer [message #654384] |
Tue, 15 February 2011 08:58  |
Eclipse User |
|
|
|
Originally posted by: quincy.cs.gmail.com
This is a multi-part message in MIME format.
------=_NextPart_000_000B_01CBCD5B.8A4692B0
Content-Type: text/plain;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable
I came across a question when trying to isolate an unrelated bug.
I'm trying to change Snippet018CheckboxTableViewerCheckedSelection into =
a treeviewer example of a similar concept.
To explain Snippet018CheckboxTableViewerCheckedSelection's concept in =
order to contrast/compare,
First, a table with two columns: name of people, and list of friends. =
So this first table depicts all people and their respective friends.
Second, a table with one column which is just the names of all people. =
Each name has a checkbox.
The concept is after selecting a person's name from the first table, the =
second table is filled with the respective checks for the friends of the =
selected person. In other words, modify a selected person's friends.
------------
Ok now what I want.
I want to keep the first table. The second, I want to modify. =20
The second table would instead contain a tree and is empty unless a =
person is selected. It still has one column and each row has a =
checkbox.
The concept is after selecting a person's name from the first table, the =
second table is filled with friend assignment possibilities which may =
differ from person to person. There would be a hierarchy where the =
first level is Company and second level is employees of that company. =
For all purposes a company is completely the same as a person. =20
Ok now to the technical issues. How do you bind the selection of the =
first table as the model which should display it=E2=80=99s friend =
assignment possibilities? Below is the fragment showing the binds I =
have tried. The first binding runs but has nothing to do with the =
selection of the first table. The second binding does not run because =
it throws a runtime error due to ViewerSupport trying to set the content =
provider of the viewer to =E2=80=98ObservableSetContentProvider=E2=80=99 =
then the viewer rejecting it in an assert:
Assert.isTrue(provider instanceof ITreeContentProvider
|| provider instanceof ITreePathContentProvider);
/*
* this runs, but NOT what I want.
*/
ViewerSupport.bind(friendsTreeViewer, viewModelPplTree,=20
BeanProperties.set("friends",Person.class),=20
BeanProperties.value(Person.class, "name")); =
=20
/*
* this doesn't run, but it IS what I want.
*/
// ViewerSupport.bind(friendsTreeViewer,=20
// =
BeansObservables.observeDetailSet(selectedPerson,"friends",Person.class),=
// BeanProperties.value(Person.class, "name"));
FINALLY THE QUESTION,
Is it possible then to bind a selected observable to a treeviewer?
Below is the full snippet.
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.BeanProperties;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.observable.value.ComputedValue;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.databinding.viewers.ViewerSupport;
import org.eclipse.jface.databinding.viewers.ViewersObservables;
import org.eclipse.jface.dialogs.IInputValidator;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.jface.layout.TableColumnLayout;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.CheckboxTreeViewer;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
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.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
public class SnippetCheckboxTreeViewerCheckedSelection {
public static void main(String[] args) {
// The SWT event loop
final Display display =3D Display.getDefault();
Realm.runWithDefault(SWTObservables.getRealm(display), new =
Runnable() {
public void run() {
ViewModel viewModel =3D createSampleModel();
ViewModelPeopleTree viewModelPplTree =3D new =
ViewModelPeopleTree();
viewModelPplTree.setFriends(new =
HashSet(viewModel.getPeople()));
Shell shell =3D new =
View(viewModel,viewModelPplTree).createShell();
shell.open();
while (!shell.isDisposed())
if (!display.readAndDispatch())
display.sleep();
}
});
display.dispose();
}
private static ViewModel createSampleModel() {
ViewModel viewModel =3D new ViewModel();
Person stan =3D createPerson("Stan");
Person kyle =3D createPerson("Kyle");
Person eric =3D createPerson("Eric");
Person kenny =3D createPerson("Kenny");
Person wendy =3D createPerson("Wendy");
Person butters =3D createPerson("Butters");
setFriends(stan, new Person[] { kyle, eric, kenny, wendy });
setFriends(kyle, new Person[] { stan, eric, kenny });
setFriends(eric, new Person[] { eric });
setFriends(kenny, new Person[] { stan, kyle, eric });
setFriends(wendy, new Person[] { stan });
setFriends(butters, new Person[0]);
Person[] people =3D new Person[] { stan, kyle, eric, kenny, =
wendy,
butters };
viewModel.setPeople(Arrays.asList(people));
return viewModel;
}
private static Person createPerson(String name) {
Person person =3D new Person();
person.setName(name);
return person;
}
private static void setFriends(Person person, Person[] friends) {
person.setFriends(new HashSet(Arrays.asList(friends)));
}
// Minimal JavaBeans support
public static abstract class AbstractModelObject {
private PropertyChangeSupport propertyChangeSupport =3D new =
PropertyChangeSupport(
this);
public void addPropertyChangeListener(PropertyChangeListener =
listener) {
propertyChangeSupport.addPropertyChangeListener(listener);
}
public void addPropertyChangeListener(String propertyName,
PropertyChangeListener listener) {
=
propertyChangeSupport.addPropertyChangeListener(propertyName,
listener);
}
public void removePropertyChangeListener(PropertyChangeListener =
listener) {
=
propertyChangeSupport.removePropertyChangeListener(listener);
}
public void removePropertyChangeListener(String propertyName,
PropertyChangeListener listener) {
=
propertyChangeSupport.removePropertyChangeListener(propertyName,
listener);
}
protected void firePropertyChange(String propertyName, Object =
oldValue,
Object newValue) {
propertyChangeSupport.firePropertyChange(propertyName, =
oldValue,
newValue);
}
}
// The data model class.
static class Person extends AbstractModelObject {
private String name;
private Set friends =3D new HashSet();
private Set<Person> children;
public String getName() {
return name;
}
public void setName(String name) {
firePropertyChange("name", this.name, this.name =3D name);
}
public Set getFriends() {
return new HashSet(friends);
}
public void setFriends(Set friends) {
firePropertyChange("friends", this.friends,
this.friends =3D new HashSet(friends));
}
=20
public String toString() {
// String str =3D name ;
// if(getChildren()!=3Dnull && getChildren().size()>0){
// str +=3D " children {";
// =20
// for(Person child : getChildren()){
// str +=3D " [" + child.getName() +"] ";
// }
// =20
// str +=3D "}";
// }
// =20
return name;
}
}
static class ViewModel extends AbstractModelObject {
private List people =3D new ArrayList();
public List getPeople() {
return new ArrayList(people);
}
public void setPeople(List people) {
firePropertyChange("people", this.people,
this.people =3D new ArrayList(people));
}
}
=20
static class ViewModelPeopleTree extends Person {
=20
}
// The GUI view
static class View {
private ViewModel viewModel;
private Shell shell;
private TableViewer peopleViewer;
private Text personName;
private CheckboxTreeViewer friendsTreeViewer;
private ViewModelPeopleTree viewModelPplTree;
public View(ViewModel viewModel, ViewModelPeopleTree =
viewModelPplTree) {
this.viewModel =3D viewModel;
this.viewModelPplTree =3D viewModelPplTree;
}
public Shell createShell() {
// Build a UI
final Display display =3D Display.getCurrent();
shell =3D new Shell(display);
createUI(shell);
// Bind UI
bindUI();
// Open and return the Shell
shell.setSize(shell.computeSize(400, SWT.DEFAULT));
shell.open();
return shell;
}
private void createUI(Shell shell) {
shell.setText("Binding checked elements in =
CheckboxTableViewer");
shell.setLayout(new GridLayout(2, false));
new Label(shell, SWT.NONE).setText("People");
Composite peopleComposite =3D new Composite(shell, =
SWT.NONE);
GridDataFactory.fillDefaults().grab(true, true).span(2, =
1).applyTo(
peopleComposite);
TableColumnLayout peopleColumnLayout =3D new =
TableColumnLayout();
peopleComposite.setLayout(peopleColumnLayout);
peopleViewer =3D new TableViewer(peopleComposite, SWT.SINGLE
| SWT.BORDER | SWT.FULL_SELECTION);
Table peopleTable =3D peopleViewer.getTable();
peopleTable.setHeaderVisible(true);
peopleTable.setLinesVisible(true);
TableColumn nameColumn =3D new TableColumn(peopleTable, =
SWT.NONE);
nameColumn.setText("Name");
peopleColumnLayout.setColumnData(nameColumn,
new ColumnWeightData(1));
TableColumn friendsColumn =3D new TableColumn(peopleTable, =
SWT.NONE);
friendsColumn.setText("Friends");
peopleColumnLayout.setColumnData(friendsColumn,
new ColumnWeightData(3));
new Label(shell, SWT.NONE).setText("Name");
personName =3D new Text(shell, SWT.BORDER);
GridDataFactory.fillDefaults().grab(true, false)
.applyTo(personName);
new Label(shell, SWT.NONE).setText("Friends");
Composite friendsComposite =3D new =
Composite(shell,SWT.NONE);
friendsComposite.setLayoutData(new =
GridData(GridData.FILL_BOTH));
friendsComposite.setLayout(new GridLayout(1, false)); =
friendsTreeViewer =3D new =
CheckboxTreeViewer(friendsComposite);
Tree friendsTree =3D friendsTreeViewer.getTree();
friendsTree.setHeaderVisible(true);
friendsTree.setLinesVisible(true);
TreeColumn friendNameColumn =3D new TreeColumn(friendsTree,
SWT.NONE);
friendNameColumn.setText("Name");
friendNameColumn.setWidth(100);
}
private void bindUI() {
DataBindingContext dbc =3D new DataBindingContext();
final IObservableList people =3D =
BeansObservables.observeList(Realm
.getDefault(), viewModel, "people");
=20
ViewerSupport.bind(peopleViewer, people, =
BeanProperties.values(
Person.class, new String[] { "name", "friends" }));
final IObservableValue selectedPerson =3D ViewersObservables
.observeSingleSelection(peopleViewer);
dbc.bindValue(SWTObservables.observeText(personName, =
SWT.Modify),
BeansObservables.observeDetailValue(selectedPerson, =
"name",
String.class));
/*
* original binding for table.
*/
// ViewerSupport.bind(friendsTreeViewer, people, =
BeanProperties.value(
// Person.class, "name"));
/*
* this runs, but not what I want.
*/
ViewerSupport.bind(friendsTreeViewer, viewModelPplTree,=20
BeanProperties.set("friends",Person.class),=20
BeanProperties.value(Person.class, "name")); =
=20
/*
* this doesn't run, but its what I want.
*/
// ViewerSupport.bind(friendsTreeViewer,=20
// =
BeansObservables.observeDetailSet(selectedPerson,"friends",Person.class),=
// BeanProperties.value(Person.class, "name"));
=20
//no change
dbc.bindSet(ViewersObservables.observeCheckedElements(
friendsTreeViewer, Person.class), BeansObservables
.observeDetailSet(selectedPerson, "friends", =
Person.class));
=20
}
}
}
------=_NextPart_000_000B_01CBCD5B.8A4692B0
Content-Type: text/html;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable
<HTML><HEAD></HEAD>
<BODY dir=3Dltr>
<DIV dir=3Dltr>
<DIV style=3D"FONT-FAMILY: 'Calibri'; COLOR: #000000; FONT-SIZE: 12pt">
<DIV>I came across a question when trying to isolate an unrelated =
bug.</DIV>
<DIV> </DIV>
<DIV>I'm trying to change Snippet018CheckboxTableViewerCheckedSelection =
into a=20
treeviewer example of a similar concept.</DIV>
<DIV> </DIV>
<DIV>To explain Snippet018CheckboxTableViewerCheckedSelection's concept =
in order=20
to contrast/compare,</DIV>
<DIV> </DIV>
<DIV>First, a table with two columns: name of people, and list of =
friends. =20
So this first table depicts all people and their respective =
friends.</DIV>
<DIV>Second, a table with one column which is just the names of all=20
people. Each name has a checkbox.</DIV>
<DIV> </DIV>
<DIV>The concept is after selecting a person's name from the first =
table, the=20
second table is filled with the respective checks for the friends of the =
selected person. In other words, modify a selected person's =
friends.</DIV>
<DIV> </DIV>
<DIV>------------</DIV>
<DIV>Ok now what I want.</DIV>
<DIV> </DIV>
<DIV>I want to keep the first table. The second, I want to =
modify. =20
</DIV>
<DIV> </DIV>
<DIV>The second table would instead contain a tree and is empty unless a =
person=20
is selected. It still has one column and each row has a =
checkbox.</DIV>
<DIV> </DIV>
<DIV>The concept is after selecting a person's name from the first =
table, the=20
second table is filled with friend assignment possibilities which may =
differ=20
from person to person. There would be a hierarchy where the first =
level is=20
Company and second level is employees of that company. For all =
purposes a=20
company is completely the same as a person. </DIV>
<DIV> </DIV>
<DIV>Ok now to the technical issues. How do you bind the selection =
of the=20
first table as the model which should display it=E2=80=99s friend =
assignment=20
possibilities? Below is the fragment showing the binds I have =
tried. =20
The first binding runs but has nothing to do with the selection of the =
first=20
table. The second binding does not run because it throws a runtime =
error=20
due to ViewerSupport trying to set the content provider of the viewer to =
=E2=80=98ObservableSetContentProvider=E2=80=99 then the viewer rejecting =
it in an assert:</DIV>
<DIV> </DIV>
<DIV></DIV>
<DIV>Assert.isTrue(provider instanceof ITreeContentProvider</DIV>
<DIV> &n=
bsp; =20
|| provider instanceof ITreePathContentProvider);</DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV></DIV>
<DIV> =
/*</DIV>
<DIV> &n=
bsp; *=20
this runs, but NOT what I want.</DIV>
<DIV> &n=
bsp;=20
*/</DIV>
<DIV> =20
ViewerSupport.bind(friendsTreeViewer, viewModelPplTree, </DIV>
<DIV> &n=
bsp; =20
BeanProperties.set("friends",Person.class), </DIV>
<DIV> &n=
bsp; =20
BeanProperties.value(Person.class,=20
"name")); &nbs=
p;=20
</DIV>
<DIV> </DIV>
<DIV> =
/*</DIV>
<DIV> &n=
bsp; *=20
this doesn't run, but it IS what I want.</DIV>
<DIV> &n=
bsp;=20
*/</DIV>
<DIV>// =
=20
ViewerSupport.bind(friendsTreeViewer, </DIV>
<DIV>// =
=20
BeansObservables.observeDetailSet(selectedPerson,"friends",Person.class),=
</DIV>
<DIV>// =
=20
BeanProperties.value(Person.class, "name"));</DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV>FINALLY THE QUESTION,</DIV>
<DIV>Is it possible then to bind a selected observable to a =
treeviewer?</DIV>
<DIV> </DIV>
<DIV>Below is the full snippet.</DIV>
<DIV> </DIV>
<DIV></DIV>
<DIV> </DIV>
<DIV>import java.beans.PropertyChangeListener;</DIV>
<DIV>import java.beans.PropertyChangeSupport;</DIV>
<DIV>import java.util.ArrayList;</DIV>
<DIV>import java.util.Arrays;</DIV>
<DIV>import java.util.HashSet;</DIV>
<DIV>import java.util.List;</DIV>
<DIV>import java.util.Set;</DIV>
<DIV> </DIV>
<DIV>import org.eclipse.core.databinding.DataBindingContext;</DIV>
<DIV>import org.eclipse.core.databinding.beans.BeanProperties;</DIV>
<DIV>import org.eclipse.core.databinding.beans.BeansObservables;</DIV>
<DIV>import org.eclipse.core.databinding.observable.Realm;</DIV>
<DIV>import =
org.eclipse.core.databinding.observable.list.IObservableList;</DIV>
<DIV>import =
org.eclipse.core.databinding.observable.value.ComputedValue;</DIV>
<DIV>import=20
org.eclipse.core.databinding.observable.value.IObservableValue;</DIV>
<DIV>import org.eclipse.jface.databinding.swt.SWTObservables;</DIV>
<DIV>import org.eclipse.jface.databinding.viewers.ViewerSupport;</DIV>
<DIV>import =
org.eclipse.jface.databinding.viewers.ViewersObservables;</DIV>
<DIV>import org.eclipse.jface.dialogs.IInputValidator;</DIV>
<DIV>import org.eclipse.jface.dialogs.InputDialog;</DIV>
<DIV>import org.eclipse.jface.dialogs.MessageDialog;</DIV>
<DIV>import org.eclipse.jface.layout.GridDataFactory;</DIV>
<DIV>import org.eclipse.jface.layout.GridLayoutFactory;</DIV>
<DIV>import org.eclipse.jface.layout.TableColumnLayout;</DIV>
<DIV>import org.eclipse.jface.viewers.CheckboxTableViewer;</DIV>
<DIV>import org.eclipse.jface.viewers.CheckboxTreeViewer;</DIV>
<DIV>import org.eclipse.jface.viewers.ColumnWeightData;</DIV>
<DIV>import org.eclipse.jface.viewers.IStructuredSelection;</DIV>
<DIV>import org.eclipse.jface.viewers.StructuredSelection;</DIV>
<DIV>import org.eclipse.jface.viewers.TableViewer;</DIV>
<DIV>import org.eclipse.jface.window.Window;</DIV>
<DIV>import org.eclipse.swt.SWT;</DIV>
<DIV>import org.eclipse.swt.layout.GridData;</DIV>
<DIV>import org.eclipse.swt.layout.GridLayout;</DIV>
<DIV>import org.eclipse.swt.widgets.Button;</DIV>
<DIV>import org.eclipse.swt.widgets.Composite;</DIV>
<DIV>import org.eclipse.swt.widgets.Display;</DIV>
<DIV>import org.eclipse.swt.widgets.Event;</DIV>
<DIV>import org.eclipse.swt.widgets.Label;</DIV>
<DIV>import org.eclipse.swt.widgets.Listener;</DIV>
<DIV>import org.eclipse.swt.widgets.Shell;</DIV>
<DIV>import org.eclipse.swt.widgets.Table;</DIV>
<DIV>import org.eclipse.swt.widgets.TableColumn;</DIV>
<DIV>import org.eclipse.swt.widgets.Text;</DIV>
<DIV>import org.eclipse.swt.widgets.Tree;</DIV>
<DIV>import org.eclipse.swt.widgets.TreeColumn;</DIV>
<DIV> </DIV>
<DIV>public class SnippetCheckboxTreeViewerCheckedSelection {</DIV>
<DIV> </DIV>
<DIV> public static void main(String[] args) {</DIV>
<DIV> // The SWT event =
loop</DIV>
<DIV> final Display display =
=3D=20
Display.getDefault();</DIV>
<DIV> =20
Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() =
{</DIV>
<DIV> =
public=20
void run() {</DIV>
<DIV> &n=
bsp; =20
ViewModel viewModel =3D createSampleModel();</DIV>
<DIV> &n=
bsp; =20
ViewModelPeopleTree viewModelPplTree =3D new =
ViewModelPeopleTree();</DIV>
<DIV> &n=
bsp; =20
viewModelPplTree.setFriends(new HashSet(viewModel.getPeople()));</DIV>
<DIV> </DIV>
<DIV> &n=
bsp; =20
Shell shell =3D new =
View(viewModel,viewModelPplTree).createShell();</DIV>
<DIV> &n=
bsp; =20
shell.open();</DIV>
<DIV> &n=
bsp; =20
while (!shell.isDisposed())</DIV>
<DIV> &n=
bsp; =20
if (!display.readAndDispatch())</DIV>
<DIV> &n=
bsp; =20
display.sleep();</DIV>
<DIV> =
}</DIV>
<DIV> });</DIV>
<DIV> display.dispose();</DIV>
<DIV> }</DIV>
<DIV> </DIV>
<DIV> private static ViewModel createSampleModel() =
{</DIV>
<DIV> ViewModel viewModel =3D =
new=20
ViewModel();</DIV>
<DIV> </DIV>
<DIV> Person stan =3D=20
createPerson("Stan");</DIV>
<DIV> Person kyle =3D=20
createPerson("Kyle");</DIV>
<DIV> Person eric =3D=20
createPerson("Eric");</DIV>
<DIV> Person kenny =3D=20
createPerson("Kenny");</DIV>
<DIV> Person wendy =3D=20
createPerson("Wendy");</DIV>
<DIV> Person butters =3D=20
createPerson("Butters");</DIV>
<DIV> </DIV>
<DIV> setFriends(stan, new =
Person[] {=20
kyle, eric, kenny, wendy });</DIV>
<DIV> setFriends(kyle, new =
Person[] {=20
stan, eric, kenny });</DIV>
<DIV> setFriends(eric, new =
Person[] {=20
eric });</DIV>
<DIV> setFriends(kenny, new =
Person[] {=20
stan, kyle, eric });</DIV>
<DIV> setFriends(wendy, new =
Person[] {=20
stan });</DIV>
<DIV> setFriends(butters, new=20
Person[0]);</DIV>
<DIV> </DIV>
<DIV> Person[] people =3D new =
Person[] {=20
stan, kyle, eric, kenny, wendy,</DIV>
<DIV> &n=
bsp; =20
butters };</DIV>
<DIV> =20
viewModel.setPeople(Arrays.asList(people));</DIV>
<DIV> return viewModel;</DIV>
<DIV> }</DIV>
<DIV> </DIV>
<DIV> private static Person createPerson(String name) =
{</DIV>
<DIV> Person person =3D new=20
Person();</DIV>
<DIV> =
person.setName(name);</DIV>
<DIV> return person;</DIV>
<DIV> }</DIV>
<DIV> </DIV>
<DIV> private static void setFriends(Person person, =
Person[]=20
friends) {</DIV>
<DIV> person.setFriends(new=20
HashSet(Arrays.asList(friends)));</DIV>
<DIV> }</DIV>
<DIV> </DIV>
<DIV> // Minimal JavaBeans support</DIV>
<DIV> public static abstract class AbstractModelObject =
{</DIV>
<DIV> private =
PropertyChangeSupport=20
propertyChangeSupport =3D new PropertyChangeSupport(</DIV>
<DIV> &n=
bsp; =20
this);</DIV>
<DIV> </DIV>
<DIV> public void=20
addPropertyChangeListener(PropertyChangeListener listener) {</DIV>
<DIV> =20
propertyChangeSupport.addPropertyChangeListener(listener);</DIV>
<DIV> }</DIV>
<DIV> </DIV>
<DIV> public void=20
addPropertyChangeListener(String propertyName,</DIV>
<DIV> &n=
bsp; =20
PropertyChangeListener listener) {</DIV>
<DIV> =20
propertyChangeSupport.addPropertyChangeListener(propertyName,</DIV>
<DIV> &n=
bsp; =20
listener);</DIV>
<DIV> }</DIV>
<DIV> </DIV>
<DIV> public void=20
removePropertyChangeListener(PropertyChangeListener listener) {</DIV>
<DIV> =20
propertyChangeSupport.removePropertyChangeListener(listener);</DIV>
<DIV> }</DIV>
<DIV> </DIV>
<DIV> public void=20
removePropertyChangeListener(String propertyName,</DIV>
<DIV> &n=
bsp; =20
PropertyChangeListener listener) {</DIV>
<DIV> =20
propertyChangeSupport.removePropertyChangeListener(propertyName,</DIV>
<DIV> &n=
bsp; =20
listener);</DIV>
<DIV> }</DIV>
<DIV> </DIV>
<DIV> protected void=20
firePropertyChange(String propertyName, Object oldValue,</DIV>
<DIV> &n=
bsp; =20
Object newValue) {</DIV>
<DIV> =20
propertyChangeSupport.firePropertyChange(propertyName, oldValue,</DIV>
<DIV> &n=
bsp; =20
newValue);</DIV>
<DIV> }</DIV>
<DIV> }</DIV>
<DIV> </DIV>
<DIV> // The data model class.</DIV>
<DIV> static class Person extends AbstractModelObject =
{</DIV>
<DIV> private String =
name;</DIV>
<DIV> private Set friends =3D =
new=20
HashSet();</DIV>
<DIV> private=20
Set<Person> children;</DIV>
<DIV> </DIV>
<DIV> public String getName() =
{</DIV>
<DIV> =
return=20
name;</DIV>
<DIV> }</DIV>
<DIV> </DIV>
<DIV> public void =
setName(String name)=20
{</DIV>
<DIV> =20
firePropertyChange("name", this.name, this.name =3D name);</DIV>
<DIV> }</DIV>
<DIV> </DIV>
<DIV> public Set getFriends() =
{</DIV>
<DIV> =
return=20
new HashSet(friends);</DIV>
<DIV> }</DIV>
<DIV> </DIV>
<DIV> public void =
setFriends(Set=20
friends) {</DIV>
<DIV> =20
firePropertyChange("friends", this.friends,</DIV>
<DIV> &n=
bsp; =20
this.friends =3D new HashSet(friends));</DIV>
<DIV> }</DIV>
<DIV> </DIV>
<DIV> public String toString() =
{</DIV>
<DIV>// =
String=20
str =3D name ;</DIV>
<DIV>// =
=20
if(getChildren()!=3Dnull && getChildren().size()>0){</DIV>
<DIV>// =
=20
str +=3D " children {";</DIV>
<DIV>// =
=20
</DIV>
<DIV>// =
=20
for(Person child : getChildren()){</DIV>
<DIV>// =
=20
str +=3D " [" + child.getName() +"] ";</DIV>
<DIV>// =
=20
}</DIV>
<DIV>// =
=20
</DIV>
<DIV>// =
=20
str +=3D "}";</DIV>
<DIV>// =
=20
}</DIV>
<DIV>// =
</DIV>
<DIV> =
return=20
name;</DIV>
<DIV> }</DIV>
<DIV> }</DIV>
<DIV> </DIV>
<DIV> static class ViewModel extends =
AbstractModelObject=20
{</DIV>
<DIV> private List people =3D =
new=20
ArrayList();</DIV>
<DIV> </DIV>
<DIV> public List getPeople() =
{</DIV>
<DIV> =
return=20
new ArrayList(people);</DIV>
<DIV> }</DIV>
<DIV> </DIV>
<DIV> public void =
setPeople(List=20
people) {</DIV>
<DIV> =20
firePropertyChange("people", this.people,</DIV>
<DIV> &n=
bsp; =20
this.people =3D new ArrayList(people));</DIV>
<DIV> }</DIV>
<DIV> }</DIV>
<DIV> </DIV>
<DIV> static class ViewModelPeopleTree extends Person =
{</DIV>
<DIV> </DIV>
<DIV> }</DIV>
<DIV> </DIV>
<DIV> // The GUI view</DIV>
<DIV> static class View {</DIV>
<DIV> private ViewModel=20
viewModel;</DIV>
<DIV> </DIV>
<DIV> private Shell =
shell;</DIV>
<DIV> </DIV>
<DIV> private TableViewer=20
peopleViewer;</DIV>
<DIV> private Text =
personName;</DIV>
<DIV> private =
CheckboxTreeViewer=20
friendsTreeViewer;</DIV>
<DIV> </DIV>
<DIV> private=20
ViewModelPeopleTree viewModelPplTree;</DIV>
<DIV> </DIV>
<DIV> public View(ViewModel =
viewModel,=20
ViewModelPeopleTree viewModelPplTree) {</DIV>
<DIV> =20
this.viewModel =3D viewModel;</DIV>
<DIV> =20
this.viewModelPplTree =3D viewModelPplTree;</DIV>
<DIV> }</DIV>
<DIV> </DIV>
<DIV> public Shell =
createShell()=20
{</DIV>
<DIV> =
// Build=20
a UI</DIV>
<DIV> =
final=20
Display display =3D Display.getCurrent();</DIV>
<DIV> =
shell =3D=20
new Shell(display);</DIV>
<DIV> </DIV>
<DIV> =20
createUI(shell);</DIV>
<DIV> </DIV>
<DIV> =
// Bind=20
UI</DIV>
<DIV> =20
bindUI();</DIV>
<DIV> </DIV>
<DIV> =
// Open=20
and return the Shell</DIV>
<DIV> =20
shell.setSize(shell.computeSize(400, SWT.DEFAULT));</DIV>
<DIV> =20
shell.open();</DIV>
<DIV> =
return=20
shell;</DIV>
<DIV> }</DIV>
<DIV> </DIV>
<DIV> private void =
createUI(Shell=20
shell) {</DIV>
<DIV> =20
shell.setText("Binding checked elements in CheckboxTableViewer");</DIV>
<DIV> =20
shell.setLayout(new GridLayout(2, false));</DIV>
<DIV> </DIV>
<DIV> =
new=20
Label(shell, SWT.NONE).setText("People");</DIV>
<DIV> </DIV>
<DIV> =20
Composite peopleComposite =3D new Composite(shell, SWT.NONE);</DIV>
<DIV> =20
GridDataFactory.fillDefaults().grab(true, true).span(2, =
1).applyTo(</DIV>
<DIV> &n=
bsp; =20
peopleComposite);</DIV>
<DIV> =20
TableColumnLayout peopleColumnLayout =3D new TableColumnLayout();</DIV>
<DIV> =20
peopleComposite.setLayout(peopleColumnLayout);</DIV>
<DIV> </DIV>
<DIV> =20
peopleViewer =3D new TableViewer(peopleComposite, SWT.SINGLE</DIV>
<DIV> &n=
bsp; =20
| SWT.BORDER | SWT.FULL_SELECTION);</DIV>
<DIV> </DIV>
<DIV> =
Table=20
peopleTable =3D peopleViewer.getTable();</DIV>
<DIV> =20
peopleTable.setHeaderVisible(true);</DIV>
<DIV> =20
peopleTable.setLinesVisible(true);</DIV>
<DIV> </DIV>
<DIV> =20
TableColumn nameColumn =3D new TableColumn(peopleTable, SWT.NONE);</DIV>
<DIV> =20
nameColumn.setText("Name");</DIV>
<DIV> =20
peopleColumnLayout.setColumnData(nameColumn,</DIV>
<DIV> &n=
bsp; =20
new ColumnWeightData(1));</DIV>
<DIV> </DIV>
<DIV> =20
TableColumn friendsColumn =3D new TableColumn(peopleTable, =
SWT.NONE);</DIV>
<DIV> =20
friendsColumn.setText("Friends");</DIV>
<DIV> =20
peopleColumnLayout.setColumnData(friendsColumn,</DIV>
<DIV> &n=
bsp; =20
new ColumnWeightData(3));</DIV>
<DIV> </DIV>
<DIV> =
new=20
Label(shell, SWT.NONE).setText("Name");</DIV>
<DIV> </DIV>
<DIV> =20
personName =3D new Text(shell, SWT.BORDER);</DIV>
<DIV> =20
GridDataFactory.fillDefaults().grab(true, false)</DIV>
<DIV> &n=
bsp; =20
..applyTo(personName);</DIV>
<DIV> </DIV>
<DIV> =
new=20
Label(shell, SWT.NONE).setText("Friends");</DIV>
<DIV> </DIV>
<DIV> =20
Composite friendsComposite =3D new Composite(shell,SWT.NONE);</DIV>
<DIV> =20
friendsComposite.setLayoutData(new GridData(GridData.FILL_BOTH));</DIV>
<DIV> =20
friendsComposite.setLayout(new GridLayout(1,=20
false)); </DIV>
<DIV> </DIV>
<DIV> =20
friendsTreeViewer =3D new CheckboxTreeViewer(friendsComposite);</DIV>
<DIV> </DIV>
<DIV> =
Tree=20
friendsTree =3D friendsTreeViewer.getTree();</DIV>
<DIV> =20
friendsTree.setHeaderVisible(true);</DIV>
<DIV> =20
friendsTree.setLinesVisible(true);</DIV>
<DIV> =20
TreeColumn friendNameColumn =3D new TreeColumn(friendsTree,</DIV>
<DIV> &n=
bsp; =20
SWT.NONE);</DIV>
<DIV> =20
friendNameColumn.setText("Name");</DIV>
<DIV> =20
friendNameColumn.setWidth(100);</DIV>
<DIV> }</DIV>
<DIV> </DIV>
<DIV> private void bindUI() =
{</DIV>
<DIV> =20
DataBindingContext dbc =3D new DataBindingContext();</DIV>
<DIV> </DIV>
<DIV> =
final=20
IObservableList people =3D BeansObservables.observeList(Realm</DIV>
<DIV> &n=
bsp; =20
..getDefault(), viewModel, "people");</DIV>
<DIV> =
</DIV>
<DIV> =20
ViewerSupport.bind(peopleViewer, people, BeanProperties.values(</DIV>
<DIV> &n=
bsp; =20
Person.class, new String[] { "name", "friends" }));</DIV>
<DIV> </DIV>
<DIV> =
final=20
IObservableValue selectedPerson =3D ViewersObservables</DIV>
<DIV> &n=
bsp; =20
..observeSingleSelection(peopleViewer);</DIV>
<DIV> </DIV>
<DIV> =20
dbc.bindValue(SWTObservables.observeText(personName, SWT.Modify),</DIV>
<DIV> &n=
bsp; =20
BeansObservables.observeDetailValue(selectedPerson, "name",</DIV>
<DIV> &n=
bsp; &nb=
sp; =20
String.class));</DIV>
<DIV> </DIV>
<DIV> =
/*</DIV>
<DIV> &n=
bsp; *=20
original binding for table.</DIV>
<DIV> &n=
bsp;=20
*/</DIV>
<DIV>// =
=20
ViewerSupport.bind(friendsTreeViewer, people, =
BeanProperties.value(</DIV>
<DIV>// =
=20
Person.class, "name"));</DIV>
<DIV> </DIV>
<DIV> =
/*</DIV>
<DIV> &n=
bsp; *=20
this runs, but not what I want.</DIV>
<DIV> &n=
bsp;=20
*/</DIV>
<DIV> =20
ViewerSupport.bind(friendsTreeViewer, viewModelPplTree, </DIV>
<DIV> &n=
bsp; =20
BeanProperties.set("friends",Person.class), </DIV>
<DIV> &n=
bsp; =20
BeanProperties.value(Person.class,=20
"name")); &nbs=
p;=20
</DIV>
<DIV> </DIV>
<DIV> =
/*</DIV>
<DIV> &n=
bsp; *=20
this doesn't run, but its what I want.</DIV>
<DIV> &n=
bsp;=20
*/</DIV>
<DIV>// =
=20
ViewerSupport.bind(friendsTreeViewer, </DIV>
<DIV>// =
=20
BeansObservables.observeDetailSet(selectedPerson,"friends",Person.class),=
</DIV>
<DIV>// =
=20
BeanProperties.value(Person.class, "name"));</DIV>
<DIV> =
</DIV>
<DIV> =
//no=20
change</DIV>
<DIV> =20
dbc.bindSet(ViewersObservables.observeCheckedElements(</DIV>
<DIV> &n=
bsp; =20
friendsTreeViewer, Person.class), BeansObservables</DIV>
<DIV> &n=
bsp; =20
..observeDetailSet(selectedPerson, "friends", Person.class));</DIV>
<DIV> =
</DIV>
<DIV> }</DIV>
<DIV> }</DIV>
<DIV>}</DIV>
<DIV> </DIV>
<DIV> </DIV></DIV></DIV></BODY></HTML>
------=_NextPart_000_000B_01CBCD5B.8A4692B0--
|
|
|
Re: Bind selected Observable to TreeViewer [message #655037 is a reply to message #654384] |
Fri, 18 February 2011 01:23  |
Eclipse User |
|
|
|
On 02/15/2011 06:58 AM, Quincy Mitchell wrote:
> /*
> * this runs, but NOT what I want.
> */
> ViewerSupport.bind(friendsTreeViewer, viewModelPplTree,
> BeanProperties.set("friends",Person.class),
> BeanProperties.value(Person.class, "name"));
> /*
> * this doesn't run, but it IS what I want.
> */
> // ViewerSupport.bind(friendsTreeViewer,
> // BeansObservables.observeDetailSet(selectedPerson,"friends",Person.class),
> // BeanProperties.value(Person.class, "name"));
> [/code]
If I understand what you're trying to do, I think you want:
ViewerSupport.bind(friendsTreeViewer, selectedPerson,
BeanProperties.set("friends", Person.class),
BeanProperties.value(Person.class, "name"));
Where selectedPerson is an IObservableValue that observes the selection
of the left-hand table.
Is this what you're looking for?
Matthew
|
|
|
Goto Forum:
Current Time: Wed Jul 23 11:42:54 EDT 2025
Powered by FUDForum. Page generated in 0.03606 seconds
|