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 net.sourceforge.phpdt.internal.ui.util.SWTUtil;
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.events.SelectionEvent;
17 import org.eclipse.swt.events.SelectionListener;
18 import org.eclipse.swt.layout.GridData;
19 import org.eclipse.swt.widgets.Button;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Control;
22 import org.eclipse.swt.widgets.Label;
23 import org.eclipse.swt.widgets.Text;
27 * Dialog field containing a label, text control and a button control.
29 public class StringButtonDialogField extends StringDialogField {
31 private Button fBrowseButton;
32 private String fBrowseButtonLabel;
33 private IStringButtonAdapter fStringButtonAdapter;
35 private boolean fButtonEnabled;
37 public StringButtonDialogField(IStringButtonAdapter adapter) {
39 fStringButtonAdapter= adapter;
40 fBrowseButtonLabel= "!Browse...!"; //$NON-NLS-1$
45 * Sets the label of the button.
47 public void setButtonLabel(String label) {
48 fBrowseButtonLabel= label;
51 // ------ adapter communication
54 * Programmatical pressing of the button
56 public void changeControlPressed() {
57 fStringButtonAdapter.changeControlPressed(this);
60 // ------- layout helpers
63 * @see DialogField#doFillIntoGrid
65 public Control[] doFillIntoGrid(Composite parent, int nColumns) {
66 assertEnoughColumns(nColumns);
68 Label label= getLabelControl(parent);
69 label.setLayoutData(gridDataForLabel(1));
70 Text text= getTextControl(parent);
71 text.setLayoutData(gridDataForText(nColumns - 2));
72 Button button= getChangeControl(parent);
73 button.setLayoutData(gridDataForButton(button, 1));
75 return new Control[] { label, text, button };
79 * @see DialogField#getNumberOfControls
81 public int getNumberOfControls() {
85 protected static GridData gridDataForButton(Button button, int span) {
86 GridData gd= new GridData();
87 gd.horizontalAlignment= GridData.FILL;
88 gd.grabExcessHorizontalSpace= false;
89 gd.horizontalSpan= span;
90 gd.heightHint = SWTUtil.getButtonHeigthHint(button);
91 gd.widthHint = SWTUtil.getButtonWidthHint(button);
95 // ------- ui creation
98 * Creates or returns the created buttom widget.
99 * @param parent The parent composite or <code>null</code> if the widget has
100 * already been created.
102 public Button getChangeControl(Composite parent) {
103 if (fBrowseButton == null) {
104 assertCompositeNotNull(parent);
106 fBrowseButton= new Button(parent, SWT.PUSH);
107 fBrowseButton.setText(fBrowseButtonLabel);
108 fBrowseButton.setEnabled(isEnabled() && fButtonEnabled);
109 fBrowseButton.addSelectionListener(new SelectionListener() {
110 public void widgetDefaultSelected(SelectionEvent e) {
111 changeControlPressed();
113 public void widgetSelected(SelectionEvent e) {
114 changeControlPressed();
119 return fBrowseButton;
122 // ------ enable / disable management
125 * Sets the enable state of the button.
127 public void enableButton(boolean enable) {
128 if (isOkToUse(fBrowseButton)) {
129 fBrowseButton.setEnabled(isEnabled() && enable);
131 fButtonEnabled= enable;
135 * @see DialogField#updateEnableState
137 protected void updateEnableState() {
138 super.updateEnableState();
139 if (isOkToUse(fBrowseButton)) {
140 fBrowseButton.setEnabled(isEnabled() && fButtonEnabled);