|
Added
Link Here
|
| 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 |
* The Pampered Chef, Inc. - initial API and implementation |
| 10 |
* Tom Schindl - cell editing |
| 11 |
* Matthew Hall - bugs 260329, 260337 |
| 12 |
* Heiko Ahlig - bug 267712 |
| 13 |
*******************************************************************************/ |
| 14 |
|
| 15 |
package org.eclipse.jface.examples.databinding.snippets; |
| 16 |
|
| 17 |
import java.beans.PropertyChangeListener; |
| 18 |
import java.beans.PropertyChangeSupport; |
| 19 |
import java.util.LinkedList; |
| 20 |
import java.util.List; |
| 21 |
|
| 22 |
import org.eclipse.core.databinding.DataBindingContext; |
| 23 |
import org.eclipse.core.databinding.beans.BeanProperties; |
| 24 |
import org.eclipse.core.databinding.beans.BeansObservables; |
| 25 |
import org.eclipse.core.databinding.beans.IBeanValueProperty; |
| 26 |
import org.eclipse.core.databinding.observable.Realm; |
| 27 |
import org.eclipse.core.databinding.observable.list.WritableList; |
| 28 |
import org.eclipse.core.databinding.observable.map.IObservableMap; |
| 29 |
import org.eclipse.core.databinding.observable.value.IObservableValue; |
| 30 |
import org.eclipse.core.databinding.property.Properties; |
| 31 |
import org.eclipse.jface.databinding.swt.SWTObservables; |
| 32 |
import org.eclipse.jface.databinding.swt.WidgetProperties; |
| 33 |
import org.eclipse.jface.databinding.viewers.CellEditorProperties; |
| 34 |
import org.eclipse.jface.databinding.viewers.ObservableListContentProvider; |
| 35 |
import org.eclipse.jface.databinding.viewers.ObservableMapCellLabelProvider; |
| 36 |
import org.eclipse.jface.databinding.viewers.ObservableValueEditingSupport; |
| 37 |
import org.eclipse.jface.databinding.viewers.ViewersObservables; |
| 38 |
import org.eclipse.jface.viewers.TableViewer; |
| 39 |
import org.eclipse.jface.viewers.TableViewerColumn; |
| 40 |
import org.eclipse.jface.viewers.TextCellEditor; |
| 41 |
import org.eclipse.swt.SWT; |
| 42 |
import org.eclipse.swt.layout.GridData; |
| 43 |
import org.eclipse.swt.layout.GridLayout; |
| 44 |
import org.eclipse.swt.widgets.Display; |
| 45 |
import org.eclipse.swt.widgets.Label; |
| 46 |
import org.eclipse.swt.widgets.Shell; |
| 47 |
import org.eclipse.swt.widgets.Table; |
| 48 |
|
| 49 |
/** |
| 50 |
* Demonstrates binding a TableViewer with multiple columns to a collection. |
| 51 |
*/ |
| 52 |
public class Snippet032TableViewerColumnEditing { |
| 53 |
public static void main(String[] args) { |
| 54 |
final Display display = new Display(); |
| 55 |
Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() { |
| 56 |
public void run() { |
| 57 |
ViewModel viewModel = new ViewModel(); |
| 58 |
Shell shell = new View(viewModel).createShell(); |
| 59 |
|
| 60 |
// The SWT event loop |
| 61 |
while (!shell.isDisposed()) { |
| 62 |
if (!display.readAndDispatch()) { |
| 63 |
display.sleep(); |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
}); |
| 68 |
} |
| 69 |
|
| 70 |
// Minimal JavaBeans support |
| 71 |
public static abstract class AbstractModelObject { |
| 72 |
private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport( |
| 73 |
this); |
| 74 |
|
| 75 |
public void addPropertyChangeListener(PropertyChangeListener listener) { |
| 76 |
propertyChangeSupport.addPropertyChangeListener(listener); |
| 77 |
} |
| 78 |
|
| 79 |
public void addPropertyChangeListener(String propertyName, |
| 80 |
PropertyChangeListener listener) { |
| 81 |
propertyChangeSupport.addPropertyChangeListener(propertyName, |
| 82 |
listener); |
| 83 |
} |
| 84 |
|
| 85 |
public void removePropertyChangeListener(PropertyChangeListener listener) { |
| 86 |
propertyChangeSupport.removePropertyChangeListener(listener); |
| 87 |
} |
| 88 |
|
| 89 |
public void removePropertyChangeListener(String propertyName, |
| 90 |
PropertyChangeListener listener) { |
| 91 |
propertyChangeSupport.removePropertyChangeListener(propertyName, |
| 92 |
listener); |
| 93 |
} |
| 94 |
|
| 95 |
protected void firePropertyChange(String propertyName, Object oldValue, |
| 96 |
Object newValue) { |
| 97 |
propertyChangeSupport.firePropertyChange(propertyName, oldValue, |
| 98 |
newValue); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
// The data model class. This is normally a persistent class of some sort. |
| 103 |
static class Person extends AbstractModelObject { |
| 104 |
// A property... |
| 105 |
String name = "Smith"; |
| 106 |
String firstName = "John"; |
| 107 |
|
| 108 |
public Person(String firstName, String name) { |
| 109 |
this.name = name; |
| 110 |
this.firstName = firstName; |
| 111 |
} |
| 112 |
|
| 113 |
public String getName() { |
| 114 |
return name; |
| 115 |
} |
| 116 |
|
| 117 |
public void setName(String name) { |
| 118 |
String oldValue = this.name; |
| 119 |
this.name = name; |
| 120 |
firePropertyChange("name", oldValue, name); |
| 121 |
} |
| 122 |
|
| 123 |
public String getFirstName() { |
| 124 |
return firstName; |
| 125 |
} |
| 126 |
|
| 127 |
public void setFirstName(String firstName) { |
| 128 |
String oldValue = this.firstName; |
| 129 |
this.firstName = firstName; |
| 130 |
firePropertyChange("firstName", oldValue, firstName); |
| 131 |
} |
| 132 |
|
| 133 |
} |
| 134 |
|
| 135 |
// The View's model--the root of our Model graph for this particular GUI. |
| 136 |
// |
| 137 |
// Typically each View class has a corresponding ViewModel class. |
| 138 |
// The ViewModel is responsible for getting the objects to edit from the |
| 139 |
// data access tier. Since this snippet doesn't have any persistent objects |
| 140 |
// ro retrieve, this ViewModel just instantiates a model object to edit. |
| 141 |
static class ViewModel { |
| 142 |
// The model to bind |
| 143 |
private List people = new LinkedList(); |
| 144 |
{ |
| 145 |
people.add(new Person("Dave", "Orme")); |
| 146 |
people.add(new Person("Gili", "Mendel")); |
| 147 |
people.add(new Person("Joe", "Winchester")); |
| 148 |
people.add(new Person("Boris", "Bokowski")); |
| 149 |
people.add(new Person("Brad", "Reynolds")); |
| 150 |
people.add(new Person("Matthew", "Hall")); |
| 151 |
} |
| 152 |
|
| 153 |
public List getPeople() { |
| 154 |
return people; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
// The GUI view |
| 159 |
static class View { |
| 160 |
private ViewModel viewModel; |
| 161 |
private Table committers; |
| 162 |
private Label selectedCommitterName; |
| 163 |
private Label selectedCommitterFirstName; |
| 164 |
|
| 165 |
public View(ViewModel viewModel) { |
| 166 |
this.viewModel = viewModel; |
| 167 |
} |
| 168 |
|
| 169 |
public Shell createShell() { |
| 170 |
// Build a UI |
| 171 |
Display display = Display.getDefault(); |
| 172 |
Shell shell = new Shell(display); |
| 173 |
shell.setLayout(new GridLayout(2, true)); |
| 174 |
committers = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION); |
| 175 |
committers.setLinesVisible(true); |
| 176 |
committers.setHeaderVisible(true); |
| 177 |
GridData layoutData = new GridData(SWT.FILL, SWT.FILL, true, true); |
| 178 |
layoutData.horizontalSpan = 2; |
| 179 |
committers.setLayoutData(layoutData); |
| 180 |
|
| 181 |
GridData fieldLayoutData = new GridData(SWT.FILL, SWT.BEGINNING, |
| 182 |
true, false); |
| 183 |
selectedCommitterName = new Label(shell, SWT.NONE); |
| 184 |
selectedCommitterName.setLayoutData(fieldLayoutData); |
| 185 |
|
| 186 |
selectedCommitterFirstName = new Label(shell, SWT.NONE); |
| 187 |
selectedCommitterFirstName.setLayoutData(fieldLayoutData); |
| 188 |
|
| 189 |
DataBindingContext bindingContext = new DataBindingContext(); |
| 190 |
bindGUI(bindingContext); |
| 191 |
|
| 192 |
// Open and return the Shell |
| 193 |
shell.setSize(250, 300); |
| 194 |
shell.open(); |
| 195 |
return shell; |
| 196 |
} |
| 197 |
|
| 198 |
protected void bindGUI(DataBindingContext bindingContext) { |
| 199 |
// Since we're using a JFace Viewer, we do first wrap our Table... |
| 200 |
TableViewer peopleViewer = new TableViewer(committers); |
| 201 |
TableViewerColumn columnName = new TableViewerColumn(peopleViewer, |
| 202 |
SWT.NONE); |
| 203 |
columnName.getColumn().setText("Name"); |
| 204 |
columnName.getColumn().setWidth(100); |
| 205 |
|
| 206 |
TableViewerColumn columnFirstName = new TableViewerColumn( |
| 207 |
peopleViewer, SWT.NONE); |
| 208 |
columnFirstName.getColumn().setText("FirstName"); |
| 209 |
columnFirstName.getColumn().setWidth(100); |
| 210 |
|
| 211 |
// Bind viewer to model |
| 212 |
IBeanValueProperty propName = BeanProperties.value(Person.class, |
| 213 |
"name"); |
| 214 |
IBeanValueProperty propFirstname = BeanProperties.value( |
| 215 |
Person.class, "firstName"); |
| 216 |
|
| 217 |
columnName.setEditingSupport(ObservableValueEditingSupport |
| 218 |
.create(peopleViewer, bindingContext, new TextCellEditor( |
| 219 |
committers), CellEditorProperties.control().value( |
| 220 |
WidgetProperties.text()), propName)); |
| 221 |
columnFirstName.setEditingSupport(ObservableValueEditingSupport |
| 222 |
.create(peopleViewer, bindingContext, new TextCellEditor( |
| 223 |
committers), CellEditorProperties.control().value( |
| 224 |
WidgetProperties.text()), propFirstname)); |
| 225 |
|
| 226 |
ObservableListContentProvider contentProvider = new ObservableListContentProvider(); |
| 227 |
peopleViewer.setContentProvider(contentProvider); |
| 228 |
|
| 229 |
// Bind the LabelProviders to the model and columns |
| 230 |
IObservableMap[] result = Properties.observeEach(contentProvider |
| 231 |
.getKnownElements(), new IBeanValueProperty[] { propName, |
| 232 |
propFirstname }); |
| 233 |
|
| 234 |
columnName.setLabelProvider(new ObservableMapCellLabelProvider( |
| 235 |
result[0])); |
| 236 |
columnFirstName |
| 237 |
.setLabelProvider(new ObservableMapCellLabelProvider( |
| 238 |
result[1])); |
| 239 |
peopleViewer.setInput(new WritableList(viewModel.getPeople(), |
| 240 |
Person.class)); |
| 241 |
|
| 242 |
// bind selectedCommitter labels to the name and forname of the |
| 243 |
// current selection |
| 244 |
IObservableValue selection = ViewersObservables |
| 245 |
.observeSingleSelection(peopleViewer); |
| 246 |
bindingContext.bindValue(SWTObservables |
| 247 |
.observeText(selectedCommitterName), BeansObservables |
| 248 |
.observeDetailValue(selection, "name", String.class)); |
| 249 |
bindingContext.bindValue(SWTObservables |
| 250 |
.observeText(selectedCommitterFirstName), BeansObservables |
| 251 |
.observeDetailValue(selection, "firstName", String.class)); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
} |