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