upload tizen1.0 source
[sdk/ide/product.git] / org.eclipse.cdt.ui / src / org / eclipse / cdt / internal / ui / wizards / dialogfields / StringButtonDialogField.java
1 /*******************************************************************************
2  * Copyright (c) 2005, 2008 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.cdt.internal.ui.wizards.dialogfields;
12
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.events.SelectionEvent;
15 import org.eclipse.swt.events.SelectionListener;
16 import org.eclipse.swt.layout.GridData;
17 import org.eclipse.swt.widgets.Button;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Control;
20 import org.eclipse.swt.widgets.Label;
21 import org.eclipse.swt.widgets.Text;
22
23 import org.eclipse.cdt.internal.ui.util.SWTUtil;
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         private String fBrowseButtonLabel;
32         private IStringButtonAdapter fStringButtonAdapter;
33         
34         private boolean fButtonEnabled;
35         
36         public StringButtonDialogField(IStringButtonAdapter adapter) {
37                 super();
38                 fStringButtonAdapter= adapter;
39                 fBrowseButtonLabel= "!Browse...!"; //$NON-NLS-1$
40                 fButtonEnabled= true;
41         }
42
43         /**
44          * Sets the label of the button.
45          */
46         public void setButtonLabel(String label) {
47                 fBrowseButtonLabel= label;
48         }
49         
50         // ------ adapter communication
51
52         /**
53          * Programmatical pressing of the button
54          */     
55         public void changeControlPressed() {
56                 fStringButtonAdapter.changeControlPressed(this);
57         }
58         
59         // ------- layout helpers
60
61         /*
62          * @see DialogField#doFillIntoGrid
63          */             
64         @Override
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         @Override
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.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         @Override
138         protected void updateEnableState() {
139                 super.updateEnableState();
140                 if (isOkToUse(fBrowseButton)) {
141                         fBrowseButton.setEnabled(isEnabled() && fButtonEnabled);
142                 }
143         }               
144 }