comparison org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet026AnonymousBeanProperties.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) 2008 Matthew Hall 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 * Matthew Hall - initial API and implementation (bug 247997)
10 * Matthew Hall - bugs 261843, 260337, 265561
11 ******************************************************************************/
12
13 package org.eclipse.jface.examples.databinding.snippets;
14
15 import java.beans.PropertyChangeEvent;
16 import java.beans.PropertyChangeListener;
17 import java.beans.PropertyChangeSupport;
18 import java.util.Collections;
19 import java.util.Iterator;
20 import java.util.Set;
21 import java.util.TreeSet;
22
23 import org.eclipse.core.databinding.DataBindingContext;
24 import org.eclipse.core.databinding.beans.BeanProperties;
25 import org.eclipse.core.databinding.observable.Realm;
26 import org.eclipse.core.databinding.observable.set.SetDiff;
27 import org.eclipse.core.databinding.observable.value.ComputedValue;
28 import org.eclipse.core.databinding.observable.value.IObservableValue;
29 import org.eclipse.core.databinding.property.INativePropertyListener;
30 import org.eclipse.core.databinding.property.IProperty;
31 import org.eclipse.core.databinding.property.ISimplePropertyListener;
32 import org.eclipse.core.databinding.property.NativePropertyListener;
33 import org.eclipse.core.databinding.property.set.DelegatingSetProperty;
34 import org.eclipse.core.databinding.property.set.ISetProperty;
35 import org.eclipse.core.databinding.property.set.SimpleSetProperty;
36 import org.eclipse.jface.databinding.swt.SWTObservables;
37 import org.eclipse.jface.databinding.swt.WidgetProperties;
38 import org.eclipse.jface.databinding.viewers.ViewerProperties;
39 import org.eclipse.jface.databinding.viewers.ViewerSupport;
40 import org.eclipse.jface.viewers.ArrayContentProvider;
41 import org.eclipse.jface.viewers.ComboViewer;
42 import org.eclipse.jface.viewers.TreeViewer;
43 import org.eclipse.swt.SWT;
44 import org.eclipse.swt.layout.GridData;
45 import org.eclipse.swt.layout.GridLayout;
46 import org.eclipse.swt.widgets.Combo;
47 import org.eclipse.swt.widgets.Display;
48 import org.eclipse.swt.widgets.Label;
49 import org.eclipse.swt.widgets.Shell;
50 import org.eclipse.swt.widgets.Text;
51 import org.eclipse.swt.widgets.Tree;
52 import org.eclipse.swt.widgets.TreeColumn;
53
54 /**
55 * @since 3.2
56 *
57 */
58 public class Snippet026AnonymousBeanProperties {
59 private ComboViewer statusViewer;
60 private Combo combo;
61 private Text nameText;
62 private TreeViewer contactViewer;
63
64 public static void main(String[] args) {
65 Display display = new Display();
66 Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
67 public void run() {
68 try {
69 Snippet026AnonymousBeanProperties window = new Snippet026AnonymousBeanProperties();
70 window.open();
71 } catch (Exception e) {
72 e.printStackTrace();
73 }
74 }
75 });
76 }
77
78 private ApplicationModel model;
79 private Shell shell;
80 private Tree tree;
81
82 // Minimal JavaBeans support
83 public static abstract class AbstractModelObject {
84 private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(
85 this);
86
87 public void addPropertyChangeListener(PropertyChangeListener listener) {
88 propertyChangeSupport.addPropertyChangeListener(listener);
89 }
90
91 public void addPropertyChangeListener(String propertyName,
92 PropertyChangeListener listener) {
93 propertyChangeSupport.addPropertyChangeListener(propertyName,
94 listener);
95 }
96
97 public void removePropertyChangeListener(PropertyChangeListener listener) {
98 propertyChangeSupport.removePropertyChangeListener(listener);
99 }
100
101 public void removePropertyChangeListener(String propertyName,
102 PropertyChangeListener listener) {
103 propertyChangeSupport.removePropertyChangeListener(propertyName,
104 listener);
105 }
106
107 protected void firePropertyChange(String propertyName, Object oldValue,
108 Object newValue) {
109 propertyChangeSupport.firePropertyChange(propertyName, oldValue,
110 newValue);
111 }
112 }
113
114 public static class ContactGroup extends AbstractModelObject implements
115 Comparable {
116 private String name;
117 private Set contacts = new TreeSet();
118
119 ContactGroup(String name) {
120 this.name = checkNull(name);
121 }
122
123 private String checkNull(String string) {
124 if (string == null)
125 throw new NullPointerException();
126 return string;
127 }
128
129 public String getName() {
130 return name;
131 }
132
133 public void setName(String name) {
134 firePropertyChange("name", this.name, this.name = checkNull(name));
135 }
136
137 public Set getContacts() {
138 return new TreeSet(contacts);
139 }
140
141 public void addContact(Contact contact) {
142 Set oldValue = getContacts();
143 contacts.add(contact);
144 Set newValue = getContacts();
145 firePropertyChange("contacts", oldValue, newValue);
146 }
147
148 public void removeContact(Contact contact) {
149 Set oldValue = getContacts();
150 contacts.remove(contact);
151 Set newValue = getContacts();
152 firePropertyChange("contacts", oldValue, newValue);
153 }
154
155 public int compareTo(Object o) {
156 ContactGroup that = (ContactGroup) o;
157 return this.name.compareTo(that.name);
158 }
159 }
160
161 public static class Contact extends AbstractModelObject implements
162 Comparable {
163 private String name;
164 private String status;
165
166 private String checkNull(String string) {
167 if (string == null)
168 throw new NullPointerException();
169 return string;
170 }
171
172 public Contact(String name, String status) {
173 this.name = checkNull(name);
174 this.status = checkNull(status);
175 }
176
177 public String getName() {
178 return name;
179 }
180
181 public void setName(String name) {
182 firePropertyChange("name", this.name, this.name = checkNull(name));
183 }
184
185 public String getStatus() {
186 return status;
187 }
188
189 public void setStatus(String status) {
190 firePropertyChange("status", this.status,
191 this.status = checkNull(status));
192 }
193
194 public int compareTo(Object o) {
195 Contact that = (Contact) o;
196 int result = this.name.compareTo(that.name);
197 if (result == 0)
198 result = this.status.compareTo(that.status);
199 return result;
200 }
201 }
202
203 public static class ApplicationModel extends AbstractModelObject {
204 private Set groups = new TreeSet();
205
206 public Set getGroups() {
207 return new TreeSet(groups);
208 }
209
210 public void setGroups(Set groups) {
211 Set oldValue = getGroups();
212 this.groups = new TreeSet(groups);
213 Set newValue = getGroups();
214 firePropertyChange("groups", oldValue, newValue);
215 }
216 }
217
218 /**
219 * Set property for the "contacts" property of a ContactGroup. Since
220 * ContactGroup does not have a setContacts() method we have to write our
221 * own property to apply set changes incrementally through the addContact
222 * and removeContact methods.
223 */
224 public static class ContactGroupContactsProperty extends SimpleSetProperty {
225 public Object getElementType() {
226 return Contact.class;
227 }
228
229 protected Set doGetSet(Object source) {
230 if (source == null)
231 return Collections.EMPTY_SET;
232 return ((ContactGroup) source).getContacts();
233 }
234
235 protected void doSetSet(Object source, Set set, SetDiff diff) {
236 ContactGroup group = (ContactGroup) source;
237 for (Iterator it = diff.getRemovals().iterator(); it.hasNext();) {
238 Contact contact = (Contact) it.next();
239 group.removeContact(contact);
240 }
241 for (Iterator it = diff.getAdditions().iterator(); it.hasNext();) {
242 Contact contact = (Contact) it.next();
243 group.addContact(contact);
244 }
245 }
246
247 public INativePropertyListener adaptListener(
248 final ISimplePropertyListener listener) {
249 return new Listener(this, listener);
250 }
251
252 private class Listener extends NativePropertyListener implements
253 PropertyChangeListener {
254 Listener(IProperty property, ISimplePropertyListener listener) {
255 super(property, listener);
256 }
257
258 public void propertyChange(PropertyChangeEvent evt) {
259 fireChange(evt.getSource(), null);
260 }
261
262 protected void doAddTo(Object source) {
263 ((ContactGroup) source).addPropertyChangeListener("contacts",
264 this);
265 }
266
267 protected void doRemoveFrom(Object source) {
268 ((ContactGroup) source).removePropertyChangeListener(
269 "contacts", this);
270 }
271 }
272 }
273
274 public void open() {
275 model = createDefaultModel();
276
277 final Display display = Display.getDefault();
278 createContents();
279 bindUI();
280 shell.open();
281 shell.layout();
282 while (!shell.isDisposed()) {
283 if (!display.readAndDispatch())
284 display.sleep();
285 }
286 }
287
288 private static final String[] statuses = new String[] { "Online", "Idle",
289 "Busy", "Offline" };
290
291 /**
292 * @return
293 */
294 private ApplicationModel createDefaultModel() {
295 ContactGroup swtGroup = new ContactGroup("SWT");
296 swtGroup.addContact(new Contact("Steve Northover", "Busy"));
297 swtGroup.addContact(new Contact("Grant Gayed", "Online"));
298 swtGroup.addContact(new Contact("Veronika Irvine", "Offline"));
299 swtGroup.addContact(new Contact("Mike Wilson", "Online"));
300 swtGroup.addContact(new Contact("Christophe Cornu", "Idle"));
301 swtGroup.addContact(new Contact("Lynne Kues", "Online"));
302 swtGroup.addContact(new Contact("Silenio Quarti", "Idle"));
303
304 ContactGroup jdbGroup = new ContactGroup("JFace Data Binding");
305 jdbGroup.addContact(new Contact("Boris Bokowski", "Online"));
306 jdbGroup.addContact(new Contact("Matthew Hall", "Idle"));
307
308 Set groups = new TreeSet();
309 groups.add(swtGroup);
310 groups.add(jdbGroup);
311 ApplicationModel model = new ApplicationModel();
312 model.setGroups(groups);
313
314 return model;
315 }
316
317 /**
318 * Create contents of the window
319 */
320 protected void createContents() {
321 shell = new Shell();
322 shell.setSize(379, 393);
323 shell.setText("Snippet026AnonymousBeanProperties");
324 final GridLayout gridLayout = new GridLayout();
325 gridLayout.numColumns = 4;
326 shell.setLayout(gridLayout);
327
328 contactViewer = new TreeViewer(shell, SWT.BORDER);
329 tree = contactViewer.getTree();
330 tree.setHeaderVisible(true);
331 tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 4, 1));
332
333 final TreeColumn nameColumn = new TreeColumn(tree, SWT.NONE);
334 nameColumn.setWidth(163);
335 nameColumn.setText("Name");
336
337 final TreeColumn newColumnTreeColumn = new TreeColumn(tree, SWT.NONE);
338 newColumnTreeColumn.setWidth(100);
339 newColumnTreeColumn.setText("Status");
340
341 final Label nameLabel = new Label(shell, SWT.NONE);
342 nameLabel.setText("Name");
343
344 nameText = new Text(shell, SWT.BORDER);
345 final GridData gd_nameText = new GridData(SWT.FILL, SWT.CENTER, true,
346 false);
347 nameText.setLayoutData(gd_nameText);
348
349 final Label statusLabel = new Label(shell, SWT.NONE);
350 statusLabel.setLayoutData(new GridData());
351 statusLabel.setText("Status");
352
353 statusViewer = new ComboViewer(shell, SWT.READ_ONLY);
354 combo = statusViewer.getCombo();
355 combo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
356 }
357
358 private void bindUI() {
359 ISetProperty treeChildrenProperty = new DelegatingSetProperty() {
360 ISetProperty modelGroups = BeanProperties.set(
361 ApplicationModel.class, "groups");
362 ISetProperty groupContacts = BeanProperties.set(ContactGroup.class,
363 "contacts");
364
365 protected ISetProperty doGetDelegate(Object source) {
366 if (source instanceof ApplicationModel)
367 return modelGroups;
368 if (source instanceof ContactGroup)
369 return groupContacts;
370 return null;
371 }
372 };
373
374 ViewerSupport.bind(contactViewer, model, treeChildrenProperty,
375 BeanProperties.values(new String[] { "name", "status" }));
376
377 contactViewer.expandAll();
378
379 final IObservableValue selection = ViewerProperties.singleSelection()
380 .observe(contactViewer);
381
382 DataBindingContext dbc = new DataBindingContext();
383
384 dbc.bindValue(WidgetProperties.text(SWT.Modify).observe(nameText),
385 BeanProperties.value("name").observeDetail(selection));
386
387 statusViewer.setContentProvider(new ArrayContentProvider());
388 statusViewer.setInput(statuses);
389
390 dbc.bindValue(ViewerProperties.singleSelection().observe(statusViewer),
391 BeanProperties.value("status").observeDetail(selection));
392
393 dbc.bindValue(WidgetProperties.enabled().observe(
394 statusViewer.getControl()), new ComputedValue() {
395 protected Object calculate() {
396 return Boolean.valueOf(selection.getValue() instanceof Contact);
397 }
398 });
399 }
400 }