Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » CheckboxTreeViewer , ViewersObservables.observeCheckedElements( )
CheckboxTreeViewer , ViewersObservables.observeCheckedElements( ) [message #654342] Tue, 15 February 2011 09:35 Go to next message
Eclipse UserFriend
Originally posted by: quincy.cs.gmail.com

Firstly, there are no snippets of using databinding with the checked
elements of a tree viewer. I followed closely as possible with the table
viewer snippet. My issue is databinding is not completely working, but
almost. The only thing that is left is having the checks appear with the
initial data. When they are checked they change bound model correctly.

I have a CheckboxTreeViewer which manages 1 type of model, CatalogItem.

I have another model, CatalogSelection which is meant to be bound upon to
viewer’s checked CatalogItems.
This is where the issue is, the viewer doesn’t have anything checked even
though its bound to models. CatalogSelection has all the same models as the
viewer, except for that root model of the viewer which is ignored from
presentation.

However, when I check/uncheck the table then the models are added/removed
from the bound CatalogSelection.

The following code is how I bind, followed by the model definitions.


......
private CatalogSelection    value;

//root of tree
private CatalogItem invisibleRoot ;
......
        ViewerSupport.bind(treeViewer, invisibleRoot, 
BeanProperties.list("list",
                CatalogItem.class), BeanProperties.value(CatalogItem.class, 
"name"));

        DataBindingContext dbc = new DataBindingContext();

        CatalogSelectionWrapper awkwardValue = new 
CatalogSelectionWrapper(value);
        IObservableValue valueObs = 
BeansObservables.observeValue(awkwardValue, "selection");
        dbc.bindSet(ViewersObservables.observeCheckedElements(
                treeViewer, CatalogItem.class), BeansObservables
                .observeDetailSet(valueObs, "catalogs", CatalogItem.class));
........

static class CatalogSelectionWrapper extends AbstractModel{


    private CatalogSelection    value;

    public CatalogSelectionWrapper(CatalogSelection value) {
        this.value=value;
    }

    public CatalogSelection getSelection(){
        return value;
    }

    @Override
    public Object getKey() {
        return null;
    }

}

static class CatalogSelection extends AbstractModel {

        private Set<CatalogItem>    cats;

        public Set<CatalogItem> getCatalogs(){
            return cats;
        }

        public void setCatalogs(Set<CatalogItem> cats){
            firePropertyChange("catalogs", this.cats,
                    this.cats = cats);
        }

        @Override
        public Object getKey() {
            return null;
        }
}


static class CatalogItem extends AbstractModel {


    private String    key;
    private String    name;


    /**
     * create portfolio catalog item
     */
    public CatalogItem(String key, String name) {
        this.key=key;
        this.name=name;
    }

    /**
     * create fund manager catalog item
     */
    public CatalogItem(String name) {
        this.name=name;
    }

    public Boolean isPortfolioItem(){
        return key!=null;
    }

    public String getName(){
        return name;
    }

    private List list;


    public List getList() {
        if (list == null)
            return null;
        return new ArrayList(list);
    }

    public void setList(List list) {
        if (list != null)
            list = new ArrayList(list);
        firePropertyChange("list", this.list,
                this.list = list);
    }

    @Override
    public Object getKey() {
        return key;
    }


}




Please reply if you need a completely isolated snippet to help.

Thanks,
Quincy Mitchell
Re: CheckboxTreeViewer , ViewersObservables.observeCheckedElements( ) [message #654606 is a reply to message #654342] Wed, 16 February 2011 13:31 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: quincy.cs.gmail.com

This is a multi-part message in MIME format.

------=_NextPart_000_000F_01CBCE20.DC4B9F90
Content-Type: text/plain;
format=flowed;
charset="utf-8";
reply-type=response
Content-Transfer-Encoding: 7bit

Here is the completely isolated snippet.

Pay attention to line 88,91.

Line 88 sets the available people hierarchy to view. Line 91, sets the
checked people.

Notice that 'stan' is friends of everyone. I set the selection to be
'kyle','eric' however they don't come up checked.

It appears that the way I bind does not support going down the hierarchy to
find the models.

If you change line 88, 91 in different combinations you can see that it
binds except only to the roots of the hierarchy.

How do I set the binding to cascade down?

This took a lot of time to isolate. I hope it is crystal clear what I want.

Thanks for your advice,
Quincy Mitchell

------=_NextPart_000_000F_01CBCE20.DC4B9F90
Content-Type: application/octet-stream;
name="SnippetCheckboxTreeViewer.java"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
filename="SnippetCheckboxTreeViewer.java"

=0A=
import java.beans.PropertyChangeListener;=0A=
import java.beans.PropertyChangeSupport;=0A=
import java.util.ArrayList;=0A=
import java.util.Arrays;=0A=
import java.util.HashSet;=0A=
import java.util.List;=0A=
import java.util.Set;=0A=
=0A=
import org.eclipse.core.databinding.DataBindingContext;=0A=
import org.eclipse.core.databinding.beans.BeanProperties;=0A=
import org.eclipse.core.databinding.beans.BeansObservables;=0A=
import org.eclipse.core.databinding.observable.Realm;=0A=
import org.eclipse.core.databinding.observable.list.IObservableList ;=0A=
import org.eclipse.core.databinding.observable.value.ComputedValue; =0A=
import org.eclipse.core.databinding.observable.value.IObservableVal ue;=0A=
import org.eclipse.jface.databinding.swt.SWTObservables;=0A=
import org.eclipse.jface.databinding.viewers.ViewerSupport;=0A=
import org.eclipse.jface.databinding.viewers.ViewersObservables;=0A =
import org.eclipse.jface.dialogs.IInputValidator;=0A=
import org.eclipse.jface.dialogs.InputDialog;=0A=
import org.eclipse.jface.dialogs.MessageDialog;=0A=
import org.eclipse.jface.layout.GridDataFactory;=0A=
import org.eclipse.jface.layout.GridLayoutFactory;=0A=
import org.eclipse.jface.layout.TableColumnLayout;=0A=
import org.eclipse.jface.viewers.CheckboxTableViewer;=0A=
import org.eclipse.jface.viewers.CheckboxTreeViewer;=0A=
import org.eclipse.jface.viewers.ColumnWeightData;=0A=
import org.eclipse.jface.viewers.IStructuredSelection;=0A=
import org.eclipse.jface.viewers.StructuredSelection;=0A=
import org.eclipse.jface.viewers.TableViewer;=0A=
import org.eclipse.jface.window.Window;=0A=
import org.eclipse.swt.SWT;=0A=
import org.eclipse.swt.layout.GridData;=0A=
import org.eclipse.swt.layout.GridLayout;=0A=
import org.eclipse.swt.widgets.Button;=0A=
import org.eclipse.swt.widgets.Composite;=0A=
import org.eclipse.swt.widgets.Display;=0A=
import org.eclipse.swt.widgets.Event;=0A=
import org.eclipse.swt.widgets.Label;=0A=
import org.eclipse.swt.widgets.Listener;=0A=
import org.eclipse.swt.widgets.Shell;=0A=
import org.eclipse.swt.widgets.Table;=0A=
import org.eclipse.swt.widgets.TableColumn;=0A=
import org.eclipse.swt.widgets.Text;=0A=
import org.eclipse.swt.widgets.Tree;=0A=
import org.eclipse.swt.widgets.TreeColumn;=0A=
=0A=
=0A=
public class SnippetCheckboxTreeViewer {=0A=
=0A=
public static void main(String[] args) {=0A=
// The SWT event loop=0A=
final Display display =3D Display.getDefault();=0A=
Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {=0A=
public void run() {=0A=
ViewModel viewModel =3D new ViewModel();=0A=
ViewModelSelection viewModelSelection =3D new ViewModelSelection();=0A=
createSampleModel(viewModel, viewModelSelection);=0A=
=0A=
Shell shell =3D new View(viewModel,viewModelSelection).createShell();=0A=
shell.open();=0A=
while (!shell.isDisposed())=0A=
if (!display.readAndDispatch())=0A=
display.sleep();=0A=
}=0A=
});=0A=
display.dispose();=0A=
}=0A=
=0A=
private static void createSampleModel(ViewModel viewModel, =
ViewModelSelection viewModelSelection) {=0A=
=0A=
Person stan =3D createPerson("Stan");=0A=
Person kyle =3D createPerson("Kyle");=0A=
Person eric =3D createPerson("Eric");=0A=
Person kenny =3D createPerson("Kenny");=0A=
Person wendy =3D createPerson("Wendy");=0A=
Person butters =3D createPerson("Butters");=0A=
=0A=
setFriends(stan, new Person[] { kyle, eric, kenny, wendy });=0A=
setFriends(kyle, new Person[] { stan, eric, kenny });=0A=
setFriends(eric, new Person[] { eric });=0A=
setFriends(kenny, new Person[] { stan, kyle, eric });=0A=
setFriends(wendy, new Person[] { stan });=0A=
setFriends(butters, new Person[0]);=0A=
=0A=
// Person[] people =3D new Person[] { stan, kyle, eric, kenny, wendy, =
butters };=0A=
Person[] people =3D new Person[] { stan };=0A=
viewModel.setFriends(new HashSet(Arrays.asList(people)));=0A=
=0A=
Person[] selection =3D new Person[] { kyle, eric };=0A=
viewModelSelection.setName("the_selection");=0A=
viewModelSelection.setFriends(new HashSet(Arrays.asList(selection)));=0A=
}=0A=
=0A=
private static Person createPerson(String name) {=0A=
Person person =3D new Person();=0A=
person.setName(name);=0A=
return person;=0A=
}=0A=
=0A=
private static void setFriends(Person person, Person[] friends) {=0A=
person.setFriends(new HashSet(Arrays.asList(friends)));=0A=
}=0A=
=0A=
// The GUI view=0A=
static class View {=0A=
=0A=
private Text personName;=0A=
private CheckboxTreeViewer friendsTreeViewer;=0A=
=0A=
private ViewModel viewModel;=0A=
private ViewModelSelection viewModelSelection;=0A=
=0A=
public View(ViewModel viewModel, ViewModelSelection =
viewModelSelection) {=0A=
this.viewModel =3D viewModel;=0A=
this.viewModelSelection =3D viewModelSelection;=0A=
}=0A=
=0A=
public Shell createShell() {=0A=
// Build a UI=0A=
final Display display =3D Display.getCurrent();=0A=
Shell shell =3D new Shell(display);=0A=
=0A=
createUI(shell);=0A=
=0A=
// Bind UI=0A=
bindUI();=0A=
=0A=
// Open and return the Shell=0A=
shell.setSize(shell.computeSize(400, SWT.DEFAULT));=0A=
shell.open();=0A=
return shell;=0A=
}=0A=
=0A=
private void createUI(Shell shell) {=0A=
shell.setText("Binding checked elements in CheckboxTableViewer");=0A=
shell.setLayout(new GridLayout(2, false));=0A=
=0A=
new Label(shell, SWT.NONE).setText("Name");=0A=
=0A=
personName =3D new Text(shell, SWT.BORDER);=0A=
GridDataFactory.fillDefaults().grab(true, false)=0A=
.applyTo(personName);=0A=
=0A=
new Label(shell, SWT.NONE).setText("Friends");=0A=
=0A=
Composite friendsComposite =3D new Composite(shell,SWT.NONE);=0A=
friendsComposite.setLayoutData(new GridData(GridData.FILL_BOTH));=0A=
friendsComposite.setLayout(new GridLayout(1, false)); =0A=
=0A=
friendsTreeViewer =3D new CheckboxTreeViewer(friendsComposite);=0A=
=0A=
Tree friendsTree =3D friendsTreeViewer.getTree();=0A=
GridData gd_tree =3D new GridData(SWT.LEFT, SWT.CENTER, false, false, =
1, 1);=0A=
gd_tree.heightHint =3D 176;=0A=
friendsTree.setLayoutData(gd_tree);=0A=
friendsTree.setHeaderVisible(true);=0A=
friendsTree.setLinesVisible(true);=0A=
TreeColumn friendNameColumn =3D new TreeColumn(friendsTree,=0A=
SWT.NONE);=0A=
friendNameColumn.setText("Name");=0A=
friendNameColumn.setWidth(100);=0A=
}=0A=
=0A=
private void bindUI() {=0A=
=0A=
ViewerSupport.bind(friendsTreeViewer, viewModel, =0A=
BeanProperties.set("friends",Person.class), =0A=
BeanProperties.value(Person.class, "name"));=0A=
=0A=
DataBindingContext dbc =3D new DataBindingContext();=0A=
=0A=
dbc.bindSet(ViewersObservables.observeCheckedElements(=0A=
friendsTreeViewer, Person.class), =0A=
BeansObservables.observeSet(viewModelSelection, "friends"));=0A=
=0A=
=0A=
dbc.bindValue(SWTObservables.observeText(personName, SWT.Modify),=0A=
BeansObservables.observeValue(viewModelSelection, "name")); =0A=
}=0A=
}=0A=
=0A=
// The data model class.=0A=
static class Person extends AbstractModelObject {=0A=
private String name;=0A=
private Set friends =3D new HashSet();=0A=
private Set<Person> children;=0A=
=0A=
public String getName() {=0A=
return name;=0A=
}=0A=
=0A=
public void setName(String name) {=0A=
firePropertyChange("name", this.name, this.name =3D name);=0A=
}=0A=
=0A=
public Set getFriends() {=0A=
return new HashSet(friends);=0A=
}=0A=
=0A=
public void setFriends(Set friends) {=0A=
firePropertyChange("friends", this.friends,=0A=
this.friends =3D new HashSet(friends));=0A=
}=0A=
=0A=
=0A=
}=0A=
=0A=
static class ViewModel extends Person {=0A=
}=0A=
static class ViewModelSelection extends Person{=0A=
}=0A=
=0A=
// Minimal JavaBeans support=0A=
public static abstract class AbstractModelObject {=0A=
private PropertyChangeSupport propertyChangeSupport =3D new =
PropertyChangeSupport(=0A=
this);=0A=
=0A=
public void addPropertyChangeListener(PropertyChangeListener listener) =
{=0A=
propertyChangeSupport.addPropertyChangeListener(listener);=0 A=
}=0A=
=0A=
public void addPropertyChangeListener(String propertyName,=0A=
PropertyChangeListener listener) {=0A=
propertyChangeSupport.addPropertyChangeListener(propertyName ,=0A=
listener);=0A=
}=0A=
=0A=
public void removePropertyChangeListener(PropertyChangeListener =
listener) {=0A=
propertyChangeSupport.removePropertyChangeListener(listener) ;=0A=
}=0A=
=0A=
public void removePropertyChangeListener(String propertyName,=0A=
PropertyChangeListener listener) {=0A=
propertyChangeSupport.removePropertyChangeListener(propertyN ame,=0A=
listener);=0A=
}=0A=
=0A=
protected void firePropertyChange(String propertyName, Object oldValue,=0A=
Object newValue) {=0A=
propertyChangeSupport.firePropertyChange(propertyName, oldValue,=0A=
newValue);=0A=
}=0A=
}=0A=
=0A=
=0A=
=0A=
=0A=
}=0A=

