comparison 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
comparison
equal deleted inserted replaced
98:48d4ee626868 99:5d5bd660917f
8 * Contributors: 8 * Contributors:
9 * Coconut Palm Software, Inc. - Initial API and implementation 9 * Coconut Palm Software, Inc. - Initial API and implementation
10 * Matthew Hall - bug 195222, 261843, 260337 10 * Matthew Hall - bug 195222, 261843, 260337
11 ******************************************************************************/ 11 ******************************************************************************/
12 12
13 package org.eclipse.jface.examples.databinding.snippets; 13 module org.eclipse.jface.examples.databinding.snippets.Snippet025TableViewerWithPropertyDerivedColumns;
14
15 import java.lang.all;
14 16
15 import java.beans.PropertyChangeListener; 17 import java.beans.PropertyChangeListener;
16 import java.beans.PropertyChangeSupport; 18 import java.beans.PropertyChangeSupport;
17 19
18 import org.eclipse.core.databinding.DataBindingContext; 20 import org.eclipse.core.databinding.DataBindingContext;
42 44
43 /** 45 /**
44 * Demonstrates binding a TableViewer to a collection. 46 * Demonstrates binding a TableViewer to a collection.
45 */ 47 */
46 public class Snippet025TableViewerWithPropertyDerivedColumns { 48 public class Snippet025TableViewerWithPropertyDerivedColumns {
47 public static void main(String[] args) { 49 public static void main(String[] args) {
48 final Display display = new Display(); 50 final Display display = new Display();
49 51
50 // Set up data binding. In an RCP application, the threading Realm 52 // Set up data binding. In an RCP application, the threading Realm
51 // will be set for you automatically by the Workbench. In an SWT 53 // will be set for you automatically by the Workbench. In an SWT
52 // application, you can do this once, wrapping your binding 54 // application, you can do this once, wrapping your binding
53 // method call. 55 // method call.
54 Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() { 56 Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
55 public void run() { 57 public void run() {
56 ViewModel viewModel = new ViewModel(); 58 ViewModel viewModel = new ViewModel();
57 Shell shell = new View(viewModel).createShell(); 59 Shell shell = new View(viewModel).createShell();
58 60
59 // The SWT event loop 61 // The SWT event loop
60 while (!shell.isDisposed()) { 62 while (!shell.isDisposed()) {
61 if (!display.readAndDispatch()) { 63 if (!display.readAndDispatch()) {
62 display.sleep(); 64 display.sleep();
63 } 65 }
64 } 66 }
65 } 67 }
66 }); 68 });
67 } 69 }
68 70
69 // Minimal JavaBeans support 71 // Minimal JavaBeans support
70 public static abstract class AbstractModelObject { 72 public static abstract class AbstractModelObject {
71 private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport( 73 private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(
72 this); 74 this);
73 75
74 public void addPropertyChangeListener(PropertyChangeListener listener) { 76 public void addPropertyChangeListener(PropertyChangeListener listener) {
75 propertyChangeSupport.addPropertyChangeListener(listener); 77 propertyChangeSupport.addPropertyChangeListener(listener);
76 } 78 }
77 79
78 public void addPropertyChangeListener(String propertyName, 80 public void addPropertyChangeListener(String propertyName,
79 PropertyChangeListener listener) { 81 PropertyChangeListener listener) {
80 propertyChangeSupport.addPropertyChangeListener(propertyName, 82 propertyChangeSupport.addPropertyChangeListener(propertyName,
81 listener); 83 listener);
82 } 84 }
83 85
84 public void removePropertyChangeListener(PropertyChangeListener listener) { 86 public void removePropertyChangeListener(PropertyChangeListener listener) {
85 propertyChangeSupport.removePropertyChangeListener(listener); 87 propertyChangeSupport.removePropertyChangeListener(listener);
86 } 88 }
87 89
88 public void removePropertyChangeListener(String propertyName, 90 public void removePropertyChangeListener(String propertyName,
89 PropertyChangeListener listener) { 91 PropertyChangeListener listener) {
90 propertyChangeSupport.removePropertyChangeListener(propertyName, 92 propertyChangeSupport.removePropertyChangeListener(propertyName,
91 listener); 93 listener);
92 } 94 }
93 95
94 protected void firePropertyChange(String propertyName, Object oldValue, 96 protected void firePropertyChange(String propertyName, Object oldValue,
95 Object newValue) { 97 Object newValue) {
96 propertyChangeSupport.firePropertyChange(propertyName, oldValue, 98 propertyChangeSupport.firePropertyChange(propertyName, oldValue,
97 newValue); 99 newValue);
98 } 100 }
99 } 101 }
100 102
101 private static Person UNKNOWN = new Person("unknown", null, null); 103 private static Person UNKNOWN = new Person("unknown", null, null);
102 104
103 // The data model class. This is normally a persistent class of some sort. 105 // The data model class. This is normally a persistent class of some sort.
104 static class Person extends AbstractModelObject { 106 static class Person extends AbstractModelObject {
105 // A property... 107 // A property...
106 String name = "Donald Duck"; 108 String name = "Donald Duck";
107 Person mother; 109 Person mother;
108 Person father; 110 Person father;
109 111
110 public Person(String name, Person mother, Person father) { 112 public Person(String name, Person mother, Person father) {
111 this.name = name; 113 this.name = name;
112 this.mother = mother; 114 this.mother = mother;
113 this.father = father; 115 this.father = father;
114 } 116 }
115 117
116 public String getName() { 118 public String getName() {
117 return name; 119 return name;
118 } 120 }
119 121
120 public void setName(String name) { 122 public void setName(String name) {
121 firePropertyChange("name", this.name, this.name = name); 123 firePropertyChange("name", this.name, this.name = name);
122 } 124 }
123 125
124 public Person getMother() { 126 public Person getMother() {
125 return mother; 127 return mother;
126 } 128 }
127 129
128 public void setMother(Person mother) { 130 public void setMother(Person mother) {
129 firePropertyChange("mother", this.mother, this.mother = mother); 131 firePropertyChange("mother", this.mother, this.mother = mother);
130 } 132 }
131 133
132 public Person getFather() { 134 public Person getFather() {
133 return father; 135 return father;
134 } 136 }
135 137
136 public void setFather(Person father) { 138 public void setFather(Person father) {
137 firePropertyChange("father", this.father, this.father = father); 139 firePropertyChange("father", this.father, this.father = father);
138 } 140 }
139 } 141 }
140 142
141 // The View's model--the root of our Model graph for this particular GUI. 143 // The View's model--the root of our Model graph for this particular GUI.
142 // 144 //
143 // Typically each View class has a corresponding ViewModel class. 145 // Typically each View class has a corresponding ViewModel class.
144 // The ViewModel is responsible for getting the objects to edit from the 146 // The ViewModel is responsible for getting the objects to edit from the
145 // data access tier. Since this snippet doesn't have any persistent objects 147 // data access tier. Since this snippet doesn't have any persistent objects
146 // ro retrieve, this ViewModel just instantiates a model object to edit. 148 // ro retrieve, this ViewModel just instantiates a model object to edit.
147 static class ViewModel { 149 static class ViewModel {
148 // The model to bind 150 // The model to bind
149 private IObservableList people = new WritableList(); 151 private IObservableList people = new WritableList();
150 { 152 {
151 Person fergus = new Person("Fergus McDuck", UNKNOWN, UNKNOWN); 153 Person fergus = new Person("Fergus McDuck", UNKNOWN, UNKNOWN);
152 Person downy = new Person("Downy O'Drake", UNKNOWN, UNKNOWN); 154 Person downy = new Person("Downy O'Drake", UNKNOWN, UNKNOWN);
153 Person scrooge = new Person("Scrooge McDuck", downy, fergus); 155 Person scrooge = new Person("Scrooge McDuck", downy, fergus);
154 Person hortense = new Person("Hortense McDuck", downy, fergus); 156 Person hortense = new Person("Hortense McDuck", downy, fergus);
155 Person quackmore = new Person("Quackmore Duck", UNKNOWN, UNKNOWN); 157 Person quackmore = new Person("Quackmore Duck", UNKNOWN, UNKNOWN);
156 Person della = new Person("Della Duck", hortense, quackmore); 158 Person della = new Person("Della Duck", hortense, quackmore);
157 Person donald = new Person("Donald Duck", hortense, quackmore); 159 Person donald = new Person("Donald Duck", hortense, quackmore);
158 people.add(UNKNOWN); 160 people.add(UNKNOWN);
159 people.add(downy); 161 people.add(downy);
160 people.add(fergus); 162 people.add(fergus);
161 people.add(scrooge); 163 people.add(scrooge);
162 people.add(quackmore); 164 people.add(quackmore);
163 people.add(hortense); 165 people.add(hortense);
164 people.add(della); 166 people.add(della);
165 people.add(donald); 167 people.add(donald);
166 } 168 }
167 169
168 public IObservableList getPeople() { 170 public IObservableList getPeople() {
169 return people; 171 return people;
170 } 172 }
171 } 173 }
172 174
173 // The GUI view 175 // The GUI view
174 static class View { 176 static class View {
175 private ViewModel viewModel; 177 private ViewModel viewModel;
176 private Table duckFamily; 178 private Table duckFamily;
177 private Text nameText; 179 private Text nameText;
178 private Combo motherCombo; 180 private Combo motherCombo;
179 private Combo fatherCombo; 181 private Combo fatherCombo;
180 182
181 public View(ViewModel viewModel) { 183 public View(ViewModel viewModel) {
182 this.viewModel = viewModel; 184 this.viewModel = viewModel;
183 } 185 }
184 186
185 public Shell createShell() { 187 public Shell createShell() {
186 // Build a UI 188 // Build a UI
187 Display display = Display.getDefault(); 189 Display display = Display.getDefault();
188 Shell shell = new Shell(display); 190 Shell shell = new Shell(display);
189 duckFamily = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION); 191 duckFamily = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
190 duckFamily.setHeaderVisible(true); 192 duckFamily.setHeaderVisible(true);
191 GridDataFactory.defaultsFor(duckFamily).span(2, 1).applyTo( 193 GridDataFactory.defaultsFor(duckFamily).span(2, 1).applyTo(
192 duckFamily); 194 duckFamily);
193 createColumn("Name"); 195 createColumn("Name");
194 createColumn("Mother"); 196 createColumn("Mother");
195 createColumn("Father"); 197 createColumn("Father");
196 createColumn("Maternal Grandmother"); 198 createColumn("Maternal Grandmother");
197 createColumn("Maternal Grandfather"); 199 createColumn("Maternal Grandfather");
198 createColumn("Paternal Grandmother"); 200 createColumn("Paternal Grandmother");
199 createColumn("Paternal Grandfather"); 201 createColumn("Paternal Grandfather");
200 202
201 duckFamily.setLinesVisible(true); 203 duckFamily.setLinesVisible(true);
202 204
203 new Label(shell, SWT.NONE).setText("Name:"); 205 new Label(shell, SWT.NONE).setText("Name:");
204 nameText = new Text(shell, SWT.BORDER); 206 nameText = new Text(shell, SWT.BORDER);
205 GridDataFactory.defaultsFor(nameText).grab(true, false).applyTo( 207 GridDataFactory.defaultsFor(nameText).grab(true, false).applyTo(
206 nameText); 208 nameText);
207 209
208 new Label(shell, SWT.NONE).setText("Mother:"); 210 new Label(shell, SWT.NONE).setText("Mother:");
209 motherCombo = new Combo(shell, SWT.READ_ONLY); 211 motherCombo = new Combo(shell, SWT.READ_ONLY);
210 212
211 new Label(shell, SWT.NONE).setText("Father:"); 213 new Label(shell, SWT.NONE).setText("Father:");
212 fatherCombo = new Combo(shell, SWT.READ_ONLY); 214 fatherCombo = new Combo(shell, SWT.READ_ONLY);
213 215
214 DataBindingContext bindingContext = new DataBindingContext(); 216 DataBindingContext bindingContext = new DataBindingContext();
215 bindGUI(bindingContext); 217 bindGUI(bindingContext);
216 218
217 GridLayoutFactory.swtDefaults().numColumns(2).applyTo(shell); 219 GridLayoutFactory.swtDefaults().numColumns(2).applyTo(shell);
218 // Open and return the Shell 220 // Open and return the Shell
219 shell.setSize(800, 300); 221 shell.setSize(800, 300);
220 shell.open(); 222 shell.open();
221 return shell; 223 return shell;
222 } 224 }
223 225
224 private void createColumn(String string) { 226 private void createColumn(String string) {
225 final TableColumn column = new TableColumn(duckFamily, SWT.NONE); 227 final TableColumn column = new TableColumn(duckFamily, SWT.NONE);
226 column.setText(string); 228 column.setText(string);
227 column.pack(); 229 column.pack();
228 if (column.getWidth() < 100) 230 if (column.getWidth() < 100)
229 column.setWidth(100); 231 column.setWidth(100);
230 } 232 }
231 233
232 protected void bindGUI(DataBindingContext dbc) { 234 protected void bindGUI(DataBindingContext dbc) {
233 // Since we're using a JFace Viewer, we do first wrap our Table... 235 // Since we're using a JFace Viewer, we do first wrap our Table...
234 TableViewer peopleViewer = new TableViewer(duckFamily); 236 TableViewer peopleViewer = new TableViewer(duckFamily);
235 peopleViewer.addFilter(new ViewerFilter() { 237 peopleViewer.addFilter(new ViewerFilter() {
236 public boolean select(Viewer viewer, Object parentElement, 238 public bool select(Viewer viewer, Object parentElement,
237 Object element) { 239 Object element) {
238 return element != UNKNOWN; 240 return element != UNKNOWN;
239 } 241 }
240 }); 242 });
241 243
242 ViewerSupport.bind(peopleViewer, viewModel.getPeople(), 244 ViewerSupport.bind(peopleViewer, viewModel.getPeople(),
243 BeanProperties.values(Person.class, new String[] { "name", 245 BeanProperties.values(Person.class, new String[] { "name",
244 "mother.name", "father.name", "mother.mother.name", 246 "mother.name", "father.name", "mother.mother.name",
245 "mother.father.name", "father.mother.name", 247 "mother.father.name", "father.mother.name",
246 "father.father.name" })); 248 "father.father.name" }));
247 249
248 IObservableValue masterSelection = ViewerProperties 250 IObservableValue masterSelection = ViewerProperties
249 .singleSelection().observe(peopleViewer); 251 .singleSelection().observe(peopleViewer);
250 252
251 dbc.bindValue(WidgetProperties.text(SWT.Modify).observe(nameText), 253 dbc.bindValue(WidgetProperties.text(SWT.Modify).observe(nameText),
252 BeanProperties.value(Person.class, "name").observeDetail( 254 BeanProperties.value(Person.class, "name").observeDetail(
253 masterSelection)); 255 masterSelection));
254 256
255 ComboViewer mothercomboViewer = new ComboViewer(motherCombo); 257 ComboViewer mothercomboViewer = new ComboViewer(motherCombo);
256 ViewerSupport.bind(mothercomboViewer, viewModel.getPeople(), 258 ViewerSupport.bind(mothercomboViewer, viewModel.getPeople(),
257 BeanProperties.value(Person.class, "name")); 259 BeanProperties.value(Person.class, "name"));
258 260
259 dbc.bindValue(ViewerProperties.singleSelection().observe( 261 dbc.bindValue(ViewerProperties.singleSelection().observe(
260 mothercomboViewer), BeanProperties.value(Person.class, 262 mothercomboViewer), BeanProperties.value(Person.class,
261 "mother").observeDetail(masterSelection)); 263 "mother").observeDetail(masterSelection));
262 264
263 ComboViewer fatherComboViewer = new ComboViewer(fatherCombo); 265 ComboViewer fatherComboViewer = new ComboViewer(fatherCombo);
264 ViewerSupport.bind(fatherComboViewer, viewModel.getPeople(), 266 ViewerSupport.bind(fatherComboViewer, viewModel.getPeople(),
265 BeanProperties.value(Person.class, "name")); 267 BeanProperties.value(Person.class, "name"));
266 268
267 dbc.bindValue(ViewerProperties.singleSelection().observe( 269 dbc.bindValue(ViewerProperties.singleSelection().observe(
268 fatherComboViewer), BeanProperties.value(Person.class, 270 fatherComboViewer), BeanProperties.value(Person.class,
269 "father").observeDetail(masterSelection)); 271 "father").observeDetail(masterSelection));
270 } 272 }
271 } 273 }
272 } 274 }
275 void main( String[] args ){
276 Snippet025TableViewerWithPropertyDerivedColumns.main(args);
277 }