/******************************************************************************* * Copyright (c) 2008 Pierre-Antoine Grégoire. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Pierre-Antoine Grégoire - initial API and implementation *******************************************************************************/ package org.org.eclipse.core.utils.platform.fields; import org.eclipse.core.runtime.Assert; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; /** * Base class of all dialog fields. */ public class DialogField implements IDialogField { private Label fLabel; protected String fLabelText; private IDialogFieldListener fDialogFieldListener; private boolean fEnabled; public DialogField() { fEnabled = true; fLabel = null; fLabelText = ""; //$NON-NLS-1$ } /* * (non-Javadoc) * * @see org.org.eclipse.core.utils.platform.dialogs.formfields.IDialogField#setLabelText(java.lang.String) */ public void setLabelText(String labeltext) { fLabelText = labeltext; if (isOkToUse(fLabel)) { fLabel.setText(labeltext); } } // ------ change listener /* * (non-Javadoc) * * @see org.org.eclipse.core.utils.platform.dialogs.formfields.IDialogField#setDialogFieldListener(org.org.eclipse.core.utils.platform.dialogs.IDialogFieldListener) */ public final void setDialogFieldListener(IDialogFieldListener listener) { fDialogFieldListener = listener; } /* * (non-Javadoc) * * @see org.org.eclipse.core.utils.platform.dialogs.formfields.IDialogField#dialogFieldChanged() */ public void dialogFieldChanged() { if (fDialogFieldListener != null) { fDialogFieldListener.dialogFieldChanged(this); } } // ------- focus management /* * (non-Javadoc) * * @see org.org.eclipse.core.utils.platform.dialogs.formfields.IDialogField#setFocus() */ public boolean setFocus() { return false; } /* * (non-Javadoc) * * @see org.org.eclipse.core.utils.platform.dialogs.formfields.IDialogField#postSetFocusOnDialogField(org.eclipse.swt.widgets.Display) */ public void postSetFocusOnDialogField(Display display) { if (display != null) { display.asyncExec(new Runnable() { public void run() { setFocus(); } }); } } // ------- layout helpers /* * (non-Javadoc) * * @see org.org.eclipse.core.utils.platform.dialogs.formfields.IDialogField#doFillIntoGrid(org.eclipse.swt.widgets.Composite, int) */ public Control[] doFillIntoTable(Composite parent, int nColumns) { Label label = getLabelControl(parent); label.setLayoutData(gridDataForLabel(nColumns)); return new Control[] { label }; } /* * (non-Javadoc) * * @see org.org.eclipse.core.utils.platform.dialogs.formfields.IDialogField#getNumberOfControls() */ public int getNumberOfControls() { return 1; } protected static GridData gridDataForLabel(int span) { GridData td = new GridData(SWT.LEFT, SWT.CENTER, false, false, span, 1); return td; } // ------- ui creation /* * (non-Javadoc) * * @see org.org.eclipse.core.utils.platform.dialogs.formfields.IDialogField#getLabelControl(org.eclipse.swt.widgets.Composite) */ public Label getLabelControl(Composite parent) { if (fLabel == null) { assertCompositeNotNull(parent); fLabel = new Label(parent, SWT.NONE); fLabel.setFont(parent.getFont()); fLabel.setEnabled(fEnabled); if (fLabelText != null && !"".equals(fLabelText)) { //$NON-NLS-1$ fLabel.setText(fLabelText); } else { // XXX: to avoid a 16 pixel wide empty label - revisit fLabel.setText("."); //$NON-NLS-1$ fLabel.setVisible(false); } } return fLabel; } /** * Tests is the control is not <code>null</code> and not disposed. */ protected final boolean isOkToUse(Control control) { return (control != null) && (Display.getCurrent() != null) && !control.isDisposed(); } // --------- enable / disable management /* * (non-Javadoc) * * @see org.org.eclipse.core.utils.platform.dialogs.formfields.IDialogField#setEnabled(boolean) */ public void setEnabled(boolean enabled) { if (enabled != fEnabled) { fEnabled = enabled; updateEnableState(); } } /** * Called when the enable state changed. To be extended by dialog field implementors. */ protected void updateEnableState() { if (fLabel != null) { fLabel.setEnabled(fEnabled); } } /** * Creates a spacer control. * * @param parent * The parent composite */ public static Control createEmptySpace(Composite parent) { return createEmptySpace(parent, 1); } /** * Creates a spacer control with the given span. The composite is assumed to have <code>MGridLayout</code> as layout. * * @param parent * The parent composite */ public static Control createEmptySpace(Composite parent, int span) { Label label = new Label(parent, SWT.LEFT); GridData td = new GridData(); td.horizontalSpan = span; td.heightHint = 0; label.setLayoutData(td); return label; } /* * (non-Javadoc) * * @see org.org.eclipse.core.utils.platform.dialogs.formfields.IDialogField#refresh() */ public void refresh() { updateEnableState(); } /* * (non-Javadoc) * * @see org.org.eclipse.core.utils.platform.dialogs.formfields.IDialogField#isEnabled() */ public final boolean isEnabled() { return fEnabled; } protected final void assertCompositeNotNull(Composite comp) { Assert.isNotNull(comp, "uncreated control requested with composite null"); //$NON-NLS-1$ } }