Mercurial > projects > dwt2
diff org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet025TableViewerWithPropertyDerivedColumns.d @ 99:5d5bd660917f
build some databind snippets
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Wed, 22 Apr 2009 18:59:26 +0200 |
parents | 6086085e153d |
children | e884642ad36e |
line wrap: on
line diff
--- a/org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet025TableViewerWithPropertyDerivedColumns.d Wed Apr 22 07:30:21 2009 +0200 +++ b/org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet025TableViewerWithPropertyDerivedColumns.d Wed Apr 22 18:59:26 2009 +0200 @@ -10,7 +10,9 @@ * Matthew Hall - bug 195222, 261843, 260337 ******************************************************************************/ -package org.eclipse.jface.examples.databinding.snippets; +module org.eclipse.jface.examples.databinding.snippets.Snippet025TableViewerWithPropertyDerivedColumns; + +import java.lang.all; import java.beans.PropertyChangeListener; import java.beans.PropertyChangeSupport; @@ -44,229 +46,232 @@ * Demonstrates binding a TableViewer to a collection. */ public class Snippet025TableViewerWithPropertyDerivedColumns { - public static void main(String[] args) { - final Display display = new Display(); + public static void main(String[] args) { + final Display display = new Display(); - // Set up data binding. In an RCP application, the threading Realm - // will be set for you automatically by the Workbench. In an SWT - // application, you can do this once, wrapping your binding - // method call. - Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() { - public void run() { - ViewModel viewModel = new ViewModel(); - Shell shell = new View(viewModel).createShell(); + // Set up data binding. In an RCP application, the threading Realm + // will be set for you automatically by the Workbench. In an SWT + // application, you can do this once, wrapping your binding + // method call. + Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() { + public void run() { + ViewModel viewModel = new ViewModel(); + Shell shell = new View(viewModel).createShell(); - // The SWT event loop - while (!shell.isDisposed()) { - if (!display.readAndDispatch()) { - display.sleep(); - } - } - } - }); - } + // The SWT event loop + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) { + display.sleep(); + } + } + } + }); + } - // Minimal JavaBeans support - public static abstract class AbstractModelObject { - private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport( - this); + // 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(PropertyChangeListener listener) { + propertyChangeSupport.addPropertyChangeListener(listener); + } - public void addPropertyChangeListener(String propertyName, - PropertyChangeListener listener) { - propertyChangeSupport.addPropertyChangeListener(propertyName, - listener); - } + public void addPropertyChangeListener(String propertyName, + PropertyChangeListener listener) { + propertyChangeSupport.addPropertyChangeListener(propertyName, + listener); + } - public void removePropertyChangeListener(PropertyChangeListener listener) { - propertyChangeSupport.removePropertyChangeListener(listener); - } + public void removePropertyChangeListener(PropertyChangeListener listener) { + propertyChangeSupport.removePropertyChangeListener(listener); + } - public void removePropertyChangeListener(String propertyName, - PropertyChangeListener listener) { - propertyChangeSupport.removePropertyChangeListener(propertyName, - 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); - } - } + protected void firePropertyChange(String propertyName, Object oldValue, + Object newValue) { + propertyChangeSupport.firePropertyChange(propertyName, oldValue, + newValue); + } + } - private static Person UNKNOWN = new Person("unknown", null, null); + private static Person UNKNOWN = new Person("unknown", null, null); - // The data model class. This is normally a persistent class of some sort. - static class Person extends AbstractModelObject { - // A property... - String name = "Donald Duck"; - Person mother; - Person father; + // The data model class. This is normally a persistent class of some sort. + static class Person extends AbstractModelObject { + // A property... + String name = "Donald Duck"; + Person mother; + Person father; - public Person(String name, Person mother, Person father) { - this.name = name; - this.mother = mother; - this.father = father; - } + public Person(String name, Person mother, Person father) { + this.name = name; + this.mother = mother; + this.father = father; + } - public String getName() { - return name; - } + public String getName() { + return name; + } - public void setName(String name) { - firePropertyChange("name", this.name, this.name = name); - } + public void setName(String name) { + firePropertyChange("name", this.name, this.name = name); + } - public Person getMother() { - return mother; - } + public Person getMother() { + return mother; + } - public void setMother(Person mother) { - firePropertyChange("mother", this.mother, this.mother = mother); - } + public void setMother(Person mother) { + firePropertyChange("mother", this.mother, this.mother = mother); + } - public Person getFather() { - return father; - } + public Person getFather() { + return father; + } - public void setFather(Person father) { - firePropertyChange("father", this.father, this.father = father); - } - } + public void setFather(Person father) { + firePropertyChange("father", this.father, this.father = father); + } + } - // The View's model--the root of our Model graph for this particular GUI. - // - // Typically each View class has a corresponding ViewModel class. - // The ViewModel is responsible for getting the objects to edit from the - // data access tier. Since this snippet doesn't have any persistent objects - // ro retrieve, this ViewModel just instantiates a model object to edit. - static class ViewModel { - // The model to bind - private IObservableList people = new WritableList(); - { - Person fergus = new Person("Fergus McDuck", UNKNOWN, UNKNOWN); - Person downy = new Person("Downy O'Drake", UNKNOWN, UNKNOWN); - Person scrooge = new Person("Scrooge McDuck", downy, fergus); - Person hortense = new Person("Hortense McDuck", downy, fergus); - Person quackmore = new Person("Quackmore Duck", UNKNOWN, UNKNOWN); - Person della = new Person("Della Duck", hortense, quackmore); - Person donald = new Person("Donald Duck", hortense, quackmore); - people.add(UNKNOWN); - people.add(downy); - people.add(fergus); - people.add(scrooge); - people.add(quackmore); - people.add(hortense); - people.add(della); - people.add(donald); - } + // The View's model--the root of our Model graph for this particular GUI. + // + // Typically each View class has a corresponding ViewModel class. + // The ViewModel is responsible for getting the objects to edit from the + // data access tier. Since this snippet doesn't have any persistent objects + // ro retrieve, this ViewModel just instantiates a model object to edit. + static class ViewModel { + // The model to bind + private IObservableList people = new WritableList(); + { + Person fergus = new Person("Fergus McDuck", UNKNOWN, UNKNOWN); + Person downy = new Person("Downy O'Drake", UNKNOWN, UNKNOWN); + Person scrooge = new Person("Scrooge McDuck", downy, fergus); + Person hortense = new Person("Hortense McDuck", downy, fergus); + Person quackmore = new Person("Quackmore Duck", UNKNOWN, UNKNOWN); + Person della = new Person("Della Duck", hortense, quackmore); + Person donald = new Person("Donald Duck", hortense, quackmore); + people.add(UNKNOWN); + people.add(downy); + people.add(fergus); + people.add(scrooge); + people.add(quackmore); + people.add(hortense); + people.add(della); + people.add(donald); + } + + public IObservableList getPeople() { + return people; + } + } - public IObservableList getPeople() { - return people; - } - } + // The GUI view + static class View { + private ViewModel viewModel; + private Table duckFamily; + private Text nameText; + private Combo motherCombo; + private Combo fatherCombo; - // The GUI view - static class View { - private ViewModel viewModel; - private Table duckFamily; - private Text nameText; - private Combo motherCombo; - private Combo fatherCombo; - - public View(ViewModel viewModel) { - this.viewModel = viewModel; - } + public View(ViewModel viewModel) { + this.viewModel = viewModel; + } - public Shell createShell() { - // Build a UI - Display display = Display.getDefault(); - Shell shell = new Shell(display); - duckFamily = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION); - duckFamily.setHeaderVisible(true); - GridDataFactory.defaultsFor(duckFamily).span(2, 1).applyTo( - duckFamily); - createColumn("Name"); - createColumn("Mother"); - createColumn("Father"); - createColumn("Maternal Grandmother"); - createColumn("Maternal Grandfather"); - createColumn("Paternal Grandmother"); - createColumn("Paternal Grandfather"); + public Shell createShell() { + // Build a UI + Display display = Display.getDefault(); + Shell shell = new Shell(display); + duckFamily = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION); + duckFamily.setHeaderVisible(true); + GridDataFactory.defaultsFor(duckFamily).span(2, 1).applyTo( + duckFamily); + createColumn("Name"); + createColumn("Mother"); + createColumn("Father"); + createColumn("Maternal Grandmother"); + createColumn("Maternal Grandfather"); + createColumn("Paternal Grandmother"); + createColumn("Paternal Grandfather"); - duckFamily.setLinesVisible(true); + duckFamily.setLinesVisible(true); + + new Label(shell, SWT.NONE).setText("Name:"); + nameText = new Text(shell, SWT.BORDER); + GridDataFactory.defaultsFor(nameText).grab(true, false).applyTo( + nameText); - new Label(shell, SWT.NONE).setText("Name:"); - nameText = new Text(shell, SWT.BORDER); - GridDataFactory.defaultsFor(nameText).grab(true, false).applyTo( - nameText); + new Label(shell, SWT.NONE).setText("Mother:"); + motherCombo = new Combo(shell, SWT.READ_ONLY); + + new Label(shell, SWT.NONE).setText("Father:"); + fatherCombo = new Combo(shell, SWT.READ_ONLY); - new Label(shell, SWT.NONE).setText("Mother:"); - motherCombo = new Combo(shell, SWT.READ_ONLY); + DataBindingContext bindingContext = new DataBindingContext(); + bindGUI(bindingContext); - new Label(shell, SWT.NONE).setText("Father:"); - fatherCombo = new Combo(shell, SWT.READ_ONLY); - - DataBindingContext bindingContext = new DataBindingContext(); - bindGUI(bindingContext); + GridLayoutFactory.swtDefaults().numColumns(2).applyTo(shell); + // Open and return the Shell + shell.setSize(800, 300); + shell.open(); + return shell; + } - GridLayoutFactory.swtDefaults().numColumns(2).applyTo(shell); - // Open and return the Shell - shell.setSize(800, 300); - shell.open(); - return shell; - } + private void createColumn(String string) { + final TableColumn column = new TableColumn(duckFamily, SWT.NONE); + column.setText(string); + column.pack(); + if (column.getWidth() < 100) + column.setWidth(100); + } - private void createColumn(String string) { - final TableColumn column = new TableColumn(duckFamily, SWT.NONE); - column.setText(string); - column.pack(); - if (column.getWidth() < 100) - column.setWidth(100); - } + protected void bindGUI(DataBindingContext dbc) { + // Since we're using a JFace Viewer, we do first wrap our Table... + TableViewer peopleViewer = new TableViewer(duckFamily); + peopleViewer.addFilter(new ViewerFilter() { + public bool select(Viewer viewer, Object parentElement, + Object element) { + return element != UNKNOWN; + } + }); - protected void bindGUI(DataBindingContext dbc) { - // Since we're using a JFace Viewer, we do first wrap our Table... - TableViewer peopleViewer = new TableViewer(duckFamily); - peopleViewer.addFilter(new ViewerFilter() { - public boolean select(Viewer viewer, Object parentElement, - Object element) { - return element != UNKNOWN; - } - }); + ViewerSupport.bind(peopleViewer, viewModel.getPeople(), + BeanProperties.values(Person.class, new String[] { "name", + "mother.name", "father.name", "mother.mother.name", + "mother.father.name", "father.mother.name", + "father.father.name" })); - ViewerSupport.bind(peopleViewer, viewModel.getPeople(), - BeanProperties.values(Person.class, new String[] { "name", - "mother.name", "father.name", "mother.mother.name", - "mother.father.name", "father.mother.name", - "father.father.name" })); + IObservableValue masterSelection = ViewerProperties + .singleSelection().observe(peopleViewer); - IObservableValue masterSelection = ViewerProperties - .singleSelection().observe(peopleViewer); + dbc.bindValue(WidgetProperties.text(SWT.Modify).observe(nameText), + BeanProperties.value(Person.class, "name").observeDetail( + masterSelection)); + + ComboViewer mothercomboViewer = new ComboViewer(motherCombo); + ViewerSupport.bind(mothercomboViewer, viewModel.getPeople(), + BeanProperties.value(Person.class, "name")); - dbc.bindValue(WidgetProperties.text(SWT.Modify).observe(nameText), - BeanProperties.value(Person.class, "name").observeDetail( - masterSelection)); + dbc.bindValue(ViewerProperties.singleSelection().observe( + mothercomboViewer), BeanProperties.value(Person.class, + "mother").observeDetail(masterSelection)); - ComboViewer mothercomboViewer = new ComboViewer(motherCombo); - ViewerSupport.bind(mothercomboViewer, viewModel.getPeople(), - BeanProperties.value(Person.class, "name")); + ComboViewer fatherComboViewer = new ComboViewer(fatherCombo); + ViewerSupport.bind(fatherComboViewer, viewModel.getPeople(), + BeanProperties.value(Person.class, "name")); - dbc.bindValue(ViewerProperties.singleSelection().observe( - mothercomboViewer), BeanProperties.value(Person.class, - "mother").observeDetail(masterSelection)); - - ComboViewer fatherComboViewer = new ComboViewer(fatherCombo); - ViewerSupport.bind(fatherComboViewer, viewModel.getPeople(), - BeanProperties.value(Person.class, "name")); - - dbc.bindValue(ViewerProperties.singleSelection().observe( - fatherComboViewer), BeanProperties.value(Person.class, - "father").observeDetail(masterSelection)); - } - } + dbc.bindValue(ViewerProperties.singleSelection().observe( + fatherComboViewer), BeanProperties.value(Person.class, + "father").observeDetail(masterSelection)); + } + } } +void main( String[] args ){ + Snippet025TableViewerWithPropertyDerivedColumns.main(args); +}