KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > databinding > swt > SWTObservables


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 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  *******************************************************************************/

13 package org.eclipse.jface.databinding.swt;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.Iterator JavaDoc;
17
18 import org.eclipse.core.databinding.observable.Realm;
19 import org.eclipse.core.databinding.observable.list.IObservableList;
20 import org.eclipse.jface.internal.databinding.internal.swt.ButtonObservableValue;
21 import org.eclipse.jface.internal.databinding.internal.swt.CComboObservableList;
22 import org.eclipse.jface.internal.databinding.internal.swt.CComboObservableValue;
23 import org.eclipse.jface.internal.databinding.internal.swt.CComboSingleSelectionObservableValue;
24 import org.eclipse.jface.internal.databinding.internal.swt.CLabelObservableValue;
25 import org.eclipse.jface.internal.databinding.internal.swt.ComboObservableList;
26 import org.eclipse.jface.internal.databinding.internal.swt.ComboObservableValue;
27 import org.eclipse.jface.internal.databinding.internal.swt.ComboSingleSelectionObservableValue;
28 import org.eclipse.jface.internal.databinding.internal.swt.ControlObservableValue;
29 import org.eclipse.jface.internal.databinding.internal.swt.LabelObservableValue;
30 import org.eclipse.jface.internal.databinding.internal.swt.ListObservableList;
31 import org.eclipse.jface.internal.databinding.internal.swt.ListObservableValue;
32 import org.eclipse.jface.internal.databinding.internal.swt.ListSingleSelectionObservableValue;
33 import org.eclipse.jface.internal.databinding.internal.swt.SWTProperties;
34 import org.eclipse.jface.internal.databinding.internal.swt.ScaleObservableValue;
35 import org.eclipse.jface.internal.databinding.internal.swt.SpinnerObservableValue;
36 import org.eclipse.jface.internal.databinding.internal.swt.TableSingleSelectionObservableValue;
37 import org.eclipse.jface.internal.databinding.internal.swt.TextEditableObservableValue;
38 import org.eclipse.jface.internal.databinding.internal.swt.TextObservableValue;
39 import org.eclipse.swt.custom.CCombo;
40 import org.eclipse.swt.custom.CLabel;
41 import org.eclipse.swt.widgets.Button;
42 import org.eclipse.swt.widgets.Combo;
43 import org.eclipse.swt.widgets.Control;
44 import org.eclipse.swt.widgets.Display;
45 import org.eclipse.swt.widgets.Label;
46 import org.eclipse.swt.widgets.List;
47 import org.eclipse.swt.widgets.Scale;
48 import org.eclipse.swt.widgets.Spinner;
49 import org.eclipse.swt.widgets.Table;
50 import org.eclipse.swt.widgets.Text;
51
52 /**
53  * A factory for creating observables for SWT widgets
54  *
55  * @since 1.1
56  *
57  */

