aa06105926f9ba7c57aac95311cb5e50217dc66f
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / internal / ui / wizards / dialogfields / StringButtonDialogField.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.wizards.dialogfields;
12
13 import net.sourceforge.phpdt.internal.ui.util.SWTUtil;
14
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;
24
25 /**
26  * Dialog field containing a label, text control and a button control.
27  */
28 public class StringButtonDialogField extends StringDialogField {
29
30         private Button fBrowseButton;
31
32         private String fBrowseButtonLabel;
33
34         private IStringButtonAdapter fStringButtonAdapter;
35
36         private boolean fButtonEnabled;
37
38         public StringButtonDialogField(IStringButtonAdapter adapter) {
39                 super();
40                 fStringButtonAdapter = adapter;
41                 fBrowseButtonLabel = "!Browse...!"; //$NON-NLS-1$
42                 fButtonEnabled = true;
43         }
44
45         /**
46          * Sets the label of the button.
47          */
48         public void setButtonLabel(String label) {
49                 fBrowseButtonLabel = label;
50         }
51
52         // ------ adapter communication
53
54         /**
55          * Programmatical pressing of the button
56          */
57         public void changeControlPressed() {
58                 fStringButtonAdapter.changeControlPressed(this);
59         }
60
61         // ------- layout helpers
62
63         /*
64          * @see DialogField#doFillIntoGrid
65          */
66         public Control[] doFillIntoGrid(Composite parent, int nColumns) {
67                 assertEnoughColumns(nColumns);
68
69                 Label label = getLabelControl(parent);
70                 label.setLayoutData(gridDataForLabel(1));
71                 Text text = getTextControl(parent);
72                 text.setLayoutData(gridDataForText(nColumns - 2));
73                 Button button = getChangeControl(parent);
74                 button.setLayoutData(gridDataForButton(button, 1));
75
76                 return new Control[] { label, text, button };
77         }
78
79         /*
80          * @see DialogField#getNumberOfControls
81          */
82         public int getNumberOfControls() {
83                 return 3;
84         }
85
86         protected static GridData gridDataForButton(Button button, int span) {
87                 GridData gd = new GridData();
88                 gd.horizontalAlignment = GridData.FILL;
89                 gd.grabExcessHorizontalSpace = false;
90                 gd.horizontalSpan = span;
91                 gd.heightHint = SWTUtil.getButtonHeightHint(button);
92                 gd.widthHint = SWTUtil.getButtonWidthHint(button);
93                 return gd;
94         }
95
96         // ------- ui creation
97
98         /**
99          * Creates or returns the created buttom widget.
100          * 
101          * @param parent
102          *            The parent composite or <code>null</code> if the widget has
103          *            already been created.
104          */
105         public Button getChangeControl(Composite parent) {
106                 if (fBrowseButton == null) {
107                         assertCompositeNotNull(parent);
108
109                         fBrowseButton = new Button(parent, SWT.PUSH);
110                         fBrowseButton.setText(fBrowseButtonLabel);
111                         fBrowseButton.setEnabled(isEnabled() && fButtonEnabled);
112                         fBrowseButton.addSelectionListener(new SelectionListener() {
113                                 public void widgetDefaultSelected(SelectionEvent e) {
114                                         changeControlPressed();
115                                 }
116
117                                 public void widgetSelected(SelectionEvent e) {
118                                         changeControlPressed();
119                                 }
120                         });
121
122                 }
123                 return fBrowseButton;
124         }
125
126         // ------ enable / disable management
127
128         /**
129          * Sets the enable state of the button.
130          */
131         public void enableButton(boolean enable) {
132                 if (isOkToUse(fBrowseButton)) {
133                         fBrowseButton.setEnabled(isEnabled() && enable);
134                 }
135                 fButtonEnabled = enable;
136         }
137
138         /*
139          * @see DialogField#updateEnableState
140          */
141         protected void updateEnableState() {
142                 super.updateEnableState();
143                 if (isOkToUse(fBrowseButton)) {
144                         fBrowseButton.setEnabled(isEnabled() && fButtonEnabled);
145                 }
146         }
147 }