KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > wizards > dialogfields > DialogField


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.jdt.internal.ui.wizards.dialogfields;
12
13 import org.eclipse.core.runtime.Assert;
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.layout.GridData;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Control;
19 import org.eclipse.swt.widgets.Display;
20 import org.eclipse.swt.widgets.Label;
21
22
23 /**
24  * Base class of all dialog fields.
25  * Dialog fields manage controls together with the model, independed
26  * from the creation time of the widgets.
27  * - support for automated layouting.
28  * - enable / disable, set focus a concept of the base class.
29  *
30  * DialogField have a label.
31  */

32 public class DialogField {
33
34     private Label fLabel;
35     protected String JavaDoc fLabelText;
36     
37     private IDialogFieldListener fDialogFieldListener;
38     
39     private boolean fEnabled;
40
41     public DialogField() {
42         fEnabled= true;
43         fLabel= null;
44         fLabelText= ""; //$NON-NLS-1$
45
}
46     
47     /**
48      * Sets the label of the dialog field.
49      */

50     public void setLabelText(String JavaDoc labeltext) {
51         fLabelText= labeltext;
52         if (isOkToUse(fLabel)) {
53             fLabel.setText(labeltext);
54         }
55     }
56         
57     // ------ change listener
58

59     /**
60      * Defines the listener for this dialog field.
61      */

62     public final void setDialogFieldListener(IDialogFieldListener listener) {
63         fDialogFieldListener= listener;
64     }
65
66     /**
67      * Programatical invocation of a dialog field change.
68      */

69     public void dialogFieldChanged() {
70         if (fDialogFieldListener != null) {
71             fDialogFieldListener.dialogFieldChanged(this);
72         }
73     }
74     
75     // ------- focus management
76

77     /**
78      * Tries to set the focus to the dialog field.
79      * Returns <code>true</code> if the dialog field can take focus.
80      * To be reimplemented by dialog field implementors.
81      */

82     public boolean setFocus() {
83         return false;
84     }
85
86     /**
87      * Posts <code>setFocus</code> to the display event queue.
88      */

89     public void postSetFocusOnDialogField(Display display) {
90         if (display != null) {
91             display.asyncExec(
92                 new Runnable JavaDoc() {
93                     public void run() {
94                         setFocus();
95                     }
96                 }
97             );
98         }
99     }
100     
101     // ------- layout helpers
102

103     /**
104      * Creates all controls of the dialog field and fills it to a composite.
105      * The composite is assumed to have <code>MGridLayout</code> as
106      * layout.
107      * The dialog field will adjust its controls' spans to the number of columns given.
108      * To be reimplemented by dialog field implementors.
109      */

110     public Control[] doFillIntoGrid(Composite parent, int nColumns) {
111         assertEnoughColumns(nColumns);
112         
113         Label label= getLabelControl(parent);
114         label.setLayoutData(gridDataForLabel(nColumns));
115         
116         return new Control[] { label };
117     }
118     
119     /**
120      * Returns the number of columns of the dialog field.
121      * To be reimplemented by dialog field implementors.
122      */

123     public int getNumberOfControls() {
124         return 1;
125     }
126     
127     protected static GridData gridDataForLabel(int span) {
128         GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
129         gd.horizontalSpan= span;
130         return gd;
131     }
132     
133     // ------- ui creation
134

135     /**
136      * Creates or returns the created label widget.
137      * @param parent The parent composite or <code>null</code> if the widget has
138      * already been created.
139      */

140     public Label getLabelControl(Composite parent) {
141         if (fLabel == null) {
142             assertCompositeNotNull(parent);
143             
144             fLabel= new Label(parent, SWT.LEFT | SWT.WRAP);
145             fLabel.setFont(parent.getFont());
146             fLabel.setEnabled(fEnabled);
147             if (fLabelText != null && !"".equals(fLabelText)) { //$NON-NLS-1$
148
fLabel.setText(fLabelText);
149             } else {
150                 // XXX: to avoid a 16 pixel wide empty label - revisit
151
fLabel.setText("."); //$NON-NLS-1$
152
fLabel.setVisible(false);
153             }
154         }
155         return fLabel;
156     }
157
158     /**
159      * Creates a spacer control.
160      * @param parent The parent composite
161      */

162     public static Control createEmptySpace(Composite parent) {
163         return createEmptySpace(parent, 1);
164     }
165
166     /**
167      * Creates a spacer control with the given span.
168      * The composite is assumed to have <code>MGridLayout</code> as
169      * layout.
170      * @param parent The parent composite
171      */

172     public static Control createEmptySpace(Composite parent, int span) {
173         Label label= new Label(parent, SWT.LEFT);
174         GridData gd= new GridData();
175         gd.horizontalAlignment= GridData.BEGINNING;
176         gd.grabExcessHorizontalSpace= false;
177         gd.horizontalSpan= span;
178         gd.horizontalIndent= 0;
179         gd.widthHint= 0;
180         gd.heightHint= 0;
181         label.setLayoutData(gd);
182         return label;
183     }
184     
185     /**
186      * Tests is the control is not <code>null</code> and not disposed.
187     */

188     protected final boolean isOkToUse(Control control) {
189         return (control != null) && (Display.getCurrent() != null) && !control.isDisposed();
190     }
191     
192     // --------- enable / disable management
193

194     /**
195      * Sets the enable state of the dialog field.
196      */

197     public final void setEnabled(boolean enabled) {
198         if (enabled != fEnabled) {
199             fEnabled= enabled;
200             updateEnableState();
201         }
202     }
203     
204     /**
205      * Called when the enable state changed.
206      * To be extended by dialog field implementors.
207      */

208     protected void updateEnableState() {
209         if (fLabel != null) {
210             fLabel.setEnabled(fEnabled);
211         }
212     }
213     
214     /**
215      * Brings the UI in sync with the model. Only needed when model was changed
216      * in different thread whil UI was lready created.
217      */

218     public void refresh() {
219         updateEnableState();
220     }
221
222     /**
223      * Gets the enable state of the dialog field.
224      */

225     public final boolean isEnabled() {
226         return fEnabled;
227     }
228
229     protected final void assertCompositeNotNull(Composite comp) {
230         Assert.isNotNull(comp, "uncreated control requested with composite null"); //$NON-NLS-1$
231
}
232     
233     protected final void assertEnoughColumns(int nColumns) {
234         Assert.isTrue(nColumns >= getNumberOfControls(), "given number of columns is too small"); //$NON-NLS-1$
235
}
236     
237     
238
239     
240 }
241
Popular Tags