KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > databinding > provisional > swt > SWTObservableFactory


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

11 package org.eclipse.jface.internal.databinding.provisional.swt;
12
13 import org.eclipse.jface.internal.databinding.internal.swt.ButtonObservableValue;
14 import org.eclipse.jface.internal.databinding.internal.swt.CComboObservableList;
15 import org.eclipse.jface.internal.databinding.internal.swt.CComboObservableValue;
16 import org.eclipse.jface.internal.databinding.internal.swt.ComboObservableList;
17 import org.eclipse.jface.internal.databinding.internal.swt.ComboObservableValue;
18 import org.eclipse.jface.internal.databinding.internal.swt.ControlObservableValue;
19 import org.eclipse.jface.internal.databinding.internal.swt.LabelObservableValue;
20 import org.eclipse.jface.internal.databinding.internal.swt.ListObservableList;
21 import org.eclipse.jface.internal.databinding.internal.swt.ListObservableValue;
22 import org.eclipse.jface.internal.databinding.internal.swt.SpinnerObservableValue;
23 import org.eclipse.jface.internal.databinding.internal.swt.TableObservableValue;
24 import org.eclipse.jface.internal.databinding.internal.swt.TextObservableValue;
25 import org.eclipse.jface.internal.databinding.provisional.DataBindingContext;
26 import org.eclipse.jface.internal.databinding.provisional.description.Property;
27 import org.eclipse.jface.internal.databinding.provisional.factories.IObservableFactory;
28 import org.eclipse.jface.internal.databinding.provisional.observable.IObservable;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.custom.CCombo;
31 import org.eclipse.swt.widgets.Button;
32 import org.eclipse.swt.widgets.Combo;
33 import org.eclipse.swt.widgets.Control;
34 import org.eclipse.swt.widgets.Label;
35 import org.eclipse.swt.widgets.List;
36 import org.eclipse.swt.widgets.Spinner;
37 import org.eclipse.swt.widgets.Table;
38 import org.eclipse.swt.widgets.Text;
39
40 /**
41  * A factory that supports binding to SWT controls. This factory supports the
42  * following description objects:
43  * <ul>
44  * <li>org.eclipse.swt.widgets.Text - denotes the text's text property</li>
45  * <li>org.eclipse.swt.widgets.Button - denotes the button's selection property</li>
46  * <li>org.eclipse.swt.widgets.Combo - denotes the combo's items collection</li>
47  * <li>org.eclipse.swt.widgets.CCombo - denotes the ccombo's items collection</li>
48  * <li>org.eclipse.swt.widgets.List - denotes the list's items collection</li>
49  * <li>org.eclipse.jface.databinding.PropertyDescription - depending on the
50  * property description's object and property ID:
51  * <ul>
52  * <li>object instanceof Widget, property ID is SWT_ENABLED - denoting the
53  * widget's enabled property</li>
54  * <li>object instanceof Spinner, property ID is SWT_SELECTION - denoting the
55  * spinner's selection property</li>
56  * <li>object instanceof Spinner, property ID is SWT_MINIMUM - denoting the
57  * spinner's minimum property</li>
58  * <li>object instanceof Spinner, property ID is SWT_MAXIMUM - denoting the
59  * spinner's maximum property</li>
60  * </ul>
61  * </li>
62  * </ul>
63  * TODO complete the list
64  *
65  * @since 1.0
66  *
67  */

