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