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. 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
27 * DialogField have a label.
29 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. Returns <code>true</code>
74 * if the dialog field can take focus. To be reimplemented by dialog field
77 public boolean setFocus() {
82 * Posts <code>setFocus</code> to the display event queue.
84 public void postSetFocusOnDialogField(Display display) {
85 if (display != null) {
86 display.asyncExec(new Runnable() {
94 // ------- layout helpers
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.
102 public Control[] doFillIntoGrid(Composite parent, int nColumns) {
103 assertEnoughColumns(nColumns);
105 Label label = getLabelControl(parent);
106 label.setLayoutData(gridDataForLabel(nColumns));
108 return new Control[] { label };
112 * Returns the number of columns of the dialog field. To be reimplemented by
113 * dialog field implementors.
115 public int getNumberOfControls() {
119 protected static GridData gridDataForLabel(int span) {
120 GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
121 gd.horizontalSpan = span;
125 // ------- ui creation
128 * Creates or returns the created label widget.
131 * The parent composite or <code>null</code> if the widget has
132 * already been created.
134 public Label getLabelControl(Composite parent) {
135 if (fLabel == null) {
136 assertCompositeNotNull(parent);
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);
144 // XXX: to avoid a 16 pixel wide empty label - revisit
145 fLabel.setText("."); //$NON-NLS-1$
146 fLabel.setVisible(false);
153 * Creates a spacer control.
156 * The parent composite
158 public static Control createEmptySpace(Composite parent) {
159 return createEmptySpace(parent, 1);
163 * Creates a spacer control with the given span. The composite is assumed to
164 * have <code>MGridLayout</code> as layout.
167 * The parent composite
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;
178 label.setLayoutData(gd);
183 * Tests is the control is not <code>null</code> and not disposed.
185 protected final boolean isOkToUse(Control control) {
186 return (control != null) && !(control.isDisposed());
189 // --------- enable / disable management
192 * Sets the enable state of the dialog field.
194 public final void setEnabled(boolean enabled) {
195 if (enabled != fEnabled) {
202 * Called when the enable state changed. To be extended by dialog field
205 protected void updateEnableState() {
206 if (fLabel != null) {
207 fLabel.setEnabled(fEnabled);
212 * Gets the enable state of the dialog field.
214 public final boolean isEnabled() {
218 protected final void assertCompositeNotNull(Composite comp) {
219 Assert.isNotNull(comp,
220 "uncreated control requested with composite null"); //$NON-NLS-1$
223 protected final void assertEnoughColumns(int nColumns) {
224 Assert.isTrue(nColumns >= getNumberOfControls(),
225 "given number of columns is too small"); //$NON-NLS-1$