comparison org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet013TableViewerEditing.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
9 * The Pampered Chef, Inc. - initial API and implementation 9 * The Pampered Chef, Inc. - initial API and implementation
10 * Tom Schindl - cell editing 10 * Tom Schindl - cell editing
11 * Matthew Hall - bugs 260329, 260337 11 * Matthew Hall - bugs 260329, 260337
12 ******************************************************************************/ 12 ******************************************************************************/
13 13
14 package org.eclipse.jface.examples.databinding.snippets; 14 module org.eclipse.jface.examples.databinding.snippets.Snippet013TableViewerEditing;
15
16 import java.lang.all;
15 17
16 import java.beans.PropertyChangeListener; 18 import java.beans.PropertyChangeListener;
17 import java.beans.PropertyChangeSupport; 19 import java.beans.PropertyChangeSupport;
18 import java.util.LinkedList; 20 import java.util.LinkedList;
19 import java.util.List; 21 import java.util.List;
44 46
45 /** 47 /**
46 * Demonstrates binding a TableViewer to a collection using the 3.3 Viewer APIs. 48 * Demonstrates binding a TableViewer to a collection using the 3.3 Viewer APIs.
47 */ 49 */
48 public class Snippet013TableViewerEditing { 50 public class Snippet013TableViewerEditing {
49 public static void main(String[] args) { 51 public static void main(String[] args) {
50 final Display display = new Display(); 52 final Display display = new Display();
51 Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() { 53 Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
52 public void run() { 54 public void run() {
53 ViewModel viewModel = new ViewModel(); 55 ViewModel viewModel = new ViewModel();
54 Shell shell = new View(viewModel).createShell(); 56 Shell shell = new View(viewModel).createShell();
55 57
56 // The SWT event loop 58 // The SWT event loop
57 while (!shell.isDisposed()) { 59 while (!shell.isDisposed()) {
58 if (!display.readAndDispatch()) { 60 if (!display.readAndDispatch()) {
59 display.sleep(); 61 display.sleep();
60 } 62 }
61 } 63 }
62 } 64 }
63 }); 65 });
64 } 66 }
65 67
66 // Minimal JavaBeans support 68 // Minimal JavaBeans support
67 public static abstract class AbstractModelObject { 69 public static abstract class AbstractModelObject {
68 private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport( 70 private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(
69 this); 71 this);
70 72
71 public void addPropertyChangeListener(PropertyChangeListener listener) { 73 public void addPropertyChangeListener(PropertyChangeListener listener) {
72 propertyChangeSupport.addPropertyChangeListener(listener); 74 propertyChangeSupport.addPropertyChangeListener(listener);
73 } 75 }
74 76
75 public void addPropertyChangeListener(String propertyName, 77 public void addPropertyChangeListener(String propertyName,
76 PropertyChangeListener listener) { 78 PropertyChangeListener listener) {
77 propertyChangeSupport.addPropertyChangeListener(propertyName, 79 propertyChangeSupport.addPropertyChangeListener(propertyName,
78 listener); 80 listener);
79 } 81 }
80 82
81 public void removePropertyChangeListener(PropertyChangeListener listener) { 83 public void removePropertyChangeListener(PropertyChangeListener listener) {
82 propertyChangeSupport.removePropertyChangeListener(listener); 84 propertyChangeSupport.removePropertyChangeListener(listener);
83 } 85 }
84 86
85 public void removePropertyChangeListener(String propertyName, 87 public void removePropertyChangeListener(String propertyName,
86 PropertyChangeListener listener) { 88 PropertyChangeListener listener) {
87 propertyChangeSupport.removePropertyChangeListener(propertyName, 89 propertyChangeSupport.removePropertyChangeListener(propertyName,
88 listener); 90 listener);
89 } 91 }
90 92
91 protected void firePropertyChange(String propertyName, Object oldValue, 93 protected void firePropertyChange(String propertyName, Object oldValue,
92 Object newValue) { 94 Object newValue) {
93 propertyChangeSupport.firePropertyChange(propertyName, oldValue, 95 propertyChangeSupport.firePropertyChange(propertyName, oldValue,
94 newValue); 96 newValue);
95 } 97 }
96 } 98 }
97 99
98 // The data model class. This is normally a persistent class of some sort. 100 // The data model class. This is normally a persistent class of some sort.
99 static class Person extends AbstractModelObject { 101 static class Person extends AbstractModelObject {
100 // A property... 102 // A property...
101 String name = "John Smith"; 103 String name = "John Smith";
102 104
103 public Person(String name) { 105 public Person(String name) {
104 this.name = name; 106 this.name = name;
105 } 107 }
106 108
107 public String getName() { 109 public String getName() {
108 return name; 110 return name;
109 } 111 }
110 112
111 public void setName(String name) { 113 public void setName(String name) {
112 String oldValue = this.name; 114 String oldValue = this.name;
113 this.name = name; 115 this.name = name;
114 firePropertyChange("name", oldValue, name); 116 firePropertyChange("name", oldValue, name);
115 } 117 }
116 } 118 }
117 119
118 // The View's model--the root of our Model graph for this particular GUI. 120 // The View's model--the root of our Model graph for this particular GUI.
119 // 121 //
120 // Typically each View class has a corresponding ViewModel class. 122 // Typically each View class has a corresponding ViewModel class.
121 // The ViewModel is responsible for getting the objects to edit from the 123 // The ViewModel is responsible for getting the objects to edit from the
122 // data access tier. Since this snippet doesn't have any persistent objects 124 // data access tier. Since this snippet doesn't have any persistent objects
123 // ro retrieve, this ViewModel just instantiates a model object to edit. 125 // ro retrieve, this ViewModel just instantiates a model object to edit.
124 static class ViewModel { 126 static class ViewModel {
125 // The model to bind 127 // The model to bind
126 private List people = new LinkedList(); 128 private List people = new LinkedList();
127 { 129 {
128 people.add(new Person("Steve Northover")); 130 people.add(new Person("Steve Northover"));
129 people.add(new Person("Grant Gayed")); 131 people.add(new Person("Grant Gayed"));
130 people.add(new Person("Veronika Irvine")); 132 people.add(new Person("Veronika Irvine"));
131 people.add(new Person("Mike Wilson")); 133 people.add(new Person("Mike Wilson"));
132 people.add(new Person("Christophe Cornu")); 134 people.add(new Person("Christophe Cornu"));
133 people.add(new Person("Lynne Kues")); 135 people.add(new Person("Lynne Kues"));
134 people.add(new Person("Silenio Quarti")); 136 people.add(new Person("Silenio Quarti"));
135 } 137 }
136 138
137 public List getPeople() { 139 public List getPeople() {
138 return people; 140 return people;
139 } 141 }
140 } 142 }
141 143
142 /** 144 /**
143 * Editing support that uses JFace Data Binding to control the editing 145 * Editing support that uses JFace Data Binding to control the editing
144 * lifecycle. The standard EditingSupport get/setValue(...) lifecycle is not 146 * lifecycle. The standard EditingSupport get/setValue(...) lifecycle is not
145 * used. 147 * used.
146 * 148 *
147 * @since 3.3 149 * @since 3.3
148 */ 150 */
149 private static class InlineEditingSupport extends 151 private static class InlineEditingSupport extends
150 ObservableValueEditingSupport { 152 ObservableValueEditingSupport {
151 private CellEditor cellEditor; 153 private CellEditor cellEditor;
152 154
153 /** 155 /**
154 * @param viewer 156 * @param viewer
155 * @param dbc 157 * @param dbc
156 */ 158 */
157 public InlineEditingSupport(ColumnViewer viewer, DataBindingContext dbc) { 159 public InlineEditingSupport(ColumnViewer viewer, DataBindingContext dbc) {
158 160
159 super(viewer, dbc); 161 super(viewer, dbc);
160 cellEditor = new TextCellEditor((Composite) viewer.getControl()); 162 cellEditor = new TextCellEditor((Composite) viewer.getControl());
161 } 163 }
162 164
163 protected CellEditor getCellEditor(Object element) { 165 protected CellEditor getCellEditor(Object element) {
164 return cellEditor; 166 return cellEditor;
165 } 167 }
166 168
167 protected IObservableValue doCreateCellEditorObservable( 169 protected IObservableValue doCreateCellEditorObservable(
168 CellEditor cellEditor) { 170 CellEditor cellEditor) {
169 171
170 return SWTObservables.observeText(cellEditor.getControl(), 172 return SWTObservables.observeText(cellEditor.getControl(),
171 SWT.Modify); 173 SWT.Modify);
172 } 174 }
173 175
174 protected IObservableValue doCreateElementObservable(Object element, 176 protected IObservableValue doCreateElementObservable(Object element,
175 ViewerCell cell) { 177 ViewerCell cell) {
176 return BeansObservables.observeValue(element, "name"); 178 return BeansObservables.observeValue(element, "name");
177 } 179 }
178 } 180 }
179 181
180 // The GUI view 182 // The GUI view
181 static class View { 183 static class View {
182 private ViewModel viewModel; 184 private ViewModel viewModel;
183 private Table committers; 185 private Table committers;
184 private Label selectedCommitter; 186 private Label selectedCommitter;
185 187
186 public View(ViewModel viewModel) { 188 public View(ViewModel viewModel) {
187 this.viewModel = viewModel; 189 this.viewModel = viewModel;
188 } 190 }
189 191
190 public Shell createShell() { 192 public Shell createShell() {
191 // Build a UI 193 // Build a UI
192 Display display = Display.getDefault(); 194 Display display = Display.getDefault();
193 Shell shell = new Shell(display); 195 Shell shell = new Shell(display);
194 shell.setLayout(new FillLayout(SWT.VERTICAL)); 196 shell.setLayout(new FillLayout(SWT.VERTICAL));
195 committers = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION); 197 committers = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
196 committers.setLinesVisible(true); 198 committers.setLinesVisible(true);
197 199
198 selectedCommitter = new Label(shell, SWT.NONE); 200 selectedCommitter = new Label(shell, SWT.NONE);
199 // Set up data binding. In an RCP application, the threading 201 // Set up data binding. In an RCP application, the threading
200 // Realm 202 // Realm
201 // will be set for you automatically by the Workbench. In an SWT 203 // will be set for you automatically by the Workbench. In an SWT
202 // application, you can do this once, wrpping your binding 204 // application, you can do this once, wrpping your binding
203 // method call. 205 // method call.
204 DataBindingContext bindingContext = new DataBindingContext(); 206 DataBindingContext bindingContext = new DataBindingContext();
205 bindGUI(bindingContext); 207 bindGUI(bindingContext);
206 208
207 // Open and return the Shell 209 // Open and return the Shell
208 shell.setSize(100, 300); 210 shell.setSize(100, 300);
209 shell.open(); 211 shell.open();
210 return shell; 212 return shell;
211 } 213 }
212 214
213 protected void bindGUI(DataBindingContext bindingContext) { 215 protected void bindGUI(DataBindingContext bindingContext) {
214 // Since we're using a JFace Viewer, we do first wrap our Table... 216 // Since we're using a JFace Viewer, we do first wrap our Table...
215 TableViewer peopleViewer = new TableViewer(committers); 217 TableViewer peopleViewer = new TableViewer(committers);
216 TableViewerColumn column = new TableViewerColumn(peopleViewer, 218 TableViewerColumn column = new TableViewerColumn(peopleViewer,
217 SWT.NONE); 219 SWT.NONE);
218 column.setEditingSupport(new InlineEditingSupport(peopleViewer, 220 column.setEditingSupport(new InlineEditingSupport(peopleViewer,
219 bindingContext)); 221 bindingContext));
220 column.getColumn().setWidth(100); 222 column.getColumn().setWidth(100);
221 223
222 // Bind viewer to model 224 // Bind viewer to model
223 ViewerSupport.bind(peopleViewer, new WritableList(viewModel 225 ViewerSupport.bind(peopleViewer, new WritableList(viewModel
224 .getPeople(), Person.class), BeanProperties.value( 226 .getPeople(), Person.class), BeanProperties.value(
225 Person.class, "name")); 227 Person.class, "name"));
226 228
227 // bind selectedCommitter label to the name of the current selection 229 // bind selectedCommitter label to the name of the current selection
228 IObservableValue selection = ViewersObservables 230 IObservableValue selection = ViewersObservables
229 .observeSingleSelection(peopleViewer); 231 .observeSingleSelection(peopleViewer);
230 bindingContext.bindValue(SWTObservables 232 bindingContext.bindValue(SWTObservables
231 .observeText(selectedCommitter), BeansObservables 233 .observeText(selectedCommitter), BeansObservables
232 .observeDetailValue(selection, "name", String.class)); 234 .observeDetailValue(selection, "name", String.class));
233 } 235 }
234 } 236 }
235 237
236 } 238 }
239 void main( String[] args ){
240 Snippet013TableViewerEditing.main(args);
241 }