KickJava   Java API By Example, From Geeks To Geeks.

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


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.swt.SWT;
14 import org.eclipse.swt.events.DisposeEvent;
15 import org.eclipse.swt.events.DisposeListener;
16 import org.eclipse.swt.events.SelectionAdapter;
17 import org.eclipse.swt.events.SelectionEvent;
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.Label;
22
23 /**
24  * A field editor for a boolean type preference.
25  */

26 public class BooleanFieldEditor extends FieldEditor {
27
28     /**
29      * Style constant (value <code>0</code>) indicating the default
30      * layout where the field editor's check box appears to the left
31      * of the label.
32      */

33     public static final int DEFAULT = 0;
34
35     /**
36      * Style constant (value <code>1</code>) indicating a layout
37      * where the field editor's label appears on the left
38      * with a check box on the right.
39      */

40     public static final int SEPARATE_LABEL = 1;
41
42     /**
43      * Style bits. Either <code>DEFAULT</code> or
44      * <code>SEPARATE_LABEL</code>.
45      */

46     private int style;
47
48     /**
49      * The previously selected, or "before", value.
50      */

51     private boolean wasSelected;
52
53     /**
54      * The checkbox control, or <code>null</code> if none.
55      */

56     private Button checkBox = null;
57
58     /**
59      * Creates a new boolean field editor
60      */

61     protected BooleanFieldEditor() {
62     }
63
64     /**
65      * Creates a boolean field editor in the given style.
66      *
67      * @param name the name of the preference this field editor works on
68      * @param labelText the label text of the field editor
69      * @param style the style, either <code>DEFAULT</code> or
70      * <code>SEPARATE_LABEL</code>
71      * @param parent the parent of the field editor's control
72      * @see #DEFAULT
73      * @see #SEPARATE_LABEL
74      */

75     public BooleanFieldEditor(String JavaDoc name, String JavaDoc labelText, int style,
76             Composite parent) {
77         init(name, labelText);
78         this.style = style;
79         createControl(parent);
80     }
81
82     /**
83      * Creates a boolean field editor in the default style.
84      *
85      * @param name the name of the preference this field editor works on
86      * @param label the label text of the field editor
87      * @param parent the parent of the field editor's control
88      */

89     public BooleanFieldEditor(String JavaDoc name, String JavaDoc label, Composite parent) {
90         this(name, label, DEFAULT, parent);
91     }
92
93     /* (non-Javadoc)
94      * Method declared on FieldEditor.
95      */

96     protected void adjustForNumColumns(int numColumns) {
97         if (style == SEPARATE_LABEL) {
98             numColumns--;
99         }
100         ((GridData) checkBox.getLayoutData()).horizontalSpan = numColumns;
101     }
102
103     /* (non-Javadoc)
104      * Method declared on FieldEditor.
105      */

106     protected void doFillIntoGrid(Composite parent, int numColumns) {
107         String JavaDoc text = getLabelText();
108         switch (style) {
109         case SEPARATE_LABEL:
110             getLabelControl(parent);
111             numColumns--;
112             text = null;
113         default:
114             checkBox = getChangeControl(parent);
115             GridData gd = new GridData();
116             gd.horizontalSpan = numColumns;
117             checkBox.setLayoutData(gd);
118             if (text != null) {
119                 checkBox.setText(text);
120             }
121         }
122     }
123
124     /* (non-Javadoc)
125      * Method declared on FieldEditor.
126      * Loads the value from the preference store and sets it to
127      * the check box.
128      */

129     protected void doLoad() {
130         if (checkBox != null) {
131             boolean value = getPreferenceStore()
132                     .getBoolean(getPreferenceName());
133             checkBox.setSelection(value);
134             wasSelected = value;
135         }
136     }
137
138     /* (non-Javadoc)
139      * Method declared on FieldEditor.
140      * Loads the default value from the preference store and sets it to
141      * the check box.
142      */

143     protected void doLoadDefault() {
144         if (checkBox != null) {
145             boolean value = getPreferenceStore().getDefaultBoolean(
146                     getPreferenceName());
147             checkBox.setSelection(value);
148             wasSelected = value;
149         }
150     }
151
152     /* (non-Javadoc)
153      * Method declared on FieldEditor.
154      */

155     protected void doStore() {
156         getPreferenceStore().setValue(getPreferenceName(),
157                 checkBox.getSelection());
158     }
159
160     /**
161      * Returns this field editor's current value.
162      *
163      * @return the value
164      */

165     public boolean getBooleanValue() {
166         return checkBox.getSelection();
167     }
168
169     /**
170      * Returns the change button for this field editor.
171      * @param parent The Composite to create the receiver in.
172      *
173      * @return the change button
174      */

175     protected Button getChangeControl(Composite parent) {
176         if (checkBox == null) {
177             checkBox = new Button(parent, SWT.CHECK | SWT.LEFT);
178             checkBox.setFont(parent.getFont());
179             checkBox.addSelectionListener(new SelectionAdapter() {
180                 public void widgetSelected(SelectionEvent e) {
181                     boolean isSelected = checkBox.getSelection();
182                     valueChanged(wasSelected, isSelected);
183                     wasSelected = isSelected;
184                 }
185             });
186             checkBox.addDisposeListener(new DisposeListener() {
187                 public void widgetDisposed(DisposeEvent event) {
188                     checkBox = null;
189                 }
190             });
191         } else {
192             checkParent(checkBox, parent);
193         }
194         return checkBox;
195     }
196
197     /* (non-Javadoc)
198      * Method declared on FieldEditor.
199      */

200     public int getNumberOfControls() {
201         switch (style) {
202         case SEPARATE_LABEL:
203             return 2;
204         default:
205             return 1;
206         }
207     }
208
209     /* (non-Javadoc)
210      * Method declared on FieldEditor.
211      */

212     public void setFocus() {
213         if (checkBox != null) {
214             checkBox.setFocus();
215         }
216     }
217
218     /* (non-Javadoc)
219      * Method declared on FieldEditor.
220      */

221     public void setLabelText(String JavaDoc text) {
222         super.setLabelText(text);
223         Label label = getLabelControl();
224         if (label == null && checkBox != null) {
225             checkBox.setText(text);
226         }
227     }
228
229     /**
230      * Informs this field editor's listener, if it has one, about a change
231      * to the value (<code>VALUE</code> property) provided that the old and
232      * new values are different.
233      *
234      * @param oldValue the old value
235      * @param newValue the new value
236      */

237     protected void valueChanged(boolean oldValue, boolean newValue) {
238         setPresentsDefaultValue(false);
239         if (oldValue != newValue) {
240             fireStateChanged(VALUE, oldValue, newValue);
241         }
242     }
243
244     /*
245      * @see FieldEditor.setEnabled
246      */

247     public void setEnabled(boolean enabled, Composite parent) {
248         //Only call super if there is a label already
249
if (style == SEPARATE_LABEL) {
250             super.setEnabled(enabled, parent);
251         }
252         getChangeControl(parent).setEnabled(enabled);
253     }
254
255 }
256
Popular Tags