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