comparison dwtx/jface/preference/ComboFieldEditor.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.ComboFieldEditor;
14
15 import dwtx.jface.preference.FieldEditor;
16
17 import dwt.DWT;
18 import dwt.events.SelectionAdapter;
19 import dwt.events.SelectionEvent;
20 import dwt.layout.GridData;
21 import dwt.widgets.Combo;
22 import dwt.widgets.Composite;
23 import dwt.widgets.Control;
24 import dwtx.core.runtime.Assert;
25
26 import dwt.dwthelper.utils;
27
28 /**
29 * A field editor for a combo box that allows the drop-down selection of one of
30 * a list of items.
31 *
32 * @since 3.3
33 */
34 public class ComboFieldEditor : FieldEditor {
35
36 /**
37 * The <code>Combo</code> widget.
38 */
39 private Combo fCombo;
40
41 /**
42 * The value (not the name) of the currently selected item in the Combo widget.
43 */
44 private String fValue;
45
46 /**
47 * The names (labels) and underlying values to populate the combo widget. These should be
48 * arranged as: { {name1, value1}, {name2, value2}, ...}
49 */
50 private String[][] fEntryNamesAndValues;
51
52 /**
53 * Create the combo box field editor.
54 *
55 * @param name the name of the preference this field editor works on
56 * @param labelText the label text of the field editor
57 * @param entryNamesAndValues the names (labels) and underlying values to populate the combo widget. These should be
58 * arranged as: { {name1, value1}, {name2, value2}, ...}
59 * @param parent the parent composite
60 */
61 public this(String name, String labelText, String[][] entryNamesAndValues, Composite parent) {
62 init(name, labelText);
63 Assert.isTrue(checkArray(entryNamesAndValues));
64 fEntryNamesAndValues = entryNamesAndValues;
65 createControl(parent);
66 }
67
68 /**
69 * Checks whether given <code>String[][]</code> is of "type"
70 * <code>String[][2]</code>.
71 *
72 * @return <code>true</code> if it is ok, and <code>false</code> otherwise
73 */
74 private bool checkArray(String[][] table) {
75 if (table is null) {
76 return false;
77 }
78 for (int i = 0; i < table.length; i++) {
79 String[] array = table[i];
80 if (array is null || array.length !is 2) {
81 return false;
82 }
83 }
84 return true;
85 }
86
87 /* (non-Javadoc)
88 * @see dwtx.jface.preference.FieldEditor#adjustForNumColumns(int)
89 */
90 protected void adjustForNumColumns(int numColumns) {
91 if (numColumns > 1) {
92 Control control = getLabelControl();
93 int left = numColumns;
94 if (control !is null) {
95 (cast(GridData)control.getLayoutData()).horizontalSpan = 1;
96 left = left - 1;
97 }
98 (cast(GridData)fCombo.getLayoutData()).horizontalSpan = left;
99 } else {
100 Control control = getLabelControl();
101 if (control !is null) {
102 (cast(GridData)control.getLayoutData()).horizontalSpan = 1;
103 }
104 (cast(GridData)fCombo.getLayoutData()).horizontalSpan = 1;
105 }
106 }
107
108 /* (non-Javadoc)
109 * @see dwtx.jface.preference.FieldEditor#doFillIntoGrid(dwt.widgets.Composite, int)
110 */
111 protected void doFillIntoGrid(Composite parent, int numColumns) {
112 int comboC = 1;
113 if (numColumns > 1) {
114 comboC = numColumns - 1;
115 }
116 Control control = getLabelControl(parent);
117 GridData gd = new GridData();
118 gd.horizontalSpan = 1;
119 control.setLayoutData(gd);
120 control = getComboBoxControl(parent);
121 gd = new GridData();
122 gd.horizontalSpan = comboC;
123 gd.horizontalAlignment = GridData.FILL;
124 control.setLayoutData(gd);
125 control.setFont(parent.getFont());
126 }
127
128 /* (non-Javadoc)
129 * @see dwtx.jface.preference.FieldEditor#doLoad()
130 */
131 protected void doLoad() {
132 updateComboForValue(getPreferenceStore().getString(getPreferenceName()));
133 }
134
135 /* (non-Javadoc)
136 * @see dwtx.jface.preference.FieldEditor#doLoadDefault()
137 */
138 protected void doLoadDefault() {
139 updateComboForValue(getPreferenceStore().getDefaultString(getPreferenceName()));
140 }
141
142 /* (non-Javadoc)
143 * @see dwtx.jface.preference.FieldEditor#doStore()
144 */
145 protected void doStore() {
146 if (fValue is null) {
147 getPreferenceStore().setToDefault(getPreferenceName());
148 return;
149 }
150 getPreferenceStore().setValue(getPreferenceName(), fValue);
151 }
152
153 /* (non-Javadoc)
154 * @see dwtx.jface.preference.FieldEditor#getNumberOfControls()
155 */
156 public int getNumberOfControls() {
157 return 2;
158 }
159
160 /*
161 * Lazily create and return the Combo control.
162 */
163 private Combo getComboBoxControl(Composite parent) {
164 if (fCombo is null) {
165 fCombo = new Combo(parent, DWT.READ_ONLY);
166 fCombo.setFont(parent.getFont());
167 for (int i = 0; i < fEntryNamesAndValues.length; i++) {
168 fCombo.add(fEntryNamesAndValues[i][0], i);
169 }
170
171 fCombo.addSelectionListener(new class SelectionAdapter {
172 public void widgetSelected(SelectionEvent evt) {
173 String oldValue = fValue;
174 String name = fCombo.getText();
175 fValue = getValueForName(name);
176 setPresentsDefaultValue(false);
177 fireValueChanged(VALUE, stringcast(oldValue), stringcast(fValue));
178 }
179 });
180 }
181 return fCombo;
182 }
183
184 /*
185 * Given the name (label) of an entry, return the corresponding value.
186 */
187 private String getValueForName(String name) {
188 for (int i = 0; i < fEntryNamesAndValues.length; i++) {
189 String[] entry = fEntryNamesAndValues[i];
190 if (name.equals(entry[0])) {
191 return entry[1];
192 }
193 }
194 return fEntryNamesAndValues[0][0];
195 }
196
197 /*
198 * Set the name in the combo widget to match the specified value.
199 */
200 private void updateComboForValue(String value) {
201 fValue = value;
202 for (int i = 0; i < fEntryNamesAndValues.length; i++) {
203 if (value.equals(fEntryNamesAndValues[i][1])) {
204 fCombo.setText(fEntryNamesAndValues[i][0]);
205 return;
206 }
207 }
208 if (fEntryNamesAndValues.length > 0) {
209 fValue = fEntryNamesAndValues[0][1];
210 fCombo.setText(fEntryNamesAndValues[0][0]);
211 }
212 }
213 }