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