58 public class SWTObservables {
59
60     private static java.util.List JavaDoc realms = new ArrayList JavaDoc();
61
62     /**
63      * Returns the realm representing the UI thread for the given display.
64      *
65      * @param display
66      * @return the realm representing the UI thread for the given display
67      */

68     public static Realm getRealm(final Display display) {
69         synchronized (realms) {
70             for (Iterator JavaDoc it = realms.iterator(); it.hasNext();) {
71                 DisplayRealm displayRealm = (DisplayRealm) it.next();
72                 if (displayRealm.display == display) {
73                     return displayRealm;
74                 }
75             }
76             DisplayRealm result = new DisplayRealm(display);
77             realms.add(result);
78             return result;
79         }
80     }
81
82     /**
83      * @param control
84      * @return an observable value tracking the enabled state of the given
85      * control
86      */

87     public static ISWTObservableValue observeEnabled(Control control) {
88         return new ControlObservableValue(control, SWTProperties.ENABLED);
89     }
90
91     /**
92      * @param control
93      * @return an observable value tracking the visible state of the given
94      * control
95      */

96     public static ISWTObservableValue observeVisible(Control control) {
97         return new ControlObservableValue(control, SWTProperties.VISIBLE);
98     }
99
100     /**
101      * @param control
102      * @return an observable value tracking the tooltip text of the given
103      * control
104      */

105     public static ISWTObservableValue observeTooltipText(Control control) {
106         return new ControlObservableValue(control, SWTProperties.TOOLTIP_TEXT);
107     }
108
109     /**
110      * Returns an observable observing the selection attribute of the provided
111      * <code>control</code>. The supported types are:
112      * <ul>
113      * <li>org.eclipse.swt.widgets.Spinner</li>
114      * <li>org.eclipse.swt.widgets.Button</li>
115      * <li>org.eclipse.swt.widgets.Combo</li>
116      * <li>org.eclipse.swt.custom.CCombo</li>
117      * <li>org.eclipse.swt.widgets.List</li>
118      * <li>org.eclipse.swt.widgets.Scale</li>
119      * </ul>
120      *
121      * @param control
122      * @return observable value
123      * @throws IllegalArgumentException
124      * if <code>control</code> type is unsupported
125      */

126     public static ISWTObservableValue observeSelection(Control control) {
127         if (control instanceof Spinner) {
128             return new SpinnerObservableValue((Spinner) control,
129                     SWTProperties.SELECTION);
130         } else if (control instanceof Button) {
131             return new ButtonObservableValue((Button) control);
132         } else if (control instanceof Combo) {
133             return new ComboObservableValue((Combo) control,
134                     SWTProperties.SELECTION);
135         } else if (control instanceof CCombo) {
136             return new CComboObservableValue((CCombo) control,
137                     SWTProperties.SELECTION);
138         } else if (control instanceof List JavaDoc) {
139             return new ListObservableValue((List JavaDoc) control);
140         } else if (control instanceof Scale) {
141             return new ScaleObservableValue((Scale) control,
142                     SWTProperties.SELECTION);
143         }
144
145         throw new IllegalArgumentException JavaDoc(
146                 "Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
147
}
148
149     /**
150      * Returns an observable observing the minimum attribute of the provided
151      * <code>control</code>. The supported types are:
152      * <ul>
153      * <li>org.eclipse.swt.widgets.Spinner</li>
154      * <li>org.eclipse.swt.widgets.Scale</li>
155      * </ul>
156      *
157      * @param control
158      * @return observable value
159      * @throws IllegalArgumentException
160      * if <code>control</code> type is unsupported
161      */

162     public static ISWTObservableValue observeMin(Control control) {
163         if (control instanceof Spinner) {
164             return new SpinnerObservableValue((Spinner) control,
165                     SWTProperties.MIN);
166         } else if (control instanceof Scale) {
167             return new ScaleObservableValue((Scale) control, SWTProperties.MIN);
168         }
169
170         throw new IllegalArgumentException JavaDoc(
171                 "Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
172
}
173
174     /**
175      * Returns an observable observing the maximum attribute of the provided
176      * <code>control</code>. The supported types are:
177      * <ul>
178      * <li>org.eclipse.swt.widgets.Spinner</li>
179      * <li>org.eclipse.swt.widgets.Scale</li>
180      * </ul>
181      *
182      * @param control
183      * @return observable value
184      * @throws IllegalArgumentException
185      * if <code>control</code> type is unsupported
186      */

187     public static ISWTObservableValue observeMax(Control control) {
188         if (control instanceof Spinner) {
189             return new SpinnerObservableValue((Spinner) control,
190                     SWTProperties.MAX);
191         } else if (control instanceof Scale) {
192             return new ScaleObservableValue((Scale) control, SWTProperties.MAX);
193         }
194
195         throw new IllegalArgumentException JavaDoc(
196                 "Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
197
}
198
199     /**
200      * Returns an observable observing the text attribute of the provided
201      * <code>control</code>. The supported types are:
202      * <ul>
203      * <li>org.eclipse.swt.widgets.Text</li>
204      * </ul>
205      *
206      * @param control
207      * @param event event type to register for change events
208      * @return observable value
209      * @throws IllegalArgumentException
210      * if <code>control</code> type is unsupported
211      */

212     public static ISWTObservableValue observeText(Control control, int event) {
213         if (control instanceof Text) {
214             return new TextObservableValue((Text) control, event);
215         }
216
217         throw new IllegalArgumentException JavaDoc(
218                 "Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
219
}
220
221     /**
222      * Returns an observable observing the text attribute of the provided
223      * <code>control</code>. The supported types are:
224      * <ul>
225      * <li>org.eclipse.swt.widgets.Label</li>
226      * <li>org.eclipse.swt.custom.Label</li>
227      * <li>org.eclipse.swt.widgets.Combo</li>
228      * <li>org.eclipse.swt.custom.CCombo</li>
229      * </ul>
230      *
231      * @param control
232      * @return observable value
233      * @throws IllegalArgumentException
234      * if <code>control</code> type is unsupported
235      */

236     public static ISWTObservableValue observeText(Control control) {
237         if (control instanceof Label) {
238             return new LabelObservableValue((Label) control);
239         } else if (control instanceof CLabel) {
240             return new CLabelObservableValue((CLabel) control);
241         } else if (control instanceof Combo) {
242             return new ComboObservableValue((Combo) control, SWTProperties.TEXT);
243         } else if (control instanceof CCombo) {
244             return new CComboObservableValue((CCombo) control,
245                     SWTProperties.TEXT);
246         }
247
248         throw new IllegalArgumentException JavaDoc(
249                 "Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
250
}
251
252     /**
253      * Returns an observable observing the items attribute of the provided
254      * <code>control</code>. The supported types are:
255      * <ul>
256      * <li>org.eclipse.swt.widgets.Combo</li>
257      * <li>org.eclipse.swt.custom.CCombo</li>
258      * <li>org.eclipse.swt.widgets.List</li>
259      * </ul>
260      *
261      * @param control
262      * @return observable list
263      * @throws IllegalArgumentException
264      * if <code>control</code> type is unsupported
265      */

266     public static IObservableList observeItems(Control control) {
267         if (control instanceof Combo) {
268             return new ComboObservableList((Combo) control);
269         } else if (control instanceof CCombo) {
270             return new CComboObservableList((CCombo) control);
271         } else if (control instanceof List JavaDoc) {
272             return new ListObservableList((List JavaDoc) control);
273         }
274
275         throw new IllegalArgumentException JavaDoc(
276                 "Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
277
}
278
279     /**
280      * Returns an observable observing the single selection index attribute of
281      * the provided <code>control</code>. The supported types are:
282      * <ul>
283      * <li>org.eclipse.swt.widgets.Table</li>
284      * <li>org.eclipse.swt.widgets.Combo</li>
285      * <li>org.eclipse.swt.custom.CCombo</li>
286      * <li>org.eclipse.swt.widgets.List</li>
287      * </ul>
288      *
289      * @param control
290      * @return observable value
291      * @throws IllegalArgumentException
292      * if <code>control</code> type is unsupported
293      */

294     public static ISWTObservableValue observeSingleSelectionIndex(
295             Control control) {
296         if (control instanceof Table) {
297             return new TableSingleSelectionObservableValue((Table) control);
298         } else if (control instanceof Combo) {
299             return new ComboSingleSelectionObservableValue((Combo) control);
300         } else if (control instanceof CCombo) {
301             return new CComboSingleSelectionObservableValue((CCombo) control);
302         } else if (control instanceof List JavaDoc) {
303             return new ListSingleSelectionObservableValue((List JavaDoc) control);
304         }
305
306         throw new IllegalArgumentException JavaDoc(
307                 "Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
308
}
309
310     /**
311      * @param control
312      * @return an observable value tracking the foreground color of the given
313      * control
314      */

315     public static ISWTObservableValue observeForeground(Control control) {
316         return new ControlObservableValue(control, SWTProperties.FOREGROUND);
317     }
318
319     /**
320      * @param control
321      * @return an observable value tracking the background color of the given
322      * control
323      */

324     public static ISWTObservableValue observeBackground(Control control) {
325         return new ControlObservableValue(control, SWTProperties.BACKGROUND);
326     }
327
328     /**
329      * @param control
330      * @return an observable value tracking the font of the given control
331      */

332     public static ISWTObservableValue observeFont(Control control) {
333         return new ControlObservableValue(control, SWTProperties.FONT);
334     }
335     
336     /**
337      * Returns an observable observing the editable attribute of
338      * the provided <code>control</code>. The supported types are:
339      * <ul>
340      * <li>org.eclipse.swt.widgets.Text</li>
341      * </ul>
342      *
343      * @param control
344      * @return observable value
345      * @throws IllegalArgumentException
346      * if <code>control</code> type is unsupported
347      */

348     public static ISWTObservableValue observeEditable(Control control) {
349         if (control instanceof Text) {
350             return new TextEditableObservableValue((Text) control);
351         }
352         
353         throw new IllegalArgumentException JavaDoc(
354                 "Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
355
}
356
357     private static class DisplayRealm extends Realm {
358         private Display display;
359
360         /**
361          * @param display
362          */

363         private DisplayRealm(Display display) {
364             this.display = display;
365         }
366
367         public boolean isCurrent() {
368             return Display.getCurrent() == display;
369         }
370
371         public void asyncExec(final Runnable JavaDoc runnable) {
372             Runnable JavaDoc safeRunnable = new Runnable JavaDoc() {
373                 public void run() {
374                     safeRun(runnable);
375                 }
376             };
377             if (!display.isDisposed()) {
378                 display.asyncExec(safeRunnable);
379             }
380         }
381
382         /*
383          * (non-Javadoc)
384          *
385          * @see java.lang.Object#hashCode()
386          */

387         public int hashCode() {
388             return (display == null) ? 0 : display.hashCode();
389         }
390
391         /*
392          * (non-Javadoc)
393          *
394          * @see java.lang.Object#equals(java.lang.Object)
395          */

396         public boolean equals(Object JavaDoc obj) {
397             if (this == obj)
398                 return true;
399             if (obj == null)
400                 return false;
401             if (getClass() != obj.getClass())
402                 return false;
403             final DisplayRealm other = (DisplayRealm) obj;
404             if (display == null) {
405                 if (other.display != null)
406                     return false;
407             } else if (!display.equals(other.display))
408                 return false;
409             return true;
410         }
411     }
412 }
413
Popular Tags