b624ec5b431e2434a251a270b0a7ca82d92e34a9
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / internal / ui / wizards / dialogfields / DialogField.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.wizards.dialogfields;
12
13 import org.eclipse.jface.util.Assert;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.layout.GridData;
16 import org.eclipse.swt.widgets.Composite;
17 import org.eclipse.swt.widgets.Control;
18 import org.eclipse.swt.widgets.Display;
19 import org.eclipse.swt.widgets.Label;
20
21 /**
22  * Base class of all dialog fields. Dialog fields manage controls together with
23  * the model, independed from the creation time of the widgets. - support for
24  * automated layouting. - enable / disable, set focus a concept of the base
25  * class.
26  * 
27  * DialogField have a label.
28  */
29 public class DialogField {
30
31         private Label fLabel;
32
33         protected String fLabelText;
34
35         private IDialogFieldListener fDialogFieldListener;
36
37         private boolean fEnabled;
38
39         public DialogField() {
40                 fEnabled = true;
41                 fLabel = null;
42                 fLabelText = ""; //$NON-NLS-1$
43         }
44
45         /**
46          * Sets the label of the dialog field.
47          */
48         public void setLabelText(String labeltext) {
49                 fLabelText = labeltext;
50         }
51
52         // ------ change listener
53
54         /**
55          * Defines the listener for this dialog field.
56          */
57         public final void setDialogFieldListener(IDialogFieldListener listener) {
58                 fDialogFieldListener = listener;
59         }
60
61         /**
62          * Programatical invocation of a dialog field change.
63          */
64         public void dialogFieldChanged() {
65                 if (fDialogFieldListener != null) {
66                         fDialogFieldListener.dialogFieldChanged(this);
67                 }
68         }
69
70         // ------- focus management
71
72         /**
73          * Tries to set the focus to the dialog field. Returns <code>true</code>
74          * if the dialog field can take focus. To be reimplemented by dialog field
75          * implementors.
76          */
77         public boolean setFocus() {
78                 return false;
79         }
80
81         /**
82          * Posts <code>setFocus</code> to the display event queue.
83          */
84         public void postSetFocusOnDialogField(Display display) {
85                 if (display != null) {
86                         display.asyncExec(new Runnable() {
87                                 public void run() {
88                                         setFocus();
89                                 }
90                         });
91                 }
92         }
93
94         // ------- layout helpers
95
96         /**
97          * Creates all controls of the dialog field and fills it to a composite. The
98          * composite is assumed to have <code>MGridLayout</code> as layout. The
99          * dialog field will adjust its controls' spans to the number of columns
100          * given. To be reimplemented by dialog field implementors.
101          */
102         public Control[] doFillIntoGrid(Composite parent, int nColumns) {
103                 assertEnoughColumns(nColumns);
104
105                 Label label = getLabelControl(parent);
106                 label.setLayoutData(gridDataForLabel(nColumns));
107
108                 return new Control[] { label };
109         }
110
111         /**
112          * Returns the number of columns of the dialog field. To be reimplemented by
113          * dialog field implementors.
114          */
115         public int getNumberOfControls() {
116                 return 1;
117         }
118
119         protected static GridData gridDataForLabel(int span) {
120                 GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
121                 gd.horizontalSpan = span;
122                 return gd;
123         }
124
125         // ------- ui creation
126
127         /**
128          * Creates or returns the created label widget.
129          * 
130          * @param parent
131          *            The parent composite or <code>null</code> if the widget has
132          *            already been created.
133          */
134         public Label getLabelControl(Composite parent) {
135                 if (fLabel == null) {
136                         assertCompositeNotNull(parent);
137
138                         fLabel = new Label(parent, SWT.LEFT | SWT.WRAP);
139                         fLabel.setFont(parent.getFont());
140                         fLabel.setEnabled(fEnabled);
141                         if (fLabelText != null && !"".equals(fLabelText)) { //$NON-NLS-1$
142                                 fLabel.setText(fLabelText);
143                         } else {
144                                 // XXX: to avoid a 16 pixel wide empty label - revisit
145                                 fLabel.setText("."); //$NON-NLS-1$
146                                 fLabel.setVisible(false);
147                         }
148                 }
149                 return fLabel;
150         }
151
152         /**
153          * Creates a spacer control.
154          * 
155          * @param parent
156          *            The parent composite
157          */
158         public static Control createEmptySpace(Composite parent) {
159                 return createEmptySpace(parent, 1);
160         }
161
162         /**
163          * Creates a spacer control with the given span. The composite is assumed to
164          * have <code>MGridLayout</code> as layout.
165          * 
166          * @param parent
167          *            The parent composite
168          */
169         public static Control createEmptySpace(Composite parent, int span) {
170                 Label label = new Label(parent, SWT.LEFT);
171                 GridData gd = new GridData();
172                 gd.horizontalAlignment = GridData.BEGINNING;
173                 gd.grabExcessHorizontalSpace = false;
174                 gd.horizontalSpan = span;
175                 gd.horizontalIndent = 0;
176                 gd.widthHint = 0;
177                 gd.heightHint = 0;
178                 label.setLayoutData(gd);
179                 return label;
180         }
181
182         /**
183          * Tests is the control is not <code>null</code> and not disposed.
184          */
185         protected final boolean isOkToUse(Control control) {
186                 return (control != null) && !(control.isDisposed());
187         }
188
189         // --------- enable / disable management
190
191         /**
192          * Sets the enable state of the dialog field.
193          */
194         public final void setEnabled(boolean enabled) {
195                 if (enabled != fEnabled) {
196                         fEnabled = enabled;
197                         updateEnableState();
198                 }
199         }
200
201         /**
202          * Called when the enable state changed. To be extended by dialog field
203          * implementors.
204          */
205         protected void updateEnableState() {
206                 if (fLabel != null) {
207                         fLabel.setEnabled(fEnabled);
208                 }
209         }
210
211         /**
212          * Gets the enable state of the dialog field.
213          */
214         public final boolean isEnabled() {
215                 return fEnabled;
216         }
217
218         protected final void assertCompositeNotNull(Composite comp) {
219                 Assert.isNotNull(comp,
220                                 "uncreated control requested with composite null"); //$NON-NLS-1$
221         }
222
223         protected final void assertEnoughColumns(int nColumns) {
224                 Assert.isTrue(nColumns >= getNumberOfControls(),
225                                 "given number of columns is too small"); //$NON-NLS-1$
226         }
227
228 }