68 final public class SWTObservableFactory implements IObservableFactory {
69
70     private int updateTime = DataBindingContext.TIME_LATE;
71
72     /**
73      * Create a factory that can create observables for SWT controls
74      */

75     public SWTObservableFactory() {
76     }
77
78     /**
79      * @param updateTime
80      * The update policy of IDataBineingContext.TIME_LATE or
81      * DataBindingContext.TIME_EARLY is a hint that some editable
82      * controls may implement (such as Text) to determine when to
83      * fire updates
84      */

85     public SWTObservableFactory(int updateTime) {
86         this.updateTime = updateTime;
87     }
88
89     public IObservable createObservable(Object JavaDoc description) {
90         if (description instanceof Property) {
91             Object JavaDoc object = ((Property) description).getObject();
92             Object JavaDoc attribute = ((Property) description).getPropertyID();
93             if (object instanceof Control
94                     && SWTProperties.ENABLED.equals(attribute)) {
95                 return new ControlObservableValue((Control) object,
96                         (String JavaDoc) attribute);
97             }
98             if (object instanceof Control
99                     && SWTProperties.VISIBLE.equals(attribute)) {
100                 return new ControlObservableValue((Control) object,
101                         (String JavaDoc) attribute);
102             }
103             if (object instanceof Spinner
104                     && (SWTProperties.SELECTION.equals(attribute)
105                             || SWTProperties.MIN.equals(attribute) || SWTProperties.MAX
106                             .equals(attribute))) {
107                 return new SpinnerObservableValue((Spinner) object,
108                         (String JavaDoc) attribute);
109             }
110             if (object instanceof Text && SWTProperties.TEXT.equals(attribute)) {
111                 return new TextObservableValue((Text) object, SWT.Modify);
112             }
113             if (object instanceof Label && SWTProperties.TEXT.equals(attribute)) {
114                 return new LabelObservableValue((Label) object);
115             }
116             if (object instanceof Button
117                     && SWTProperties.SELECTION.equals(attribute)) {
118                 return new ButtonObservableValue((Button) object);
119             }
120             if (object instanceof Combo
121                     && (SWTProperties.TEXT.equals(attribute) || SWTProperties.SELECTION
122                             .equals(attribute))) {
123                 return new ComboObservableValue((Combo) object,
124                         (String JavaDoc) attribute);
125             } else if (object instanceof Combo
126                     && SWTProperties.ITEMS.equals(attribute)) {
127                 return new ComboObservableList((Combo) object);
128             }
129             if (object instanceof CCombo
130                     && (SWTProperties.TEXT.equals(attribute) || SWTProperties.SELECTION
131                             .equals(attribute))) {
132                 return new CComboObservableValue((CCombo) object,
133                         (String JavaDoc) attribute);
134             } else if (object instanceof CCombo
135                     && SWTProperties.ITEMS.equals(attribute)) {
136                 return new CComboObservableList((CCombo) object);
137             }
138             if (object instanceof List
139                     && SWTProperties.SELECTION.equals(attribute)) {
140                 // SWT.SINGLE selection only
141
return new ListObservableValue((List) object);
142             } else if (object instanceof List
143                     && SWTProperties.ITEMS.equals(attribute)) {
144                 return new ListObservableList((List) object);
145             }
146             if (object instanceof Table) {
147                 return new TableObservableValue((Table) object,
148                         (String JavaDoc) attribute);
149             }
150         }
151         if (description instanceof Text) {
152             int updatePolicy = new int[] { SWT.Modify, SWT.FocusOut, SWT.None }[updateTime];
153             return new TextObservableValue((Text) description, updatePolicy);
154         } else if (description instanceof Button) {
155             // int updatePolicy = new int[] { SWT.Modify, SWT.FocusOut, SWT.None }[updateTime];
156
return new ButtonObservableValue((Button) description);
157         } else if (description instanceof Label) {
158             return new LabelObservableValue((Label) description);
159         } else if (description instanceof Combo) {
160             return new ComboObservableList((Combo) description);
161         } else if (description instanceof Spinner) {
162             return new SpinnerObservableValue((Spinner) description,
163                     SWTProperties.SELECTION);
164         } else if (description instanceof CCombo) {
165             return new CComboObservableList((CCombo) description);
166         } else if (description instanceof List) {
167             return new ListObservableList((List) description);
168         }
169         return null;
170     }
171
172     /**
173      * @param time
174      * Values are TIME_EARLY or TIME_LATE and specify when update
175      * occurs For example TIME_EARLY on Text control and update
176      * occurs per keystroke, TIME_LATE and validation occurs when the
177      * field loses focus
178      */

179     public void setUpdateTime(int time) {
180         updateTime = time;
181     }
182 }
183
Popular Tags