comparison org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet017TableViewerWithDerivedColumns.d @ 90:6086085e153d

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