Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2008 Ovidio Mallo 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 |
* Ovidio Mallo - initial API and implementation (bug 233191) |
10 |
******************************************************************************/ |
11 |
|
12 |
package org.eclipse.jface.examples.databinding.snippets; |
13 |
|
14 |
import java.util.Random; |
15 |
|
16 |
import org.eclipse.core.databinding.DataBindingContext; |
17 |
import org.eclipse.core.databinding.UpdateListStrategy; |
18 |
import org.eclipse.core.databinding.UpdateValueStrategy; |
19 |
import org.eclipse.core.databinding.conversion.Converter; |
20 |
import org.eclipse.core.databinding.conversion.IConverter; |
21 |
import org.eclipse.core.databinding.observable.Realm; |
22 |
import org.eclipse.core.databinding.observable.list.IObservableList; |
23 |
import org.eclipse.core.databinding.observable.list.WritableList; |
24 |
import org.eclipse.core.databinding.observable.value.IObservableValue; |
25 |
import org.eclipse.core.databinding.observable.value.WritableValue; |
26 |
import org.eclipse.jface.databinding.swt.SWTObservables; |
27 |
import org.eclipse.jface.databinding.viewers.ObservableListContentProvider; |
28 |
import org.eclipse.jface.layout.GridDataFactory; |
29 |
import org.eclipse.jface.layout.GridLayoutFactory; |
30 |
import org.eclipse.jface.viewers.IStructuredSelection; |
31 |
import org.eclipse.jface.viewers.LabelProvider; |
32 |
import org.eclipse.jface.viewers.ListViewer; |
33 |
import org.eclipse.swt.SWT; |
34 |
import org.eclipse.swt.events.KeyAdapter; |
35 |
import org.eclipse.swt.events.KeyEvent; |
36 |
import org.eclipse.swt.events.ModifyEvent; |
37 |
import org.eclipse.swt.events.ModifyListener; |
38 |
import org.eclipse.swt.events.SelectionAdapter; |
39 |
import org.eclipse.swt.events.SelectionEvent; |
40 |
import org.eclipse.swt.widgets.Button; |
41 |
import org.eclipse.swt.widgets.Composite; |
42 |
import org.eclipse.swt.widgets.Display; |
43 |
import org.eclipse.swt.widgets.Group; |
44 |
import org.eclipse.swt.widgets.Label; |
45 |
import org.eclipse.swt.widgets.Shell; |
46 |
import org.eclipse.swt.widgets.Spinner; |
47 |
import org.eclipse.swt.widgets.Text; |
48 |
|
49 |
/** |
50 |
* Snippet that demonstrates the usage of asynchronous bindings by defining an |
51 |
* asynchronous converter which introduces some artificial delay during |
52 |
* conversion. The example allows to manually change the target or model of a |
53 |
* ValueBinding/ListBinding and to see how the counterpart gets updated |
54 |
* asynchronously. In addition, automatic updates of the target and/or model can |
55 |
* be triggered. |
56 |
*/ |
57 |
public class Snippet022AsyncUpdate { |
58 |
|
59 |
private int updateDelay = 1000; |
60 |
|
61 |
private boolean randomizeUpdateDelay = true; |
62 |
|
63 |
private DataBindingContext dbc; |
64 |
|
65 |
private WritableValue valueTarget; |
66 |
|
67 |
private WritableValue valueModel; |
68 |
|
69 |
private WritableList listTarget; |
70 |
|
71 |
private WritableList listModel; |
72 |
|
73 |
public void createControl(Composite parent) { |
74 |
dbc = new DataBindingContext(); |
75 |
|
76 |
parent.setLayout(GridLayoutFactory.fillDefaults().margins(5, 5) |
77 |
.spacing(15, 10).create()); |
78 |
|
79 |
createSettingsGroup(parent); |
80 |
createValueBindingGroup(parent); |
81 |
createListBindingGroup(parent); |
82 |
createAutomaticUpdatesGroup(parent); |
83 |
} |
84 |
|
85 |
private void createSettingsGroup(Composite parent) { |
86 |
Group group = new Group(parent, SWT.NONE); |
87 |
group.setText("Settings"); |
88 |
group.setLayout(GridLayoutFactory.fillDefaults().numColumns(2).margins( |
89 |
5, 7).spacing(15, 10).create()); |
90 |
GridDataFactory.fillDefaults().grab(true, false).applyTo(group); |
91 |
|
92 |
new Label(group, SWT.NONE).setText("Update delay"); |
93 |
final Spinner updateDelaySpinner = new Spinner(group, SWT.BORDER); |
94 |
GridDataFactory.fillDefaults().grab(true, false).applyTo( |
95 |
updateDelaySpinner); |
96 |
updateDelaySpinner.setMinimum(1); |
97 |
updateDelaySpinner.setMaximum(Integer.MAX_VALUE); |
98 |
updateDelaySpinner.setSelection(updateDelay); |
99 |
updateDelaySpinner.addModifyListener(new ModifyListener() { |
100 |
public void modifyText(ModifyEvent e) { |
101 |
updateDelay = updateDelaySpinner.getSelection(); |
102 |
} |
103 |
}); |
104 |
|
105 |
final Button randomizeUpdateDelayButton = new Button(group, SWT.CHECK); |
106 |
randomizeUpdateDelayButton.setText("Randomize update delay"); |
107 |
GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo( |
108 |
randomizeUpdateDelayButton); |
109 |
randomizeUpdateDelayButton.setSelection(randomizeUpdateDelay); |
110 |
randomizeUpdateDelayButton.addSelectionListener(new SelectionAdapter() { |
111 |
public void widgetSelected(SelectionEvent e) { |
112 |
randomizeUpdateDelay = randomizeUpdateDelayButton |
113 |
.getSelection(); |
114 |
} |
115 |
}); |
116 |
} |
117 |
|
118 |
private void createValueBindingGroup(Composite parent) { |
119 |
Group group = new Group(parent, SWT.NONE); |
120 |
group.setText("Value Binding"); |
121 |
group.setLayout(GridLayoutFactory.fillDefaults().numColumns(2).margins( |
122 |
5, 7).spacing(15, 10).create()); |
123 |
GridDataFactory.fillDefaults().grab(true, false).applyTo(group); |
124 |
|
125 |
valueTarget = WritableValue.withValueType(String.class); |
126 |
createValueComponent(group, "Target", valueTarget); |
127 |
|
128 |
valueModel = WritableValue.withValueType(String.class); |
129 |
createValueComponent(group, "Model", valueModel); |
130 |
|
131 |
dbc.bindValue(valueTarget, valueModel, new UpdateValueStrategy() |
132 |
.setConverter(asyncConverter()), new UpdateValueStrategy() |
133 |
.setConverter(asyncConverter())); |
134 |
} |
135 |
|
136 |
private void createValueComponent(Composite parent, String title, |
137 |
IObservableValue observableValue) { |
138 |
new Label(parent, SWT.NONE).setText(title); |
139 |
Text text = new Text(parent, SWT.BORDER); |
140 |
GridDataFactory.fillDefaults().grab(true, false).applyTo(text); |
141 |
|
142 |
dbc.bindValue(SWTObservables.observeText(text, SWT.Modify), |
143 |
observableValue, null, null); |
144 |
} |
145 |
|
146 |
private void createListBindingGroup(Composite parent) { |
147 |
Group group = new Group(parent, SWT.NONE); |
148 |
group.setText("List Binding"); |
149 |
group.setLayout(GridLayoutFactory.fillDefaults().numColumns(2).margins( |
150 |
5, 7).spacing(15, 10).create()); |
151 |
GridDataFactory.fillDefaults().grab(true, true).applyTo(group); |
152 |
|
153 |
listTarget = WritableList.withElementType(String.class); |
154 |
createListComponent(group, "Target", listTarget); |
155 |
|
156 |
listModel = WritableList.withElementType(String.class); |
157 |
createListComponent(group, "Model", listModel); |
158 |
|
159 |
dbc.bindList(listTarget, listModel, new UpdateListStrategy() |
160 |
.setConverter(asyncConverter()), new UpdateListStrategy() |
161 |
.setConverter(asyncConverter())); |
162 |
} |
163 |
|
164 |
private void createListComponent(Composite parent, String title, |
165 |
final IObservableList observableList) { |
166 |
new Label(parent, SWT.NONE).setText(title); |
167 |
final Text targetText = new Text(parent, SWT.BORDER); |
168 |
GridDataFactory.fillDefaults().grab(true, false).applyTo(targetText); |
169 |
targetText.addKeyListener(new KeyAdapter() { |
170 |
public void keyPressed(KeyEvent e) { |
171 |
if (e.keyCode == SWT.CR) { |
172 |
observableList.add(targetText.getText()); |
173 |
targetText.selectAll(); |
174 |
} |
175 |
} |
176 |
}); |
177 |
new Label(parent, SWT.NONE); |
178 |
final ListViewer targetList = new ListViewer(parent, SWT.BORDER |
179 |
| SWT.MULTI | SWT.V_SCROLL); |
180 |
GridDataFactory.fillDefaults().minSize(SWT.DEFAULT, 150).grab(true, |
181 |
true).applyTo(targetList.getList()); |
182 |
targetList.setContentProvider(new ObservableListContentProvider()); |
183 |
targetList.setLabelProvider(new LabelProvider()); |
184 |
targetList.setInput(observableList); |
185 |
targetList.getList().addKeyListener(new KeyAdapter() { |
186 |
public void keyPressed(KeyEvent e) { |
187 |
if (e.keyCode == SWT.DEL) { |
188 |
IStructuredSelection selection = (IStructuredSelection) targetList |
189 |
.getSelection(); |
190 |
observableList.removeAll(selection.toList()); |
191 |
} |
192 |
} |
193 |
}); |
194 |
} |
195 |
|
196 |
private void createAutomaticUpdatesGroup(Composite parent) { |
197 |
Group group = new Group(parent, SWT.NONE); |
198 |
group.setText("Automatic Updates"); |
199 |
group.setLayout(GridLayoutFactory.fillDefaults().numColumns(3) |
200 |
.equalWidth(true).margins(5, 5).create()); |
201 |
GridDataFactory.fillDefaults().grab(true, false).applyTo(group); |
202 |
|
203 |
Button targetButton = new Button(group, SWT.PUSH); |
204 |
targetButton.setText("Target"); |
205 |
GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, |
206 |
false).applyTo(targetButton); |
207 |
targetButton.addSelectionListener(new SelectionAdapter() { |
208 |
public void widgetSelected(SelectionEvent e) { |
209 |
valueModel.setValue(""); |
210 |
listTarget.clear(); |
211 |
for (int i = 0; i < 10; i++) { |
212 |
String value = String.valueOf(i); |
213 |
valueTarget.setValue(value); |
214 |
listTarget.add(value); |
215 |
|
216 |
Display.getCurrent().update(); |
217 |
sleep(100); |
218 |
} |
219 |
} |
220 |
}); |
221 |
|
222 |
Button modelButton = new Button(group, SWT.PUSH); |
223 |
modelButton.setText("Model"); |
224 |
GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, |
225 |
false).applyTo(modelButton); |
226 |
modelButton.addSelectionListener(new SelectionAdapter() { |
227 |
public void widgetSelected(SelectionEvent e) { |
228 |
valueTarget.setValue(""); |
229 |
listModel.clear(); |
230 |
for (int i = 0; i < 10; i++) { |
231 |
String value = String.valueOf(i); |
232 |
valueModel.setValue(value); |
233 |
listModel.add(value); |
234 |
|
235 |
Display.getCurrent().update(); |
236 |
sleep(100); |
237 |
} |
238 |
} |
239 |
}); |
240 |
|
241 |
Button twowayButton = new Button(group, SWT.PUSH); |
242 |
twowayButton.setText("Twoway"); |
243 |
GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, |
244 |
false).applyTo(twowayButton); |
245 |
twowayButton.addSelectionListener(new SelectionAdapter() { |
246 |
public void widgetSelected(SelectionEvent e) { |
247 |
valueTarget.setValue(""); |
248 |
valueModel.setValue(""); |
249 |
listTarget.clear(); |
250 |
listModel.clear(); |
251 |
for (int i = 0; i < 10; i++) { |
252 |
String value = String.valueOf(i); |
253 |
if (i % 2 == 0) { |
254 |
valueTarget.setValue(value); |
255 |
listTarget.add(value); |
256 |
} else { |
257 |
valueModel.setValue(value); |
258 |
listModel.add(value); |
259 |
} |
260 |
|
261 |
Display.getCurrent().update(); |
262 |
sleep(100); |
263 |
} |
264 |
} |
265 |
}); |
266 |
} |
267 |
|
268 |
private IConverter asyncConverter() { |
269 |
return new Converter(null, null) { |
270 |
private final Random random = new Random(System.currentTimeMillis()); |
271 |
|
272 |
public Object convert(Object fromObject) { |
273 |
if (randomizeUpdateDelay) { |
274 |
sleep(random.nextInt(updateDelay)); |
275 |
} else { |
276 |
sleep(updateDelay); |
277 |
} |
278 |
|
279 |
return fromObject; |
280 |
} |
281 |
|
282 |
public boolean isAsync() { |
283 |
return true; |
284 |
} |
285 |
}; |
286 |
} |
287 |
|
288 |
private static void sleep(long millis) { |
289 |
try { |
290 |
Thread.sleep(millis); |
291 |
} catch (InterruptedException e1) { |
292 |
// go ahead |
293 |
} |
294 |
} |
295 |
|
296 |
public static void main(String[] args) { |
297 |
Display display = new Display(); |
298 |
|
299 |
Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() { |
300 |
public void run() { |
301 |
Display display = Display.getCurrent(); |
302 |
final Shell shell = new Shell(display); |
303 |
shell.setText("Asynchronous Bindings"); |
304 |
new Snippet022AsyncUpdate().createControl(shell); |
305 |
|
306 |
shell.setMinimumSize(shell.computeSize(350, SWT.DEFAULT)); |
307 |
shell.pack(); |
308 |
shell.open(); |
309 |
|
310 |
// The SWT event loop |
311 |
while (!shell.isDisposed()) { |
312 |
if (!display.readAndDispatch()) { |
313 |
display.sleep(); |
314 |
} |
315 |
} |
316 |
display.dispose(); |
317 |
} |
318 |
}); |
319 |
} |
320 |
} |