KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > preference > RadioGroupFieldEditor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.jface.preference;
12
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.DisposeEvent;
16 import org.eclipse.swt.events.DisposeListener;
17 import org.eclipse.swt.events.SelectionAdapter;
18 import org.eclipse.swt.events.SelectionEvent;
19 import org.eclipse.swt.graphics.Font;
20 import org.eclipse.swt.layout.GridData;
21 import org.eclipse.swt.layout.GridLayout;
22 import org.eclipse.swt.widgets.Button;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.swt.widgets.Group;
26
27 /**
28  * A field editor for an enumeration type preference.
29  * The choices are presented as a list of radio buttons.
30  */

31 public class RadioGroupFieldEditor extends FieldEditor {
32
33     /**
34      * List of radio button entries of the form [label,value].
35      */

36     private String JavaDoc[][] labelsAndValues;
37
38     /**
39      * Number of columns into which to arrange the radio buttons.
40      */

41     private int numColumns;
42
43     /**
44      * Indent used for the first column of the radion button matrix.
45      */

46     private int indent = HORIZONTAL_GAP;
47
48     /**
49      * The current value, or <code>null</code> if none.
50      */

51     private String JavaDoc value;
52
53     /**
54      * The box of radio buttons, or <code>null</code> if none
55      * (before creation and after disposal).
56      */

57     private Composite radioBox;
58
59     /**
60      * The radio buttons, or <code>null</code> if none
61      * (before creation and after disposal).
62      */

63     private Button[] radioButtons;
64
65     /**
66      * Whether to use a Group control.
67      */

68     private boolean useGroup;
69
70     /**
71      * Creates a new radio group field editor
72      */

73     protected RadioGroupFieldEditor() {
74     }
75
76     /**
77      * Creates a radio group field editor.
78      * This constructor does not use a <code>Group</code> to contain the radio buttons.
79      * It is equivalent to using the following constructor with <code>false</code>
80      * for the <code>useGroup</code> argument.
81      * <p>
82      * Example usage:
83      * <pre>
84      * RadioGroupFieldEditor editor= new RadioGroupFieldEditor(
85      * "GeneralPage.DoubleClick", resName, 1,
86      * new String[][] {
87      * {"Open Browser", "open"},
88      * {"Expand Tree", "expand"}
89      * },
90      * parent);
91      * </pre>
92      * </p>
93      *
94      * @param name the name of the preference this field editor works on
95      * @param labelText the label text of the field editor
96      * @param numColumns the number of columns for the radio button presentation
97      * @param labelAndValues list of radio button [label, value] entries;
98      * the value is returned when the radio button is selected
99      * @param parent the parent of the field editor's control
100      */

101     public RadioGroupFieldEditor(String JavaDoc name, String JavaDoc labelText, int numColumns,
102             String JavaDoc[][] labelAndValues, Composite parent) {
103         this(name, labelText, numColumns, labelAndValues, parent, false);
104     }
105
106     /**
107      * Creates a radio group field editor.
108      * <p>
109      * Example usage:
110      * <pre>
111      * RadioGroupFieldEditor editor= new RadioGroupFieldEditor(
112      * "GeneralPage.DoubleClick", resName, 1,
113      * new String[][] {
114      * {"Open Browser", "open"},
115      * {"Expand Tree", "expand"}
116      * },
117      * parent,
118      * true);
119      * </pre>
120      * </p>
121      *
122      * @param name the name of the preference this field editor works on
123      * @param labelText the label text of the field editor
124      * @param numColumns the number of columns for the radio button presentation
125      * @param labelAndValues list of radio button [label, value] entries;
126      * the value is returned when the radio button is selected
127      * @param parent the parent of the field editor's control
128      * @param useGroup whether to use a Group control to contain the radio buttons
129      */

130     public RadioGroupFieldEditor(String JavaDoc name, String JavaDoc labelText, int numColumns,
131             String JavaDoc[][] labelAndValues, Composite parent, boolean useGroup) {
132         init(name, labelText);
133         Assert.isTrue(checkArray(labelAndValues));
134         this.labelsAndValues = labelAndValues;
135         this.numColumns = numColumns;
136         this.useGroup = useGroup;
137         createControl(parent);
138     }
139
140     /* (non-Javadoc)
141      * Method declared on FieldEditor.
142      */

143     protected void adjustForNumColumns(int numColumns) {
144         Control control = getLabelControl();
145         if (control != null) {
146             ((GridData) control.getLayoutData()).horizontalSpan = numColumns;
147         }
148         ((GridData) radioBox.getLayoutData()).horizontalSpan = numColumns;
149     }
150
151     /**
152      * Checks whether given <code>String[][]</code> is of "type"
153      * <code>String[][2]</code>.
154      * @param table
155      *
156      * @return <code>true</code> if it is ok, and <code>false</code> otherwise
157      */

158     private boolean checkArray(String JavaDoc[][] table) {
159         if (table == null) {
160             return false;
161         }
162         for (int i = 0; i < table.length; i++) {
163             String JavaDoc[] array = table[i];
164             if (array == null || array.length != 2) {
165                 return false;
166             }
167         }
168         return true;
169     }
170
171     /* (non-Javadoc)
172      * Method declared on FieldEditor.
173      */

174     protected void doFillIntoGrid(Composite parent, int numColumns) {
175         if (useGroup) {
176             Control control = getRadioBoxControl(parent);
177             GridData gd = new GridData(GridData.FILL_HORIZONTAL);
178             control.setLayoutData(gd);
179         } else {
180             Control control = getLabelControl(parent);
181             GridData gd = new GridData();
182             gd.horizontalSpan = numColumns;
183             control.setLayoutData(gd);
184             control = getRadioBoxControl(parent);
185             gd = new GridData();
186             gd.horizontalSpan = numColumns;
187             gd.horizontalIndent = indent;
188             control.setLayoutData(gd);
189         }
190
191     }
192
193     /* (non-Javadoc)
194      * Method declared on FieldEditor.
195      */

196     protected void doLoad() {
197         updateValue(getPreferenceStore().getString(getPreferenceName()));
198     }
199
200     /* (non-Javadoc)
201      * Method declared on FieldEditor.
202      */

203     protected void doLoadDefault() {
204         updateValue(getPreferenceStore().getDefaultString(getPreferenceName()));
205     }
206
207     /* (non-Javadoc)
208      * Method declared on FieldEditor.
209      */

210     protected void doStore() {
211         if (value == null) {
212             getPreferenceStore().setToDefault(getPreferenceName());
213             return;
214         }
215
216         getPreferenceStore().setValue(getPreferenceName(), value);
217     }
218
219     /* (non-Javadoc)
220      * Method declared on FieldEditor.
221      */

222     public int getNumberOfControls() {
223         return 1;
224     }
225
226     /**
227      * Returns this field editor's radio group control.
228      * @param parent The parent to create the radioBox in
229      * @return the radio group control
230      */

231     public Composite getRadioBoxControl(Composite parent) {
232         if (radioBox == null) {
233
234             Font font = parent.getFont();
235
236             if (useGroup) {
237                 Group group = new Group(parent, SWT.NONE);
238                 group.setFont(font);
239                 String JavaDoc text = getLabelText();
240                 if (text != null) {
241                     group.setText(text);
242                 }
243                 radioBox = group;
244                 GridLayout layout = new GridLayout();
245                 layout.horizontalSpacing = HORIZONTAL_GAP;
246                 layout.numColumns = numColumns;
247                 radioBox.setLayout(layout);
248             } else {
249                 radioBox = new Composite(parent, SWT.NONE);
250                 GridLayout layout = new GridLayout();
251                 layout.marginWidth = 0;
252                 layout.marginHeight = 0;
253                 layout.horizontalSpacing = HORIZONTAL_GAP;
254                 layout.numColumns = numColumns;
255                 radioBox.setLayout(layout);
256                 radioBox.setFont(font);
257             }
258
259             radioButtons = new Button[labelsAndValues.length];
260             for (int i = 0; i < labelsAndValues.length; i++) {
261                 Button radio = new Button(radioBox, SWT.RADIO | SWT.LEFT);
262                 radioButtons[i] = radio;
263                 String JavaDoc[] labelAndValue = labelsAndValues[i];
264                 radio.setText(labelAndValue[0]);
265                 radio.setData(labelAndValue[1]);
266                 radio.setFont(font);
267                 radio.addSelectionListener(new SelectionAdapter() {
268                     public void widgetSelected(SelectionEvent event) {
269                         String JavaDoc oldValue = value;
270                         value = (String JavaDoc) event.widget.getData();
271                         setPresentsDefaultValue(false);
272                         fireValueChanged(VALUE, oldValue, value);
273                     }
274                 });
275             }
276             radioBox.addDisposeListener(new DisposeListener() {
277                 public void widgetDisposed(DisposeEvent event) {
278                     radioBox = null;
279                     radioButtons = null;
280                 }
281             });
282         } else {
283             checkParent(radioBox, parent);
284         }
285         return radioBox;
286     }
287
288     /**
289      * Sets the indent used for the first column of the radion button matrix.
290      *
291      * @param indent the indent (in pixels)
292      */

293     public void setIndent(int indent) {
294         if (indent < 0) {
295             this.indent = 0;
296         } else {
297             this.indent = indent;
298         }
299     }
300
301     /**
302      * Select the radio button that conforms to the given value.
303      *
304      * @param selectedValue the selected value
305      */

306     private void updateValue(String JavaDoc selectedValue) {
307         this.value = selectedValue;
308         if (radioButtons == null) {
309             return;
310         }
311
312         if (this.value != null) {
313             boolean found = false;
314             for (int i = 0; i < radioButtons.length; i++) {
315                 Button radio = radioButtons[i];
316                 boolean selection = false;
317                 if (((String JavaDoc) radio.getData()).equals(this.value)) {
318                     selection = true;
319                     found = true;
320                 }
321                 radio.setSelection(selection);
322             }
323             if (found) {
324                 return;
325             }
326         }
327
328         // We weren't able to find the value. So we select the first
329
// radio button as a default.
330
if (radioButtons.length > 0) {
331             radioButtons[0].setSelection(true);
332             this.value = (String JavaDoc) radioButtons[0].getData();
333         }
334         return;
335     }
336
337     /*
338      * @see FieldEditor.setEnabled(boolean,Composite).
339      */

340     public void setEnabled(boolean enabled, Composite parent) {
341         if (!useGroup) {
342             super.setEnabled(enabled, parent);
343         }
344         for (int i = 0; i < radioButtons.length; i++) {
345             radioButtons[i].setEnabled(enabled);
346         }
347
348     }
349 }
350
Popular Tags