comparison org.eclipse.jface/src/org/eclipse/jface/preference/RadioGroupFieldEditor.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
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 org.eclipse.jface.preference.RadioGroupFieldEditor;
14
15 import org.eclipse.jface.preference.FieldEditor;
16
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.events.DisposeEvent;
19 import org.eclipse.swt.events.DisposeListener;
20 import org.eclipse.swt.events.SelectionAdapter;
21 import org.eclipse.swt.events.SelectionEvent;
22 import org.eclipse.swt.graphics.Font;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.Button;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Control;
28 import org.eclipse.swt.widgets.Group;
29 import org.eclipse.core.runtime.Assert;
30
31 import java.lang.all;
32 import java.util.List;
33 import java.util.Set;
34
35 /**
36 * A field editor for an enumeration type preference.
37 * The choices are presented as a list of radio buttons.
38 */
39 public class RadioGroupFieldEditor : FieldEditor {
40
41 /**
42 * List of radio button entries of the form [label,value].
43 */
44 private String[][] labelsAndValues;
45
46 /**
47 * Number of columns into which to arrange the radio buttons.
48 */
49 private int numColumns;
50
51 /**
52 * Indent used for the first column of the radion button matrix.
53 */
54 private int indent = HORIZONTAL_GAP;
55
56 /**
57 * The current value, or <code>null</code> if none.
58 */
59 private String value;
60
61 /**
62 * The box of radio buttons, or <code>null</code> if none
63 * (before creation and after disposal).
64 */
65 private Composite radioBox;
66
67 /**
68 * The radio buttons, or <code>null</code> if none
69 * (before creation and after disposal).
70 */
71 private Button[] radioButtons;
72
73 /**
74 * Whether to use a Group control.
75 */
76 private bool useGroup;
77
78 /**
79 * Creates a new radio group field editor
80 */
81 protected this() {
82 }
83
84 /**
85 * Creates a radio group field editor.
86 * This constructor does not use a <code>Group</code> to contain the radio buttons.
87 * It is equivalent to using the following constructor with <code>false</code>
88 * for the <code>useGroup</code> argument.
89 * <p>
90 * Example usage:
91 * <pre>
92 * RadioGroupFieldEditor editor= new RadioGroupFieldEditor(
93 * "GeneralPage.DoubleClick", resName, 1,
94 * new String[][] {
95 * {"Open Browser", "open"},
96 * {"Expand Tree", "expand"}
97 * },
98 * parent);
99 * </pre>
100 * </p>
101 *
102 * @param name the name of the preference this field editor works on
103 * @param labelText the label text of the field editor
104 * @param numColumns the number of columns for the radio button presentation
105 * @param labelAndValues list of radio button [label, value] entries;
106 * the value is returned when the radio button is selected
107 * @param parent the parent of the field editor's control
108 */
109 public this(String name, String labelText, int numColumns,
110 String[][] labelAndValues, Composite parent) {
111 this(name, labelText, numColumns, labelAndValues, parent, false);
112 }
113
114 /**
115 * Creates a radio group field editor.
116 * <p>
117 * Example usage:
118 * <pre>
119 * RadioGroupFieldEditor editor= new RadioGroupFieldEditor(
120 * "GeneralPage.DoubleClick", resName, 1,
121 * new String[][] {
122 * {"Open Browser", "open"},
123 * {"Expand Tree", "expand"}
124 * },
125 * parent,
126 * true);
127 * </pre>
128 * </p>
129 *
130 * @param name the name of the preference this field editor works on
131 * @param labelText the label text of the field editor
132 * @param numColumns the number of columns for the radio button presentation
133 * @param labelAndValues list of radio button [label, value] entries;
134 * the value is returned when the radio button is selected
135 * @param parent the parent of the field editor's control
136 * @param useGroup whether to use a Group control to contain the radio buttons
137 */
138 public this(String name, String labelText, int numColumns,
139 String[][] labelAndValues, Composite parent, bool useGroup) {
140 init(name, labelText);
141 Assert.isTrue(checkArray(labelAndValues));
142 this.labelsAndValues = labelAndValues;
143 this.numColumns = numColumns;
144 this.useGroup = useGroup;
145 createControl(parent);
146 }
147
148 /* (non-Javadoc)
149 * Method declared on FieldEditor.
150 */
151 protected override void adjustForNumColumns(int numColumns) {
152 Control control = getLabelControl();
153 if (control !is null) {
154 (cast(GridData) control.getLayoutData()).horizontalSpan = numColumns;
155 }
156 (cast(GridData) radioBox.getLayoutData()).horizontalSpan = numColumns;
157 }
158
159 /**
160 * Checks whether given <code>String[][]</code> is of "type"
161 * <code>String[][2]</code>.
162 * @param table
163 *
164 * @return <code>true</code> if it is ok, and <code>false</code> otherwise
165 */
166 private bool checkArray(String[][] table) {
167 if (table is null) {
168 return false;
169 }
170 for (int i = 0; i < table.length; i++) {
171 String[] array = table[i];
172 if (array is null || array.length !is 2) {
173 return false;
174 }
175 }
176 return true;
177 }
178
179 /* (non-Javadoc)
180 * Method declared on FieldEditor.
181 */
182 protected override void doFillIntoGrid(Composite parent, int numColumns) {
183 if (useGroup) {
184 Control control = getRadioBoxControl(parent);
185 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
186 control.setLayoutData(gd);
187 } else {
188 Control control = getLabelControl(parent);
189 GridData gd = new GridData();
190 gd.horizontalSpan = numColumns;
191 control.setLayoutData(gd);
192 control = getRadioBoxControl(parent);
193 gd = new GridData();
194 gd.horizontalSpan = numColumns;
195 gd.horizontalIndent = indent;
196 control.setLayoutData(gd);
197 }
198
199 }
200
201 /* (non-Javadoc)
202 * Method declared on FieldEditor.
203 */
204 protected override void doLoad() {
205 updateValue(getPreferenceStore().getString(getPreferenceName()));
206 }
207
208 /* (non-Javadoc)
209 * Method declared on FieldEditor.
210 */
211 protected override void doLoadDefault() {
212 updateValue(getPreferenceStore().getDefaultString(getPreferenceName()));
213 }
214
215 /* (non-Javadoc)
216 * Method declared on FieldEditor.
217 */
218 protected override void doStore() {
219 if (value is null) {
220 getPreferenceStore().setToDefault(getPreferenceName());
221 return;
222 }
223
224 getPreferenceStore().setValue(getPreferenceName(), value);
225 }
226
227 /* (non-Javadoc)
228 * Method declared on FieldEditor.
229 */
230 public override int getNumberOfControls() {
231 return 1;
232 }
233
234 /**
235 * Returns this field editor's radio group control.
236 * @param parent The parent to create the radioBox in
237 * @return the radio group control
238 */
239 public Composite getRadioBoxControl(Composite parent) {
240 if (radioBox is null) {
241
242 Font font = parent.getFont();
243
244 if (useGroup) {
245 Group group = new Group(parent, SWT.NONE);
246 group.setFont(font);
247 String text = getLabelText();
248 if (text !is null) {
249 group.setText(text);
250 }
251 radioBox = group;
252 GridLayout layout = new GridLayout();
253 layout.horizontalSpacing = HORIZONTAL_GAP;
254 layout.numColumns = numColumns;
255 radioBox.setLayout(layout);
256 } else {
257 radioBox = new Composite(parent, SWT.NONE);
258 GridLayout layout = new GridLayout();
259 layout.marginWidth = 0;
260 layout.marginHeight = 0;
261 layout.horizontalSpacing = HORIZONTAL_GAP;
262 layout.numColumns = numColumns;
263 radioBox.setLayout(layout);
264 radioBox.setFont(font);
265 }
266
267 radioButtons = new Button[labelsAndValues.length];
268 for (int i = 0; i < labelsAndValues.length; i++) {
269 Button radio = new Button(radioBox, SWT.RADIO | SWT.LEFT);
270 radioButtons[i] = radio;
271 String[] labelAndValue = labelsAndValues[i];
272 radio.setText(labelAndValue[0]);
273 radio.setData(stringcast(labelAndValue[1]));
274 radio.setFont(font);
275 radio.addSelectionListener(new class SelectionAdapter {
276 public void widgetSelected(SelectionEvent event) {
277 String oldValue = value;
278 value = stringcast( event.widget.getData() );
279 setPresentsDefaultValue(false);
280 fireValueChanged(VALUE, stringcast(oldValue), stringcast(value));
281 }
282 });
283 }
284 radioBox.addDisposeListener(new class DisposeListener {
285 public void widgetDisposed(DisposeEvent event) {
286 radioBox = null;
287 radioButtons = null;
288 }
289 });
290 } else {
291 checkParent(radioBox, parent);
292 }
293 return radioBox;
294 }
295
296 /**
297 * Sets the indent used for the first column of the radion button matrix.
298 *
299 * @param indent the indent (in pixels)
300 */
301 public void setIndent(int indent) {
302 if (indent < 0) {
303 this.indent = 0;
304 } else {
305 this.indent = indent;
306 }
307 }
308
309 /**
310 * Select the radio button that conforms to the given value.
311 *
312 * @param selectedValue the selected value
313 */
314 private void updateValue(String selectedValue) {
315 this.value = selectedValue;
316 if (radioButtons is null) {
317 return;
318 }
319
320 if (this.value !is null) {
321 bool found = false;
322 for (int i = 0; i < radioButtons.length; i++) {
323 Button radio = radioButtons[i];
324 bool selection = false;
325 if (stringcast( radio.getData()).equals(this.value)) {
326 selection = true;
327 found = true;
328 }
329 radio.setSelection(selection);
330 }
331 if (found) {
332 return;
333 }
334 }
335
336 // We weren't able to find the value. So we select the first
337 // radio button as a default.
338 if (radioButtons.length > 0) {
339 radioButtons[0].setSelection(true);
340 this.value = stringcast( radioButtons[0].getData());
341 }
342 return;
343 }
344
345 /*
346 * @see FieldEditor.setEnabled(bool,Composite).
347 */
348 public override void setEnabled(bool enabled, Composite parent) {
349 if (!useGroup) {
350 super.setEnabled(enabled, parent);
351 }
352 for (int i = 0; i < radioButtons.length; i++) {
353 radioButtons[i].setEnabled(enabled);
354 }
355
356 }
357 }