KickJava   Java API By Example, From Geeks To Geeks.

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


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
21 import org.eclipse.jdt.internal.ui.util.SWTUtil;
22
23 /**
24  * Dialog Field containing a single button such as a radio or checkbox button.
25  */

26 public class SelectionButtonDialogField extends DialogField {
27     
28     private Button fButton;
29     private boolean fIsSelected;
30     private DialogField[] fAttachedDialogFields;
31     private int fButtonStyle;
32
33     /**
34      * Creates a selection button.
35      * Allowed button styles: SWT.RADIO, SWT.CHECK, SWT.TOGGLE, SWT.PUSH
36      */

37     public SelectionButtonDialogField(int buttonStyle) {
38         super();
39         fIsSelected= false;
40         fAttachedDialogFields= null;
41         fButtonStyle= buttonStyle;
42     }
43     
44     /**
45      * Attaches a field to the selection state of the selection button.
46      * The attached field will be disabled if the selection button is not selected.
47      */

48     public void attachDialogField(DialogField dialogField) {
49         attachDialogFields(new DialogField[] { dialogField });
50     }
51
52     /**
53      * Attaches fields to the selection state of the selection button.
54      * The attached fields will be disabled if the selection button is not selected.
55      */

56     public void attachDialogFields(DialogField[] dialogFields) {
57         fAttachedDialogFields= dialogFields;
58         for (int i= 0; i < dialogFields.length; i++) {
59             dialogFields[i].setEnabled(fIsSelected);
60         }
61     }
62     
63     /**
64      * Returns <code>true</code> is teh gived field is attached to the selection button.
65      */

66     public boolean isAttached(DialogField editor) {
67         if (fAttachedDialogFields != null) {
68             for (int i=0; i < fAttachedDialogFields.length; i++) {
69                 if (fAttachedDialogFields[i] == editor) {
70                     return true;
71                 }
72             }
73         }
74         return false;
75     }
76     
77     // ------- layout helpers
78

79     /*
80      * @see DialogField#doFillIntoGrid
81      */

82     public Control[] doFillIntoGrid(Composite parent, int nColumns) {
83         assertEnoughColumns(nColumns);
84         
85         Button button= getSelectionButton(parent);
86         GridData gd= new GridData();
87         gd.horizontalSpan= nColumns;
88         gd.horizontalAlignment= GridData.FILL;
89         if (fButtonStyle == SWT.PUSH) {
90             gd.widthHint = SWTUtil.getButtonWidthHint(button);
91         }
92         
93         button.setLayoutData(gd);
94         
95         return new Control[] { button };
96     }
97
98     /*
99      * @see DialogField#getNumberOfControls
100      */

101     public int getNumberOfControls() {
102         return 1;
103     }
104     
105     // ------- ui creation
106

107     /**
108      * Returns the selection button widget. When called the first time, the widget will be created.
109      * @param group The parent composite when called the first time, or <code>null</code>
110      * after.
111      */

112     public Button getSelectionButton(Composite group) {
113         if (fButton == null) {
114             assertCompositeNotNull(group);
115             
116             fButton= new Button(group, fButtonStyle);
117             fButton.setFont(group.getFont());
118             fButton.setText(fLabelText);
119             fButton.setEnabled(isEnabled());
120             fButton.setSelection(fIsSelected);
121             fButton.addSelectionListener(new SelectionListener() {
122                 public void widgetDefaultSelected(SelectionEvent e) {
123                     doWidgetSelected(e);
124                 }
125                 public void widgetSelected(SelectionEvent e) {
126                     doWidgetSelected(e);
127                 }
128             });
129         }
130         return fButton;
131     }
132     
133     private void doWidgetSelected(SelectionEvent e) {
134         if (isOkToUse(fButton)) {
135             changeValue(fButton.getSelection());
136         }
137     }
138     
139     private void changeValue(boolean newState) {
140         if (fIsSelected != newState) {
141             fIsSelected= newState;
142             if (fAttachedDialogFields != null) {
143                 boolean focusSet= false;
144                 for (int i= 0; i < fAttachedDialogFields.length; i++) {
145                     fAttachedDialogFields[i].setEnabled(fIsSelected);
146                     if (fIsSelected && !focusSet) {
147                         focusSet= fAttachedDialogFields[i].setFocus();
148                     }
149                 }
150             }
151             dialogFieldChanged();
152         } else if (fButtonStyle == SWT.PUSH) {
153             dialogFieldChanged();
154         }
155     }
156
157     /* (non-Javadoc)
158      * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField#setLabelText(java.lang.String)
159      */

160     public void setLabelText(String JavaDoc labeltext) {
161         fLabelText= labeltext;
162         if (isOkToUse(fButton)) {
163             fButton.setText(labeltext);
164         }
165     }
166     
167     
168     // ------ model access
169

170     /**
171      * Returns the selection state of the button.
172      */

173     public boolean isSelected() {
174         return fIsSelected;
175     }
176
177     /**
178      * Sets the selection state of the button.
179      */

180     public void setSelection(boolean selected) {
181         changeValue(selected);
182         if (isOkToUse(fButton)) {
183             fButton.setSelection(selected);
184         }
185     }
186
187     // ------ enable / disable management
188

189     /*
190      * @see DialogField#updateEnableState
191      */

192     protected void updateEnableState() {
193         super.updateEnableState();
194         if (isOkToUse(fButton)) {
195             fButton.setEnabled(isEnabled());
196         }
197     }
198     
199     /*(non-Javadoc)
200      * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField#refresh()
201      */

202     public void refresh() {
203         super.refresh();
204         if (isOkToUse(fButton)) {
205             fButton.setSelection(fIsSelected);
206         }
207     }
208     
209         
210 }
211
Popular Tags