Mercurial > projects > dwt-addons
comparison dwtx/jface/preference/BooleanFieldEditor.d @ 34:b3c8e32d406f
preference
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 05 Apr 2008 01:45:47 +0200 |
parents | |
children | ea8ff534f622 |
comparison
equal
deleted
inserted
replaced
33:f25582573129 | 34:b3c8e32d406f |
---|---|
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 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module dwtx.jface.preference.BooleanFieldEditor; | |
14 | |
15 import dwtx.jface.preference.FieldEditor; | |
16 | |
17 import dwt.DWT; | |
18 import dwt.events.DisposeEvent; | |
19 import dwt.events.DisposeListener; | |
20 import dwt.events.SelectionAdapter; | |
21 import dwt.events.SelectionEvent; | |
22 import dwt.layout.GridData; | |
23 import dwt.widgets.Button; | |
24 import dwt.widgets.Composite; | |
25 import dwt.widgets.Label; | |
26 | |
27 import dwt.dwthelper.utils; | |
28 | |
29 /** | |
30 * A field editor for a bool type preference. | |
31 */ | |
32 public class BooleanFieldEditor : FieldEditor { | |
33 | |
34 /** | |
35 * Style constant (value <code>0</code>) indicating the default | |
36 * layout where the field editor's check box appears to the left | |
37 * of the label. | |
38 */ | |
39 public static const int DEFAULT = 0; | |
40 | |
41 /** | |
42 * Style constant (value <code>1</code>) indicating a layout | |
43 * where the field editor's label appears on the left | |
44 * with a check box on the right. | |
45 */ | |
46 public static const int SEPARATE_LABEL = 1; | |
47 | |
48 /** | |
49 * Style bits. Either <code>DEFAULT</code> or | |
50 * <code>SEPARATE_LABEL</code>. | |
51 */ | |
52 private int style; | |
53 | |
54 /** | |
55 * The previously selected, or "before", value. | |
56 */ | |
57 private bool wasSelected; | |
58 | |
59 /** | |
60 * The checkbox control, or <code>null</code> if none. | |
61 */ | |
62 private Button checkBox = null; | |
63 | |
64 /** | |
65 * Creates a new bool field editor | |
66 */ | |
67 protected this() { | |
68 } | |
69 | |
70 /** | |
71 * Creates a bool field editor in the given style. | |
72 * | |
73 * @param name the name of the preference this field editor works on | |
74 * @param labelText the label text of the field editor | |
75 * @param style the style, either <code>DEFAULT</code> or | |
76 * <code>SEPARATE_LABEL</code> | |
77 * @param parent the parent of the field editor's control | |
78 * @see #DEFAULT | |
79 * @see #SEPARATE_LABEL | |
80 */ | |
81 public this(String name, String labelText, int style, | |
82 Composite parent) { | |
83 init(name, labelText); | |
84 this.style = style; | |
85 createControl(parent); | |
86 } | |
87 | |
88 /** | |
89 * Creates a bool field editor in the default style. | |
90 * | |
91 * @param name the name of the preference this field editor works on | |
92 * @param label the label text of the field editor | |
93 * @param parent the parent of the field editor's control | |
94 */ | |
95 public this(String name, String label, Composite parent) { | |
96 this(name, label, DEFAULT, parent); | |
97 } | |
98 | |
99 /* (non-Javadoc) | |
100 * Method declared on FieldEditor. | |
101 */ | |
102 protected void adjustForNumColumns(int numColumns) { | |
103 if (style is SEPARATE_LABEL) { | |
104 numColumns--; | |
105 } | |
106 (cast(GridData) checkBox.getLayoutData()).horizontalSpan = numColumns; | |
107 } | |
108 | |
109 /* (non-Javadoc) | |
110 * Method declared on FieldEditor. | |
111 */ | |
112 protected void doFillIntoGrid(Composite parent, int numColumns) { | |
113 String text = getLabelText(); | |
114 switch (style) { | |
115 case SEPARATE_LABEL: | |
116 getLabelControl(parent); | |
117 numColumns--; | |
118 text = null; | |
119 default: | |
120 checkBox = getChangeControl(parent); | |
121 GridData gd = new GridData(); | |
122 gd.horizontalSpan = numColumns; | |
123 checkBox.setLayoutData(gd); | |
124 if (text !is null) { | |
125 checkBox.setText(text); | |
126 } | |
127 } | |
128 } | |
129 | |
130 /* (non-Javadoc) | |
131 * Method declared on FieldEditor. | |
132 * Loads the value from the preference store and sets it to | |
133 * the check box. | |
134 */ | |
135 protected void doLoad() { | |
136 if (checkBox !is null) { | |
137 bool value = getPreferenceStore() | |
138 .getBoolean(getPreferenceName()); | |
139 checkBox.setSelection(value); | |
140 wasSelected = value; | |
141 } | |
142 } | |
143 | |
144 /* (non-Javadoc) | |
145 * Method declared on FieldEditor. | |
146 * Loads the default value from the preference store and sets it to | |
147 * the check box. | |
148 */ | |
149 protected void doLoadDefault() { | |
150 if (checkBox !is null) { | |
151 bool value = getPreferenceStore().getDefaultBoolean( | |
152 getPreferenceName()); | |
153 checkBox.setSelection(value); | |
154 wasSelected = value; | |
155 } | |
156 } | |
157 | |
158 /* (non-Javadoc) | |
159 * Method declared on FieldEditor. | |
160 */ | |
161 protected void doStore() { | |
162 getPreferenceStore().setValue(getPreferenceName(), | |
163 checkBox.getSelection()); | |
164 } | |
165 | |
166 /** | |
167 * Returns this field editor's current value. | |
168 * | |
169 * @return the value | |
170 */ | |
171 public bool getBooleanValue() { | |
172 return checkBox.getSelection(); | |
173 } | |
174 | |
175 /** | |
176 * Returns the change button for this field editor. | |
177 * @param parent The Composite to create the receiver in. | |
178 * | |
179 * @return the change button | |
180 */ | |
181 protected Button getChangeControl(Composite parent) { | |
182 if (checkBox is null) { | |
183 checkBox = new Button(parent, DWT.CHECK | DWT.LEFT); | |
184 checkBox.setFont(parent.getFont()); | |
185 checkBox.addSelectionListener(new class SelectionAdapter { | |
186 public void widgetSelected(SelectionEvent e) { | |
187 bool isSelected = checkBox.getSelection(); | |
188 valueChanged(wasSelected, isSelected); | |
189 wasSelected = isSelected; | |
190 } | |
191 }); | |
192 checkBox.addDisposeListener(new class DisposeListener { | |
193 public void widgetDisposed(DisposeEvent event) { | |
194 checkBox = null; | |
195 } | |
196 }); | |
197 } else { | |
198 checkParent(checkBox, parent); | |
199 } | |
200 return checkBox; | |
201 } | |
202 | |
203 /* (non-Javadoc) | |
204 * Method declared on FieldEditor. | |
205 */ | |
206 public int getNumberOfControls() { | |
207 switch (style) { | |
208 case SEPARATE_LABEL: | |
209 return 2; | |
210 default: | |
211 return 1; | |
212 } | |
213 } | |
214 | |
215 /* (non-Javadoc) | |
216 * Method declared on FieldEditor. | |
217 */ | |
218 public void setFocus() { | |
219 if (checkBox !is null) { | |
220 checkBox.setFocus(); | |
221 } | |
222 } | |
223 | |
224 /* (non-Javadoc) | |
225 * Method declared on FieldEditor. | |
226 */ | |
227 public void setLabelText(String text) { | |
228 super.setLabelText(text); | |
229 Label label = getLabelControl(); | |
230 if (label is null && checkBox !is null) { | |
231 checkBox.setText(text); | |
232 } | |
233 } | |
234 | |
235 /** | |
236 * Informs this field editor's listener, if it has one, about a change | |
237 * to the value (<code>VALUE</code> property) provided that the old and | |
238 * new values are different. | |
239 * | |
240 * @param oldValue the old value | |
241 * @param newValue the new value | |
242 */ | |
243 protected void valueChanged(bool oldValue, bool newValue) { | |
244 setPresentsDefaultValue(false); | |
245 if (oldValue !is newValue) { | |
246 fireStateChanged(VALUE, oldValue, newValue); | |
247 } | |
248 } | |
249 | |
250 /* | |
251 * @see FieldEditor.setEnabled | |
252 */ | |
253 public void setEnabled(bool enabled, Composite parent) { | |
254 //Only call super if there is a label already | |
255 if (style is SEPARATE_LABEL) { | |
256 super.setEnabled(enabled, parent); | |
257 } | |
258 getChangeControl(parent).setEnabled(enabled); | |
259 } | |
260 | |
261 } |