Changes:
[phpeclipse.git] / net.sourceforge.phpeclipse / 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 /**
27  * Dialog field containing a label, text control and a button control.
28  */
29 public class StringButtonDialogField extends StringDialogField {
30                 
31         private Button fBrowseButton;
32         private String fBrowseButtonLabel;
33         private IStringButtonAdapter fStringButtonAdapter;
34         
35         private boolean fButtonEnabled;
36         
37         public StringButtonDialogField(IStringButtonAdapter adapter) {
38                 super();
39                 fStringButtonAdapter= adapter;
40                 fBrowseButtonLabel= "!Browse...!"; //$NON-NLS-1$
41                 fButtonEnabled= true;
42         }
43
44         /**
45          * Sets the label of the button.
46          */
47         public void setButtonLabel(String label) {
48                 fBrowseButtonLabel= label;
49         }
50         
51         // ------ adapter communication
52
53         /**
54          * Programmatical pressing of the button
55          */     
56         public void changeControlPressed() {
57                 fStringButtonAdapter.changeControlPressed(this);
58         }
59         
60         // ------- layout helpers
61
62         /*
63          * @see DialogField#doFillIntoGrid
64          */             
65         public Control[] doFillIntoGrid(Composite parent, int nColumns) {
66                 assertEnoughColumns(nColumns);
67                 
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));
74         
75                 return new Control[] { label, text, button };
76         }       
77
78         /*
79          * @see DialogField#getNumberOfControls
80          */             
81         public int getNumberOfControls() {
82                 return 3;       
83         }
84         
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);              
92                 return gd;
93         }               
94         
95         // ------- ui creation  
96
97         /**
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.
101          */             
102         public Button getChangeControl(Composite parent) {
103                 if (fBrowseButton == null) {
104                         assertCompositeNotNull(parent);
105                         
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();
112                                 }
113                                 public void widgetSelected(SelectionEvent e) {
114                                         changeControlPressed();
115                                 }
116                         });     
117                         
118                 }
119                 return fBrowseButton;
120         }
121         
122         // ------ enable / disable management
123         
124         /**
125          * Sets the enable state of the button.
126          */
127         public void enableButton(boolean enable) {
128                 if (isOkToUse(fBrowseButton)) {
129                         fBrowseButton.setEnabled(isEnabled() && enable);
130                 }
131                 fButtonEnabled= enable;
132         }
133
134         /*
135          * @see DialogField#updateEnableState
136          */     
137         protected void updateEnableState() {
138                 super.updateEnableState();
139                 if (isOkToUse(fBrowseButton)) {
140                         fBrowseButton.setEnabled(isEnabled() && fButtonEnabled);
141                 }
142         }       
143 }