001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: * Matt Carter - bug 170668
011: * Brad Reynolds - bug 170848
012: *******************************************************************************/package org.eclipse.jface.databinding.swt;
013:
014: import java.util.ArrayList;
015: import java.util.Iterator;
016:
017: import org.eclipse.core.databinding.observable.Realm;
018: import org.eclipse.core.databinding.observable.list.IObservableList;
019: import org.eclipse.jface.internal.databinding.internal.swt.ButtonObservableValue;
020: import org.eclipse.jface.internal.databinding.internal.swt.CComboObservableList;
021: import org.eclipse.jface.internal.databinding.internal.swt.CComboObservableValue;
022: import org.eclipse.jface.internal.databinding.internal.swt.CComboSingleSelectionObservableValue;
023: import org.eclipse.jface.internal.databinding.internal.swt.CLabelObservableValue;
024: import org.eclipse.jface.internal.databinding.internal.swt.ComboObservableList;
025: import org.eclipse.jface.internal.databinding.internal.swt.ComboObservableValue;
026: import org.eclipse.jface.internal.databinding.internal.swt.ComboSingleSelectionObservableValue;
027: import org.eclipse.jface.internal.databinding.internal.swt.ControlObservableValue;
028: import org.eclipse.jface.internal.databinding.internal.swt.LabelObservableValue;
029: import org.eclipse.jface.internal.databinding.internal.swt.ListObservableList;
030: import org.eclipse.jface.internal.databinding.internal.swt.ListObservableValue;
031: import org.eclipse.jface.internal.databinding.internal.swt.ListSingleSelectionObservableValue;
032: import org.eclipse.jface.internal.databinding.internal.swt.SWTProperties;
033: import org.eclipse.jface.internal.databinding.internal.swt.ScaleObservableValue;
034: import org.eclipse.jface.internal.databinding.internal.swt.SpinnerObservableValue;
035: import org.eclipse.jface.internal.databinding.internal.swt.TableSingleSelectionObservableValue;
036: import org.eclipse.jface.internal.databinding.internal.swt.TextEditableObservableValue;
037: import org.eclipse.jface.internal.databinding.internal.swt.TextObservableValue;
038: import org.eclipse.swt.custom.CCombo;
039: import org.eclipse.swt.custom.CLabel;
040: import org.eclipse.swt.widgets.Button;
041: import org.eclipse.swt.widgets.Combo;
042: import org.eclipse.swt.widgets.Control;
043: import org.eclipse.swt.widgets.Display;
044: import org.eclipse.swt.widgets.Label;
045: import org.eclipse.swt.widgets.List;
046: import org.eclipse.swt.widgets.Scale;
047: import org.eclipse.swt.widgets.Spinner;
048: import org.eclipse.swt.widgets.Table;
049: import org.eclipse.swt.widgets.Text;
050:
051: /**
052: * A factory for creating observables for SWT widgets
053: *
054: * @since 1.1
055: *
056: */
057: public class SWTObservables {
058:
059: private static java.util.List realms = new ArrayList();
060:
061: /**
062: * Returns the realm representing the UI thread for the given display.
063: *
064: * @param display
065: * @return the realm representing the UI thread for the given display
066: */
067: public static Realm getRealm(final Display display) {
068: synchronized (realms) {
069: for (Iterator it = realms.iterator(); it.hasNext();) {
070: DisplayRealm displayRealm = (DisplayRealm) it.next();
071: if (displayRealm.display == display) {
072: return displayRealm;
073: }
074: }
075: DisplayRealm result = new DisplayRealm(display);
076: realms.add(result);
077: return result;
078: }
079: }
080:
081: /**
082: * @param control
083: * @return an observable value tracking the enabled state of the given
084: * control
085: */
086: public static ISWTObservableValue observeEnabled(Control control) {
087: return new ControlObservableValue(control,
088: SWTProperties.ENABLED);
089: }
090:
091: /**
092: * @param control
093: * @return an observable value tracking the visible state of the given
094: * control
095: */
096: public static ISWTObservableValue observeVisible(Control control) {
097: return new ControlObservableValue(control,
098: SWTProperties.VISIBLE);
099: }
100:
101: /**
102: * @param control
103: * @return an observable value tracking the tooltip text of the given
104: * control
105: */
106: public static ISWTObservableValue observeTooltipText(Control control) {
107: return new ControlObservableValue(control,
108: SWTProperties.TOOLTIP_TEXT);
109: }
110:
111: /**
112: * Returns an observable observing the selection attribute of the provided
113: * <code>control</code>. The supported types are:
114: * <ul>
115: * <li>org.eclipse.swt.widgets.Spinner</li>
116: * <li>org.eclipse.swt.widgets.Button</li>
117: * <li>org.eclipse.swt.widgets.Combo</li>
118: * <li>org.eclipse.swt.custom.CCombo</li>
119: * <li>org.eclipse.swt.widgets.List</li>
120: * <li>org.eclipse.swt.widgets.Scale</li>
121: * </ul>
122: *
123: * @param control
124: * @return observable value
125: * @throws IllegalArgumentException
126: * if <code>control</code> type is unsupported
127: */
128: public static ISWTObservableValue observeSelection(Control control) {
129: if (control instanceof Spinner) {
130: return new SpinnerObservableValue((Spinner) control,
131: SWTProperties.SELECTION);
132: } else if (control instanceof Button) {
133: return new ButtonObservableValue((Button) control);
134: } else if (control instanceof Combo) {
135: return new ComboObservableValue((Combo) control,
136: SWTProperties.SELECTION);
137: } else if (control instanceof CCombo) {
138: return new CComboObservableValue((CCombo) control,
139: SWTProperties.SELECTION);
140: } else if (control instanceof List) {
141: return new ListObservableValue((List) control);
142: } else if (control instanceof Scale) {
143: return new ScaleObservableValue((Scale) control,
144: SWTProperties.SELECTION);
145: }
146:
147: throw new IllegalArgumentException(
148: "Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
149: }
150:
151: /**
152: * Returns an observable observing the minimum attribute of the provided
153: * <code>control</code>. The supported types are:
154: * <ul>
155: * <li>org.eclipse.swt.widgets.Spinner</li>
156: * <li>org.eclipse.swt.widgets.Scale</li>
157: * </ul>
158: *
159: * @param control
160: * @return observable value
161: * @throws IllegalArgumentException
162: * if <code>control</code> type is unsupported
163: */
164: public static ISWTObservableValue observeMin(Control control) {
165: if (control instanceof Spinner) {
166: return new SpinnerObservableValue((Spinner) control,
167: SWTProperties.MIN);
168: } else if (control instanceof Scale) {
169: return new ScaleObservableValue((Scale) control,
170: SWTProperties.MIN);
171: }
172:
173: throw new IllegalArgumentException(
174: "Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
175: }
176:
177: /**
178: * Returns an observable observing the maximum attribute of the provided
179: * <code>control</code>. The supported types are:
180: * <ul>
181: * <li>org.eclipse.swt.widgets.Spinner</li>
182: * <li>org.eclipse.swt.widgets.Scale</li>
183: * </ul>
184: *
185: * @param control
186: * @return observable value
187: * @throws IllegalArgumentException
188: * if <code>control</code> type is unsupported
189: */
190: public static ISWTObservableValue observeMax(Control control) {
191: if (control instanceof Spinner) {
192: return new SpinnerObservableValue((Spinner) control,
193: SWTProperties.MAX);
194: } else if (control instanceof Scale) {
195: return new ScaleObservableValue((Scale) control,
196: SWTProperties.MAX);
197: }
198:
199: throw new IllegalArgumentException(
200: "Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
201: }
202:
203: /**
204: * Returns an observable observing the text attribute of the provided
205: * <code>control</code>. The supported types are:
206: * <ul>
207: * <li>org.eclipse.swt.widgets.Text</li>
208: * </ul>
209: *
210: * @param control
211: * @param event event type to register for change events
212: * @return observable value
213: * @throws IllegalArgumentException
214: * if <code>control</code> type is unsupported
215: */
216: public static ISWTObservableValue observeText(Control control,
217: int event) {
218: if (control instanceof Text) {
219: return new TextObservableValue((Text) control, event);
220: }
221:
222: throw new IllegalArgumentException(
223: "Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
224: }
225:
226: /**
227: * Returns an observable observing the text attribute of the provided
228: * <code>control</code>. The supported types are:
229: * <ul>
230: * <li>org.eclipse.swt.widgets.Label</li>
231: * <li>org.eclipse.swt.custom.Label</li>
232: * <li>org.eclipse.swt.widgets.Combo</li>
233: * <li>org.eclipse.swt.custom.CCombo</li>
234: * </ul>
235: *
236: * @param control
237: * @return observable value
238: * @throws IllegalArgumentException
239: * if <code>control</code> type is unsupported
240: */
241: public static ISWTObservableValue observeText(Control control) {
242: if (control instanceof Label) {
243: return new LabelObservableValue((Label) control);
244: } else if (control instanceof CLabel) {
245: return new CLabelObservableValue((CLabel) control);
246: } else if (control instanceof Combo) {
247: return new ComboObservableValue((Combo) control,
248: SWTProperties.TEXT);
249: } else if (control instanceof CCombo) {
250: return new CComboObservableValue((CCombo) control,
251: SWTProperties.TEXT);
252: }
253:
254: throw new IllegalArgumentException(
255: "Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
256: }
257:
258: /**
259: * Returns an observable observing the items attribute of the provided
260: * <code>control</code>. The supported types are:
261: * <ul>
262: * <li>org.eclipse.swt.widgets.Combo</li>
263: * <li>org.eclipse.swt.custom.CCombo</li>
264: * <li>org.eclipse.swt.widgets.List</li>
265: * </ul>
266: *
267: * @param control
268: * @return observable list
269: * @throws IllegalArgumentException
270: * if <code>control</code> type is unsupported
271: */
272: public static IObservableList observeItems(Control control) {
273: if (control instanceof Combo) {
274: return new ComboObservableList((Combo) control);
275: } else if (control instanceof CCombo) {
276: return new CComboObservableList((CCombo) control);
277: } else if (control instanceof List) {
278: return new ListObservableList((List) control);
279: }
280:
281: throw new IllegalArgumentException(
282: "Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
283: }
284:
285: /**
286: * Returns an observable observing the single selection index attribute of
287: * the provided <code>control</code>. The supported types are:
288: * <ul>
289: * <li>org.eclipse.swt.widgets.Table</li>
290: * <li>org.eclipse.swt.widgets.Combo</li>
291: * <li>org.eclipse.swt.custom.CCombo</li>
292: * <li>org.eclipse.swt.widgets.List</li>
293: * </ul>
294: *
295: * @param control
296: * @return observable value
297: * @throws IllegalArgumentException
298: * if <code>control</code> type is unsupported
299: */
300: public static ISWTObservableValue observeSingleSelectionIndex(
301: Control control) {
302: if (control instanceof Table) {
303: return new TableSingleSelectionObservableValue(
304: (Table) control);
305: } else if (control instanceof Combo) {
306: return new ComboSingleSelectionObservableValue(
307: (Combo) control);
308: } else if (control instanceof CCombo) {
309: return new CComboSingleSelectionObservableValue(
310: (CCombo) control);
311: } else if (control instanceof List) {
312: return new ListSingleSelectionObservableValue(
313: (List) control);
314: }
315:
316: throw new IllegalArgumentException(
317: "Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
318: }
319:
320: /**
321: * @param control
322: * @return an observable value tracking the foreground color of the given
323: * control
324: */
325: public static ISWTObservableValue observeForeground(Control control) {
326: return new ControlObservableValue(control,
327: SWTProperties.FOREGROUND);
328: }
329:
330: /**
331: * @param control
332: * @return an observable value tracking the background color of the given
333: * control
334: */
335: public static ISWTObservableValue observeBackground(Control control) {
336: return new ControlObservableValue(control,
337: SWTProperties.BACKGROUND);
338: }
339:
340: /**
341: * @param control
342: * @return an observable value tracking the font of the given control
343: */
344: public static ISWTObservableValue observeFont(Control control) {
345: return new ControlObservableValue(control, SWTProperties.FONT);
346: }
347:
348: /**
349: * Returns an observable observing the editable attribute of
350: * the provided <code>control</code>. The supported types are:
351: * <ul>
352: * <li>org.eclipse.swt.widgets.Text</li>
353: * </ul>
354: *
355: * @param control
356: * @return observable value
357: * @throws IllegalArgumentException
358: * if <code>control</code> type is unsupported
359: */
360: public static ISWTObservableValue observeEditable(Control control) {
361: if (control instanceof Text) {
362: return new TextEditableObservableValue((Text) control);
363: }
364:
365: throw new IllegalArgumentException(
366: "Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
367: }
368:
369: private static class DisplayRealm extends Realm {
370: private Display display;
371:
372: /**
373: * @param display
374: */
375: private DisplayRealm(Display display) {
376: this .display = display;
377: }
378:
379: public boolean isCurrent() {
380: return Display.getCurrent() == display;
381: }
382:
383: public void asyncExec(final Runnable runnable) {
384: Runnable safeRunnable = new Runnable() {
385: public void run() {
386: safeRun(runnable);
387: }
388: };
389: if (!display.isDisposed()) {
390: display.asyncExec(safeRunnable);
391: }
392: }
393:
394: /*
395: * (non-Javadoc)
396: *
397: * @see java.lang.Object#hashCode()
398: */
399: public int hashCode() {
400: return (display == null) ? 0 : display.hashCode();
401: }
402:
403: /*
404: * (non-Javadoc)
405: *
406: * @see java.lang.Object#equals(java.lang.Object)
407: */
408: public boolean equals(Object obj) {
409: if (this == obj)
410: return true;
411: if (obj == null)
412: return false;
413: if (getClass() != obj.getClass())
414: return false;
415: final DisplayRealm other = (DisplayRealm) obj;
416: if (display == null) {
417: if (other.display != null)
418: return false;
419: } else if (!display.equals(other.display))
420: return false;
421: return true;
422: }
423: }
424: }
|