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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.wizards.dialogfields;
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;
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.
28 * DialogField have a label.
30 public class DialogField {
33 protected String fLabelText;
35 private IDialogFieldListener fDialogFieldListener;
37 private boolean fEnabled;
39 public DialogField() {
42 fLabelText= ""; //$NON-NLS-1$
46 * Sets the label of the dialog field.
48 public void setLabelText(String labeltext) {
49 fLabelText= labeltext;
52 // ------ change listener
55 * Defines the listener for this dialog field.
57 public final void setDialogFieldListener(IDialogFieldListener listener) {
58 fDialogFieldListener= listener;
62 * Programatical invocation of a dialog field change.
64 public void dialogFieldChanged() {
65 if (fDialogFieldListener != null) {
66 fDialogFieldListener.dialogFieldChanged(this);
70 // ------- focus management
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.
77 public boolean setFocus() {
82 * Posts <code>setFocus</code> to the display event queue.
84 public void postSetFocusOnDialogField(Display display) {
85 if (display != null) {
96 // ------- layout helpers
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
102 * The dialog field will adjust its controls' spans to the number of columns given.
103 * To be reimplemented by dialog field implementors.
105 public Control[] doFillIntoGrid(Composite parent, int nColumns) {
106 assertEnoughColumns(nColumns);
108 Label label= getLabelControl(parent);
109 label.setLayoutData(gridDataForLabel(nColumns));
111 return new Control[] { label };
115 * Returns the number of columns of the dialog field.
116 * To be reimplemented by dialog field implementors.
118 public int getNumberOfControls() {
122 protected static GridData gridDataForLabel(int span) {
123 GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
124 gd.horizontalSpan= span;
128 // ------- ui creation
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.
135 public Label getLabelControl(Composite parent) {
136 if (fLabel == null) {
137 assertCompositeNotNull(parent);
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);
145 // XXX: to avoid a 16 pixel wide empty label - revisit
146 fLabel.setText("."); //$NON-NLS-1$
147 fLabel.setVisible(false);
154 * Creates a spacer control.
155 * @param parent The parent composite
157 public static Control createEmptySpace(Composite parent) {
158 return createEmptySpace(parent, 1);
162 * Creates a spacer control with the given span.
163 * The composite is assumed to have <code>MGridLayout</code> as
165 * @param parent The parent composite
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;
176 label.setLayoutData(gd);
181 * Tests is the control is not <code>null</code> and not disposed.
183 protected final boolean isOkToUse(Control control) {
184 return (control != null) && !(control.isDisposed());
187 // --------- enable / disable management
190 * Sets the enable state of the dialog field.
192 public final void setEnabled(boolean enabled) {
193 if (enabled != fEnabled) {
200 * Called when the enable state changed.
201 * To be extended by dialog field implementors.
203 protected void updateEnableState() {
204 if (fLabel != null) {
205 fLabel.setEnabled(fEnabled);
210 * Gets the enable state of the dialog field.
212 public final boolean isEnabled() {
216 protected final void assertCompositeNotNull(Composite comp) {
217 Assert.isNotNull(comp, "uncreated control requested with composite null"); //$NON-NLS-1$
220 protected final void assertEnoughColumns(int nColumns) {
221 Assert.isTrue(nColumns >= getNumberOfControls(), "given number of columns is too small"); //$NON-NLS-1$