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;
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;
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
29 * DialogField have a label.
31 public class DialogField {
35 protected String fLabelText;
37 private IDialogFieldListener fDialogFieldListener;
39 private boolean fEnabled;
41 public DialogField() {
44 fLabelText = ""; //$NON-NLS-1$
48 * Sets the label of the dialog field.
50 public void setLabelText(String labeltext) {
51 fLabelText = labeltext;
54 // ------ change listener
57 * Defines the listener for this dialog field.
59 public final void setDialogFieldListener(IDialogFieldListener listener) {
60 fDialogFieldListener = listener;
64 * Programatical invocation of a dialog field change.
66 public void dialogFieldChanged() {
67 if (fDialogFieldListener != null) {
68 fDialogFieldListener.dialogFieldChanged(this);
72 // ------- focus management
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
79 public boolean setFocus() {
84 * Posts <code>setFocus</code> to the display event queue.
86 public void postSetFocusOnDialogField(Display display) {
87 if (display != null) {
88 display.asyncExec(new Runnable() {
96 // ------- layout helpers
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.
104 public Control[] doFillIntoGrid(Composite parent, int nColumns) {
105 assertEnoughColumns(nColumns);
107 Label label = getLabelControl(parent);
108 label.setLayoutData(gridDataForLabel(nColumns));
110 return new Control[] { label };
114 * Returns the number of columns of the dialog field. To be reimplemented by
115 * dialog field implementors.
117 public int getNumberOfControls() {
121 protected static GridData gridDataForLabel(int span) {
122 GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
123 gd.horizontalSpan = span;
127 // ------- ui creation
130 * Creates or returns the created label widget.
133 * The parent composite or <code>null</code> if the widget has
134 * already been created.
136 public Label getLabelControl(Composite parent) {
137 if (fLabel == null) {
138 assertCompositeNotNull(parent);
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);
146 // XXX: to avoid a 16 pixel wide empty label - revisit
147 fLabel.setText("."); //$NON-NLS-1$
148 fLabel.setVisible(false);
155 * Creates a spacer control.
158 * The parent composite
160 public static Control createEmptySpace(Composite parent) {
161 return createEmptySpace(parent, 1);
165 * Creates a spacer control with the given span. The composite is assumed to
166 * have <code>MGridLayout</code> as layout.
169 * The parent composite
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;
180 label.setLayoutData(gd);
185 * Tests is the control is not <code>null</code> and not disposed.
187 protected final boolean isOkToUse(Control control) {
188 return (control != null) && !(control.isDisposed());
191 // --------- enable / disable management
194 * Sets the enable state of the dialog field.
196 public final void setEnabled(boolean enabled) {
197 if (enabled != fEnabled) {
204 * Called when the enable state changed. To be extended by dialog field
207 protected void updateEnableState() {
208 if (fLabel != null) {
209 fLabel.setEnabled(fEnabled);
214 * Gets the enable state of the dialog field.
216 public final boolean isEnabled() {
220 protected final void assertCompositeNotNull(Composite comp) {
221 Assert.isNotNull(comp,
222 "uncreated control requested with composite null"); //$NON-NLS-1$
225 protected final void assertEnoughColumns(int nColumns) {
226 Assert.isTrue(nColumns >= getNumberOfControls(),
227 "given number of columns is too small"); //$NON-NLS-1$