KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > wizards > dialogfields > StringButtonDialogField


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.jdt.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.jdt.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 JavaDoc 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 JavaDoc 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     public Control[] doFillIntoGrid(Composite parent, int nColumns) {
65         assertEnoughColumns(nColumns);
66         
67         Label label= getLabelControl(parent);
68         label.setLayoutData(gridDataForLabel(1));
69         Text text= getTextControl(parent);
70         text.setLayoutData(gridDataForText(nColumns - 2));
71         Button button= getChangeControl(parent);
72         button.setLayoutData(gridDataForButton(button, 1));
73     
74         return new Control[] { label, text, button };
75     }
76
77     /*
78      * @see DialogField#getNumberOfControls
79      */

80     public int getNumberOfControls() {
81         return 3;
82     }
83     
84     protected static GridData gridDataForButton(Button button, int span) {
85         GridData gd= new GridData();
86         gd.horizontalAlignment= GridData.FILL;
87         gd.grabExcessHorizontalSpace= false;
88         gd.horizontalSpan= span;
89         gd.widthHint = SWTUtil.getButtonWidthHint(button);
90         return gd;
91     }
92     
93     // ------- ui creation
94

95     /**
96      * Creates or returns the created buttom widget.
97      * @param parent The parent composite or <code>null</code> if the widget has
98      * already been created.
99      */

100     public Button getChangeControl(Composite parent) {
101         if (fBrowseButton == null) {
102             assertCompositeNotNull(parent);
103             
104             fBrowseButton= new Button(parent, SWT.PUSH);
105             fBrowseButton.setFont(parent.getFont());
106             fBrowseButton.setText(fBrowseButtonLabel);
107             fBrowseButton.setEnabled(isEnabled() && fButtonEnabled);
108             fBrowseButton.addSelectionListener(new SelectionListener() {
109                 public void widgetDefaultSelected(SelectionEvent e) {
110                     changeControlPressed();
111                 }
112                 public void widgetSelected(SelectionEvent e) {
113                     changeControlPressed();
114                 }
115             });
116             
117         }
118         return fBrowseButton;
119     }
120     
121     // ------ enable / disable management
122

123     /**
124      * Sets the enable state of the button.
125      */

126     public void enableButton(boolean enable) {
127         if (isOkToUse(fBrowseButton)) {
128             fBrowseButton.setEnabled(isEnabled() && enable);
129         }
130         fButtonEnabled= enable;
131     }
132
133     /*
134      * @see DialogField#updateEnableState
135      */

136     protected void updateEnableState() {
137         super.updateEnableState();
138         if (isOkToUse(fBrowseButton)) {
139             fBrowseButton.setEnabled(isEnabled() && fButtonEnabled);
140         }
141     }
142 }
143
Popular Tags