Changes:
[phpeclipse.git] / net.sourceforge.phpeclipse / 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.
23  * Dialog fields manage controls together with the model, independed
24  * from the creation time of the widgets.
25  * - support for automated layouting.
26  * - enable / disable, set focus a concept of the base class.
27  * 
28  * DialogField have a label.
29  */ 
30 public class DialogField {
31
32         private Label fLabel;
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.
74          * Returns <code>true</code> if the dialog field can take focus.
75          *      To be reimplemented by dialog field 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(
87                                 new Runnable() {
88                                         public void run() {
89                                                 setFocus();
90                                         }
91                                 }
92                         );
93                 }
94         }               
95         
96         // ------- layout helpers
97         
98         /**
99          * Creates all controls of the dialog field and fills it to a composite.
100          * The composite is assumed to have <code>MGridLayout</code> as
101          * layout.
102          * The dialog field will adjust its controls' spans to the number of columns given.
103          *      To be reimplemented by dialog field implementors.
104          */
105         public Control[] doFillIntoGrid(Composite parent, int nColumns) {
106                 assertEnoughColumns(nColumns);
107                 
108                 Label label= getLabelControl(parent);
109                 label.setLayoutData(gridDataForLabel(nColumns));
110                 
111                 return new Control[] { label };
112         }
113         
114         /**
115          * Returns the number of columns of the dialog field.
116          *      To be reimplemented by dialog field implementors.
117          */
118         public int getNumberOfControls() {
119                 return 1;       
120         }       
121         
122         protected static GridData gridDataForLabel(int span) {
123                 GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
124                 gd.horizontalSpan= span;
125                 return gd;
126         }
127         
128         // ------- ui creation
129
130         /**
131          * Creates or returns the created label widget.
132          * @param parent The parent composite or <code>null</code> if the widget has
133          * already been created.
134          */                     
135         public Label getLabelControl(Composite parent) {
136                 if (fLabel == null) {
137                         assertCompositeNotNull(parent);
138                         
139                         fLabel= new Label(parent, SWT.LEFT | SWT.WRAP);
140                         fLabel.setFont(parent.getFont());
141                         fLabel.setEnabled(fEnabled);            
142                         if (fLabelText != null && !"".equals(fLabelText)) { //$NON-NLS-1$
143                                 fLabel.setText(fLabelText);
144                         } else {
145                                 // XXX: to avoid a 16 pixel wide empty label - revisit
146                                 fLabel.setText("."); //$NON-NLS-1$
147                                 fLabel.setVisible(false);
148                         }                       
149                 }
150                 return fLabel;
151         }
152
153         /**
154          * Creates a spacer control.
155          * @param parent The parent composite
156          */             
157         public static Control createEmptySpace(Composite parent) {
158                 return createEmptySpace(parent, 1);
159         }
160
161         /**
162          * Creates a spacer control with the given span.
163          * The composite is assumed to have <code>MGridLayout</code> as
164          * layout.
165          * @param parent The parent composite
166          */                     
167         public static Control createEmptySpace(Composite parent, int span) {
168                 Label label= new Label(parent, SWT.LEFT);
169                 GridData gd= new GridData();
170                 gd.horizontalAlignment= GridData.BEGINNING;
171                 gd.grabExcessHorizontalSpace= false;
172                 gd.horizontalSpan= span;
173                 gd.horizontalIndent= 0;
174                 gd.widthHint= 0;
175                 gd.heightHint= 0;
176                 label.setLayoutData(gd);
177                 return label;
178         }
179         
180         /**
181          * Tests is the control is not <code>null</code> and not disposed.
182         */
183         protected final boolean isOkToUse(Control control) {
184                 return (control != null) && !(control.isDisposed());
185         }
186         
187         // --------- enable / disable management
188         
189         /**
190          * Sets the enable state of the dialog field.
191          */
192         public final void setEnabled(boolean enabled) {
193                 if (enabled != fEnabled) {
194                         fEnabled= enabled;
195                         updateEnableState();
196                 }
197         }
198         
199         /**
200          * Called when the enable state changed.
201          * To be extended by dialog field implementors.
202          */
203         protected void updateEnableState() {
204                 if (fLabel != null) {
205                         fLabel.setEnabled(fEnabled);
206                 }
207         }
208
209         /**
210          * Gets the enable state of the dialog field.
211          */     
212         public final boolean isEnabled() {
213                 return fEnabled;
214         }
215
216         protected final void assertCompositeNotNull(Composite comp) {
217                 Assert.isNotNull(comp, "uncreated control requested with composite null"); //$NON-NLS-1$
218         }
219         
220         protected final void assertEnoughColumns(int nColumns) {
221                 Assert.isTrue(nColumns >= getNumberOfControls(), "given number of columns is too small"); //$NON-NLS-1$
222         }
223         
224         
225
226         
227 }