Mercurial > projects > dwt2
comparison org.eclipse.jface.databinding/src/org/eclipse/jface/databinding/swt/SWTObservables.d @ 78:0a55d2d5a946
Added file for databinding
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Tue, 14 Apr 2009 11:35:29 +0200 |
parents | |
children | 6be48cf9f95c |
comparison
equal
deleted
inserted
replaced
76:f05e6e8b2f2d | 78:0a55d2d5a946 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2005, 2008 IBM Corporation 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 * IBM Corporation - initial API and implementation | |
10 * Matt Carter - bug 170668 | |
11 * Brad Reynolds - bug 170848 | |
12 * Matthew Hall - bug 180746, bug 207844 | |
13 * Michael Krauter, bug 180223 | |
14 *******************************************************************************/ | |
15 module org.eclipse.jface.databinding.swt.SWTObservables; | |
16 | |
17 import java.lang.all; | |
18 | |
19 import java.util.ArrayList; | |
20 import java.util.Iterator; | |
21 | |
22 import org.eclipse.core.databinding.observable.Realm; | |
23 import org.eclipse.core.databinding.observable.list.IObservableList; | |
24 import org.eclipse.jface.internal.databinding.internal.swt.LinkObservableValue; | |
25 import org.eclipse.jface.internal.databinding.swt.ButtonObservableValue; | |
26 import org.eclipse.jface.internal.databinding.swt.CComboObservableList; | |
27 import org.eclipse.jface.internal.databinding.swt.CComboObservableValue; | |
28 import org.eclipse.jface.internal.databinding.swt.CComboSingleSelectionObservableValue; | |
29 import org.eclipse.jface.internal.databinding.swt.CLabelObservableValue; | |
30 import org.eclipse.jface.internal.databinding.swt.ComboObservableList; | |
31 import org.eclipse.jface.internal.databinding.swt.ComboObservableValue; | |
32 import org.eclipse.jface.internal.databinding.swt.ComboSingleSelectionObservableValue; | |
33 import org.eclipse.jface.internal.databinding.swt.ControlObservableValue; | |
34 import org.eclipse.jface.internal.databinding.swt.DelayedObservableValue; | |
35 import org.eclipse.jface.internal.databinding.swt.LabelObservableValue; | |
36 import org.eclipse.jface.internal.databinding.swt.ListObservableList; | |
37 import org.eclipse.jface.internal.databinding.swt.ListObservableValue; | |
38 import org.eclipse.jface.internal.databinding.swt.ListSingleSelectionObservableValue; | |
39 import org.eclipse.jface.internal.databinding.swt.SWTProperties; | |
40 import org.eclipse.jface.internal.databinding.swt.ScaleObservableValue; | |
41 import org.eclipse.jface.internal.databinding.swt.ShellObservableValue; | |
42 import org.eclipse.jface.internal.databinding.swt.SpinnerObservableValue; | |
43 import org.eclipse.jface.internal.databinding.swt.TableSingleSelectionObservableValue; | |
44 import org.eclipse.jface.internal.databinding.swt.TextEditableObservableValue; | |
45 import org.eclipse.jface.internal.databinding.swt.TextObservableValue; | |
46 import org.eclipse.swt.custom.CCombo; | |
47 import org.eclipse.swt.custom.CLabel; | |
48 import org.eclipse.swt.widgets.Button; | |
49 import org.eclipse.swt.widgets.Combo; | |
50 import org.eclipse.swt.widgets.Control; | |
51 import org.eclipse.swt.widgets.Display; | |
52 import org.eclipse.swt.widgets.Label; | |
53 import org.eclipse.swt.widgets.Link; | |
54 import org.eclipse.swt.widgets.List; | |
55 import org.eclipse.swt.widgets.Scale; | |
56 import org.eclipse.swt.widgets.Shell; | |
57 import org.eclipse.swt.widgets.Spinner; | |
58 import org.eclipse.swt.widgets.Table; | |
59 import org.eclipse.swt.widgets.Text; | |
60 | |
61 /** | |
62 * A factory for creating observables for SWT widgets | |
63 * | |
64 * @since 1.1 | |
65 * | |
66 */ | |
67 public class SWTObservables { | |
68 | |
69 private static java.util.List realms = new ArrayList(); | |
70 | |
71 /** | |
72 * Returns the realm representing the UI thread for the given display. | |
73 * | |
74 * @param display | |
75 * @return the realm representing the UI thread for the given display | |
76 */ | |
77 public static Realm getRealm(Display display) { | |
78 synchronized (realms) { | |
79 for (Iterator it = realms.iterator(); it.hasNext();) { | |
80 DisplayRealm displayRealm = cast(DisplayRealm) it.next(); | |
81 if (displayRealm.display is display) { | |
82 return displayRealm; | |
83 } | |
84 } | |
85 DisplayRealm result = new DisplayRealm(display); | |
86 realms.add(result); | |
87 return result; | |
88 } | |
89 } | |
90 | |
91 /** | |
92 * Returns an observable which delays notification of value change events | |
93 * from <code>observable</code> until <code>delay</code> milliseconds | |
94 * have passed since the last change event, or until a FocusOut event is | |
95 * received from the underlying widget (whichever happens earlier). This | |
96 * class helps to delay validation until the user stops typing. To notify | |
97 * about pending changes, the returned observable value will fire a stale | |
98 * event when the wrapped observable value fires a change event, but this | |
99 * change is being delayed. | |
100 * | |
101 * @param delay | |
102 * @param observable | |
103 * @return an observable which delays notification of value change events | |
104 * from <code>observable</code> until <code>delay</code> | |
105 * milliseconds have passed since the last change event. | |
106 * | |
107 * @since 1.2 | |
108 */ | |
109 public static ISWTObservableValue observeDelayedValue(int delay, ISWTObservableValue observable) { | |
110 return new DelayedObservableValue(delay, observable); | |
111 } | |
112 | |
113 /** | |
114 * @param control | |
115 * @return an observable value tracking the enabled state of the given | |
116 * control | |
117 */ | |
118 public static ISWTObservableValue observeEnabled(Control control) { | |
119 return new ControlObservableValue(control, SWTProperties.ENABLED); | |
120 } | |
121 | |
122 /** | |
123 * @param control | |
124 * @return an observable value tracking the visible state of the given | |
125 * control | |
126 */ | |
127 public static ISWTObservableValue observeVisible(Control control) { | |
128 return new ControlObservableValue(control, SWTProperties.VISIBLE); | |
129 } | |
130 | |
131 /** | |
132 * @param control | |
133 * @return an observable value tracking the tooltip text of the given | |
134 * control | |
135 */ | |
136 public static ISWTObservableValue observeTooltipText(Control control) { | |
137 return new ControlObservableValue(control, SWTProperties.TOOLTIP_TEXT); | |
138 } | |
139 | |
140 /** | |
141 * Returns an observable observing the selection attribute of the provided | |
142 * <code>control</code>. The supported types are: | |
143 * <ul> | |
144 * <li>org.eclipse.swt.widgets.Spinner</li> | |
145 * <li>org.eclipse.swt.widgets.Button</li> | |
146 * <li>org.eclipse.swt.widgets.Combo</li> | |
147 * <li>org.eclipse.swt.custom.CCombo</li> | |
148 * <li>org.eclipse.swt.widgets.List</li> | |
149 * <li>org.eclipse.swt.widgets.Scale</li> | |
150 * </ul> | |
151 * | |
152 * @param control | |
153 * @return observable value | |
154 * @throws IllegalArgumentException | |
155 * if <code>control</code> type is unsupported | |
156 */ | |
157 public static ISWTObservableValue observeSelection(Control control) { | |
158 if (null !is cast(Spinner)control) { | |
159 return new SpinnerObservableValue(cast(Spinner) control, | |
160 SWTProperties.SELECTION); | |
161 } else if (null !is cast(Button)control) { | |
162 return new ButtonObservableValue(cast(Button) control); | |
163 } else if (null !is cast(Combo)control) { | |
164 return new ComboObservableValue(cast(Combo) control, | |
165 SWTProperties.SELECTION); | |
166 } else if (null !is cast(CCombo)control) { | |
167 return new CComboObservableValue(cast(CCombo) control, | |
168 SWTProperties.SELECTION); | |
169 } else if (null !is cast(List)control) { | |
170 return new ListObservableValue(cast(List) control); | |
171 } else if (null !is cast(Scale)control) { | |
172 return new ScaleObservableValue(cast(Scale) control, | |
173 SWTProperties.SELECTION); | |
174 } | |
175 | |
176 throw new IllegalArgumentException( | |
177 "Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$ | |
178 } | |
179 | |
180 /** | |
181 * Returns an observable observing the minimum attribute of the provided | |
182 * <code>control</code>. The supported types are: | |
183 * <ul> | |
184 * <li>org.eclipse.swt.widgets.Spinner</li> | |
185 * <li>org.eclipse.swt.widgets.Scale</li> | |
186 * </ul> | |
187 * | |
188 * @param control | |
189 * @return observable value | |
190 * @throws IllegalArgumentException | |
191 * if <code>control</code> type is unsupported | |
192 */ | |
193 public static ISWTObservableValue observeMin(Control control) { | |
194 if (null !is cast(Spinner)control) { | |
195 return new SpinnerObservableValue(cast(Spinner) control, | |
196 SWTProperties.MIN); | |
197 } else if (null !is cast(Scale)control) { | |
198 return new ScaleObservableValue(cast(Scale) control, SWTProperties.MIN); | |
199 } | |
200 | |
201 throw new IllegalArgumentException( | |
202 "Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$ | |
203 } | |
204 | |
205 /** | |
206 * Returns an observable observing the maximum attribute of the provided | |
207 * <code>control</code>. The supported types are: | |
208 * <ul> | |
209 * <li>org.eclipse.swt.widgets.Spinner</li> | |
210 * <li>org.eclipse.swt.widgets.Scale</li> | |
211 * </ul> | |
212 * | |
213 * @param control | |
214 * @return observable value | |
215 * @throws IllegalArgumentException | |
216 * if <code>control</code> type is unsupported | |
217 */ | |
218 public static ISWTObservableValue observeMax(Control control) { | |
219 if (null !is cast(Spinner)control) { | |
220 return new SpinnerObservableValue(cast(Spinner) control, | |
221 SWTProperties.MAX); | |
222 } else if (null !is cast(Scale)control) { | |
223 return new ScaleObservableValue(cast(Scale) control, SWTProperties.MAX); | |
224 } | |
225 | |
226 throw new IllegalArgumentException( | |
227 "Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$ | |
228 } | |
229 | |
230 /** | |
231 * Returns an observable observing the text attribute of the provided | |
232 * <code>control</code>. The supported types are: | |
233 * <ul> | |
234 * <li>org.eclipse.swt.widgets.Text</li> | |
235 * </ul> | |
236 * | |
237 * <li>org.eclipse.swt.widgets.Label</li> | |
238 * @param control | |
239 * @param event event type to register for change events | |
240 * @return observable value | |
241 * @throws IllegalArgumentException | |
242 * if <code>control</code> type is unsupported | |
243 */ | |
244 public static ISWTObservableValue observeText(Control control, int event) { | |
245 if (null !is cast(Text)control) { | |
246 return new TextObservableValue(cast(Text) control, event); | |
247 } | |
248 | |
249 throw new IllegalArgumentException( | |
250 "Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$ | |
251 } | |
252 | |
253 /** | |
254 * Returns an observable observing the text attribute of the provided | |
255 * <code>control</code>. The supported types are: | |
256 * <ul> | |
257 * <li>org.eclipse.swt.widgets.Label</li> | |
258 * <li>org.eclipse.swt.widgets.Link (as of 1.2)</li> | |
259 * <li>org.eclipse.swt.custom.Label</li> | |
260 * <li>org.eclipse.swt.widgets.Combo</li> | |
261 * <li>org.eclipse.swt.custom.CCombo</li> | |
262 * <li>org.eclipse.swt.widgets.Shell</li> | |
263 * </ul> | |
264 * | |
265 * @param control | |
266 * @return observable value | |
267 * @throws IllegalArgumentException | |
268 * if <code>control</code> type is unsupported | |
269 */ | |
270 public static ISWTObservableValue observeText(Control control) { | |
271 if (null !is cast(Label)control) { | |
272 return new LabelObservableValue(cast(Label) control); | |
273 } else if (null !is cast(Link)control) { | |
274 return new LinkObservableValue(cast(Link) control); | |
275 } else if (null !is cast(CLabel)control) { | |
276 return new CLabelObservableValue(cast(CLabel) control); | |
277 } else if (null !is cast(Combo)control) { | |
278 return new ComboObservableValue(cast(Combo) control, SWTProperties.TEXT); | |
279 } else if (null !is cast(CCombo)control) { | |
280 return new CComboObservableValue(cast(CCombo) control, | |
281 SWTProperties.TEXT); | |
282 } else if (null !is cast(Shell)control) { | |
283 return new ShellObservableValue(cast(Shell) control); | |
284 } | |
285 | |
286 throw new IllegalArgumentException( | |
287 "Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$ | |
288 } | |
289 | |
290 /** | |
291 * Returns an observable observing the items attribute of the provided | |
292 * <code>control</code>. The supported types are: | |
293 * <ul> | |
294 * <li>org.eclipse.swt.widgets.Combo</li> | |
295 * <li>org.eclipse.swt.custom.CCombo</li> | |
296 * <li>org.eclipse.swt.widgets.List</li> | |
297 * </ul> | |
298 * | |
299 * @param control | |
300 * @return observable list | |
301 * @throws IllegalArgumentException | |
302 * if <code>control</code> type is unsupported | |
303 */ | |
304 public static IObservableList observeItems(Control control) { | |
305 if (null !is cast(Combo)control) { | |
306 return new ComboObservableList(cast(Combo) control); | |
307 } else if (null !is cast(CCombo)control) { | |
308 return new CComboObservableList(cast(CCombo) control); | |
309 } else if (null !is cast(List)control) { | |
310 return new ListObservableList(cast(List) control); | |
311 } | |
312 | |
313 throw new IllegalArgumentException( | |
314 "Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$ | |
315 } | |
316 | |
317 /** | |
318 * Returns an observable observing the single selection index attribute of | |
319 * the provided <code>control</code>. The supported types are: | |
320 * <ul> | |
321 * <li>org.eclipse.swt.widgets.Table</li> | |
322 * <li>org.eclipse.swt.widgets.Combo</li> | |
323 * <li>org.eclipse.swt.custom.CCombo</li> | |
324 * <li>org.eclipse.swt.widgets.List</li> | |
325 * </ul> | |
326 * | |
327 * @param control | |
328 * @return observable value | |
329 * @throws IllegalArgumentException | |
330 * if <code>control</code> type is unsupported | |
331 */ | |
332 public static ISWTObservableValue observeSingleSelectionIndex( | |
333 Control control) { | |
334 if (null !is cast(Table)control) { | |
335 return new TableSingleSelectionObservableValue(cast(Table) control); | |
336 } else if (null !is cast(Combo)control) { | |
337 return new ComboSingleSelectionObservableValue(cast(Combo) control); | |
338 } else if (null !is cast(CCombo)control) { | |
339 return new CComboSingleSelectionObservableValue(cast(CCombo) control); | |
340 } else if (null !is cast(List)control) { | |
341 return new ListSingleSelectionObservableValue(cast(List) control); | |
342 } | |
343 | |
344 throw new IllegalArgumentException( | |
345 "Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$ | |
346 } | |
347 | |
348 /** | |
349 * @param control | |
350 * @return an observable value tracking the foreground color of the given | |
351 * control | |
352 */ | |
353 public static ISWTObservableValue observeForeground(Control control) { | |
354 return new ControlObservableValue(control, SWTProperties.FOREGROUND); | |
355 } | |
356 | |
357 /** | |
358 * @param control | |
359 * @return an observable value tracking the background color of the given | |
360 * control | |
361 */ | |
362 public static ISWTObservableValue observeBackground(Control control) { | |
363 return new ControlObservableValue(control, SWTProperties.BACKGROUND); | |
364 } | |
365 | |
366 /** | |
367 * @param control | |
368 * @return an observable value tracking the font of the given control | |
369 */ | |
370 public static ISWTObservableValue observeFont(Control control) { | |
371 return new ControlObservableValue(control, SWTProperties.FONT); | |
372 } | |
373 | |
374 /** | |
375 * Returns an observable observing the editable attribute of | |
376 * the provided <code>control</code>. The supported types are: | |
377 * <ul> | |
378 * <li>org.eclipse.swt.widgets.Text</li> | |
379 * </ul> | |
380 * | |
381 * @param control | |
382 * @return observable value | |
383 * @throws IllegalArgumentException | |
384 * if <code>control</code> type is unsupported | |
385 */ | |
386 public static ISWTObservableValue observeEditable(Control control) { | |
387 if (null !is cast(Text)control) { | |
388 return new TextEditableObservableValue(cast(Text) control); | |
389 } | |
390 | |
391 throw new IllegalArgumentException( | |
392 "Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$ | |
393 } | |
394 | |
395 private static class DisplayRealm : Realm { | |
396 private Display display; | |
397 | |
398 /** | |
399 * @param display | |
400 */ | |
401 private this(Display display) { | |
402 this.display = display; | |
403 } | |
404 | |
405 public bool isCurrent() { | |
406 return Display.getCurrent() is display; | |
407 } | |
408 | |
409 public void asyncExec(Runnable runnable) { | |
410 Runnable safeRunnable = dgRunnable((Runnable runnable_) { | |
411 safeRun(runnable_); | |
412 }, runnable); | |
413 if (!display.isDisposed()) { | |
414 display.asyncExec(safeRunnable); | |
415 } | |
416 } | |
417 | |
418 /* | |
419 * (non-Javadoc) | |
420 * | |
421 * @see java.lang.Object#hashCode() | |
422 */ | |
423 public int hashCode() { | |
424 return (display is null) ? 0 : display.hashCode(); | |
425 } | |
426 | |
427 /* | |
428 * (non-Javadoc) | |
429 * | |
430 * @see java.lang.Object#equals(java.lang.Object) | |
431 */ | |
432 public bool equals(Object obj) { | |
433 if (this is obj) | |
434 return true; | |
435 if (obj is null) | |
436 return false; | |
437 if (getClass() !is obj.getClass()) | |
438 return false; | |
439 final DisplayRealm other = cast(DisplayRealm) obj; | |
440 if (display is null) { | |
441 if (other.display !is null) | |
442 return false; | |
443 } else if (!display.equals(other.display)) | |
444 return false; | |
445 return true; | |
446 } | |
447 } | |
448 } |