------=_NextPart_000_000F_01CBCE20.DC4B9F90--
Re: CheckboxTreeViewer , ViewersObservables.observeCheckedElements( ) [message #654629 is a reply to message #654606] Wed, 16 February 2011 13:40 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: quincy.cs.gmail.com

Just incase your newsreader can't get the code here it is in plain text.


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 SnippetCheckboxTreeViewer {

    public static void main(String[] args) {
        // The SWT event loop
        final Display display = Display.getDefault();
        Realm.runWithDefault(SWTObservables.getRealm(display), new 
Runnable() {
            public void run() {
                ViewModel viewModel = new ViewModel();
                ViewModelSelection viewModelSelection = new 
ViewModelSelection();
                createSampleModel(viewModel, viewModelSelection);

                Shell shell = new 
View(viewModel,viewModelSelection).createShell();
                shell.open();
                while (!shell.isDisposed())
                    if (!display.readAndDispatch())
                        display.sleep();
            }
        });
        display.dispose();
    }

    private static void createSampleModel(ViewModel viewModel, 
ViewModelSelection viewModelSelection) {

        Person stan = createPerson("Stan");
        Person kyle = createPerson("Kyle");
        Person eric = createPerson("Eric");
        Person kenny = createPerson("Kenny");
        Person wendy = createPerson("Wendy");
        Person butters = 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 = new Person[] { stan, kyle, eric, kenny, wendy, 
butters };
        Person[] people = new Person[] { stan };
        viewModel.setFriends(new HashSet(Arrays.asList(people)));

        Person[] selection = new Person[] { kyle, eric };
        viewModelSelection.setName("the_selection");
        viewModelSelection.setFriends(new 
HashSet(Arrays.asList(selection)));
    }

    private static Person createPerson(String name) {
        Person person = new Person();
        person.setName(name);
        return person;
    }

    private static void setFriends(Person person, Person[] friends) {
        person.setFriends(new HashSet(Arrays.asList(friends)));
    }

    // The GUI view
    static class View {

        private Text personName;
        private CheckboxTreeViewer friendsTreeViewer;

        private ViewModel viewModel;
        private ViewModelSelection    viewModelSelection;

        public View(ViewModel viewModel, ViewModelSelection 
viewModelSelection) {
            this.viewModel = viewModel;
            this.viewModelSelection = viewModelSelection;
        }

        public Shell createShell() {
            // Build a UI
            final Display display = Display.getCurrent();
            Shell shell = 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("Name");

            personName = new Text(shell, SWT.BORDER);
            GridDataFactory.fillDefaults().grab(true, false)
                    .applyTo(personName);

            new Label(shell, SWT.NONE).setText("Friends");

            Composite friendsComposite = new Composite(shell,SWT.NONE);
            friendsComposite.setLayoutData(new 
GridData(GridData.FILL_BOTH));
            friendsComposite.setLayout(new GridLayout(1, false));

            friendsTreeViewer = new CheckboxTreeViewer(friendsComposite);

            Tree friendsTree = friendsTreeViewer.getTree();
            GridData gd_tree = new GridData(SWT.LEFT, SWT.CENTER, false, 
false, 1, 1);
            gd_tree.heightHint = 176;
            friendsTree.setLayoutData(gd_tree);
            friendsTree.setHeaderVisible(true);
            friendsTree.setLinesVisible(true);
            TreeColumn friendNameColumn = new TreeColumn(friendsTree,
                    SWT.NONE);
            friendNameColumn.setText("Name");
            friendNameColumn.setWidth(100);
        }

        private void bindUI() {

            ViewerSupport.bind(friendsTreeViewer, viewModel,
                    BeanProperties.set("friends",Person.class),
                    BeanProperties.value(Person.class, "name"));

            DataBindingContext dbc = new DataBindingContext();

            dbc.bindSet(ViewersObservables.observeCheckedElements(
                    friendsTreeViewer, Person.class),
                    BeansObservables.observeSet(viewModelSelection, 
"friends"));


            dbc.bindValue(SWTObservables.observeText(personName, 
SWT.Modify),
                    BeansObservables.observeValue(viewModelSelection, 
"name"));
        }
    }

    // The data model class.
    static class Person extends AbstractModelObject {
        private String name;
        private Set friends = new HashSet();
        private Set<Person>    children;

        public String getName() {
            return name;
        }

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

        public Set getFriends() {
            return new HashSet(friends);
        }

        public void setFriends(Set friends) {
            firePropertyChange("friends", this.friends,
                    this.friends = new HashSet(friends));
        }


    }

    static class ViewModel extends Person {
    }
    static class ViewModelSelection extends Person{
    }

    // Minimal JavaBeans support
    public static abstract class AbstractModelObject {
        private PropertyChangeSupport propertyChangeSupport = 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);
        }
    }




}
Re: CheckboxTreeViewer , ViewersObservables.observeCheckedElements( ) [message #669985 is a reply to message #654629] Thu, 12 May 2011 10:16 Go to previous message
Kevin Missing name is currently offline Kevin Missing nameFriend
Messages: 10
Registered: October 2009
Junior Member
Hi,

i stumbled over your snipped while having almost the same use-case. When
i try to run it, i got the

"Exception in thread "main" java.lang.NoClassDefFoundError:
com/ibm/icu/text/NumberFormat"
Caused by Caused by: java.lang.ClassNotFoundException:
com.ibm.icu.text.NumberFormat"

Kevin


Am 16.02.2011 14:40, schrieb Quincy Mitchell:
> Just incase your newsreader can't get the code here it is in plain text.
>
>
>
> 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 SnippetCheckboxTreeViewer {
>
> public static void main(String[] args) {
> // The SWT event loop
> final Display display = Display.getDefault();
> Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
> public void run() {
> ViewModel viewModel = new ViewModel();
> ViewModelSelection viewModelSelection = new ViewModelSelection();
> createSampleModel(viewModel, viewModelSelection);
>
> Shell shell = new View(viewModel,viewModelSelection).createShell();
> shell.open();
> while (!shell.isDisposed())
> if (!display.readAndDispatch())
> display.sleep();
> }
> });
> display.dispose();
> }
>
> private static void createSampleModel(ViewModel viewModel,
> ViewModelSelection viewModelSelection) {
>
> Person stan = createPerson("Stan");
> Person kyle = createPerson("Kyle");
> Person eric = createPerson("Eric");
> Person kenny = createPerson("Kenny");
> Person wendy = createPerson("Wendy");
> Person butters = 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 = new Person[] { stan, kyle, eric, kenny, wendy,
> butters };
> Person[] people = new Person[] { stan };
> viewModel.setFriends(new HashSet(Arrays.asList(people)));
>
> Person[] selection = new Person[] { kyle, eric };
> viewModelSelection.setName("the_selection");
> viewModelSelection.setFriends(new HashSet(Arrays.asList(selection)));
> }
>
> private static Person createPerson(String name) {
> Person person = new Person();
> person.setName(name);
> return person;
> }
>
> private static void setFriends(Person person, Person[] friends) {
> person.setFriends(new HashSet(Arrays.asList(friends)));
> }
>
> // The GUI view
> static class View {
>
> private Text personName;
> private CheckboxTreeViewer friendsTreeViewer;
>
> private ViewModel viewModel;
> private ViewModelSelection viewModelSelection;
>
> public View(ViewModel viewModel, ViewModelSelection viewModelSelection) {
> this.viewModel = viewModel;
> this.viewModelSelection = viewModelSelection;
> }
>
> public Shell createShell() {
> // Build a UI
> final Display display = Display.getCurrent();
> Shell shell = 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("Name");
>
> personName = new Text(shell, SWT.BORDER);
> GridDataFactory.fillDefaults().grab(true, false)
> .applyTo(personName);
>
> new Label(shell, SWT.NONE).setText("Friends");
>
> Composite friendsComposite = new Composite(shell,SWT.NONE);
> friendsComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
> friendsComposite.setLayout(new GridLayout(1, false));
>
> friendsTreeViewer = new CheckboxTreeViewer(friendsComposite);
>
> Tree friendsTree = friendsTreeViewer.getTree();
> GridData gd_tree = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
> gd_tree.heightHint = 176;
> friendsTree.setLayoutData(gd_tree);
> friendsTree.setHeaderVisible(true);
> friendsTree.setLinesVisible(true);
> TreeColumn friendNameColumn = new TreeColumn(friendsTree,
> SWT.NONE);
> friendNameColumn.setText("Name");
> friendNameColumn.setWidth(100);
> }
>
> private void bindUI() {
>
> ViewerSupport.bind(friendsTreeViewer, viewModel,
> BeanProperties.set("friends",Person.class),
> BeanProperties.value(Person.class, "name"));
>
> DataBindingContext dbc = new DataBindingContext();
>
> dbc.bindSet(ViewersObservables.observeCheckedElements(
> friendsTreeViewer, Person.class),
> BeansObservables.observeSet(viewModelSelection, "friends"));
>
>
> dbc.bindValue(SWTObservables.observeText(personName, SWT.Modify),
> BeansObservables.observeValue(viewModelSelection, "name"));
> }
> }
>
> // The data model class.
> static class Person extends AbstractModelObject {
> private String name;
> private Set friends = new HashSet();
> private Set<Person> children;
>
> public String getName() {
> return name;
> }
>
> public void setName(String name) {
> firePropertyChange("name", this.name, this.name = name);
> }
>
> public Set getFriends() {
> return new HashSet(friends);
> }
>
> public void setFriends(Set friends) {
> firePropertyChange("friends", this.friends,
> this.friends = new HashSet(friends));
> }
>
>
> }
>
> static class ViewModel extends Person {
> }
> static class ViewModelSelection extends Person{
> }
>
> // Minimal JavaBeans support
> public static abstract class AbstractModelObject {
> private PropertyChangeSupport propertyChangeSupport = 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);
> }
> }
>
>
>
>
> }
> 
Previous Topic:How do I scroll to an ExpandItem in an ExpandBar?
Next Topic:Drag and Drop with Button
Goto Forum:
  


Current Time: Thu Nov 27 22:11:03 GMT 2014

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

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