Mercurial > projects > dwt-addons
annotate dwtx/jface/viewers/ComboBoxCellEditor.d @ 90:7ffeace6c47f
Update 3.4M7 to 3.4
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 06 Jul 2008 23:30:07 +0200 |
parents | 5df4896124c7 |
children |
rev | line source |
---|---|
10 | 1 /******************************************************************************* |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
2 * Copyright (c) 2000, 2008 IBM Corporation and others. |
10 | 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 | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
10 * Tom Schindl <tom.schindl@bestsolution.at> - bugfix in 174739 |
10 | 11 * Port to the D programming language: |
12 * Frank Benoit <benoit@tionex.de> | |
13 *******************************************************************************/ | |
14 | |
15 module dwtx.jface.viewers.ComboBoxCellEditor; | |
16 | |
17 import dwtx.jface.viewers.CellEditor; | |
72
5df4896124c7
JFace and its examples do compile
Frank Benoit <benoit@tionex.de>
parents:
70
diff
changeset
|
18 import dwtx.jface.viewers.AbstractComboBoxCellEditor; |
10 | 19 |
20 import dwt.DWT; | |
21 import dwt.custom.CCombo; | |
22 import dwt.events.FocusAdapter; | |
23 import dwt.events.FocusEvent; | |
24 import dwt.events.KeyAdapter; | |
25 import dwt.events.KeyEvent; | |
26 import dwt.events.SelectionAdapter; | |
27 import dwt.events.SelectionEvent; | |
28 import dwt.events.TraverseEvent; | |
29 import dwt.events.TraverseListener; | |
30 import dwt.graphics.GC; | |
31 import dwt.widgets.Composite; | |
32 import dwt.widgets.Control; | |
33 import dwtx.core.runtime.Assert; | |
34 | |
35 import dwt.dwthelper.utils; | |
36 import tango.text.convert.Format; | |
37 | |
38 /** | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
39 * A cell editor that presents a list of items in a combo box. The cell editor's |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
40 * value is the zero-based index of the selected item. |
10 | 41 * <p> |
42 * This class may be instantiated; it is not intended to be subclassed. | |
43 * </p> | |
90 | 44 * @noextend This class is not intended to be subclassed by clients. |
10 | 45 */ |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
46 public class ComboBoxCellEditor : AbstractComboBoxCellEditor { |
10 | 47 |
48 /** | |
49 * The list of items to present in the combo box. | |
50 */ | |
51 private String[] items; | |
52 | |
53 /** | |
54 * The zero-based index of the selected item. | |
55 */ | |
56 int selection; | |
57 | |
58 /** | |
59 * The custom combo box control. | |
60 */ | |
61 CCombo comboBox; | |
62 | |
63 /** | |
64 * Default ComboBoxCellEditor style | |
65 */ | |
66 private static const int defaultStyle = DWT.NONE; | |
67 | |
68 /** | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
69 * Creates a new cell editor with no control and no st of choices. |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
70 * Initially, the cell editor has no cell validator. |
10 | 71 * |
72 * @since 2.1 | |
73 * @see CellEditor#setStyle | |
74 * @see CellEditor#create | |
75 * @see ComboBoxCellEditor#setItems | |
76 * @see CellEditor#dispose | |
77 */ | |
78 public this() { | |
79 setStyle(defaultStyle); | |
80 } | |
81 | |
82 /** | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
83 * Creates a new cell editor with a combo containing the given list of |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
84 * choices and parented under the given control. The cell editor value is |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
85 * the zero-based index of the selected item. Initially, the cell editor has |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
86 * no cell validator and the first item in the list is selected. |
10 | 87 * |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
88 * @param parent |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
89 * the parent control |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
90 * @param items |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
91 * the list of strings for the combo box |
10 | 92 */ |
93 public this(Composite parent, String[] items) { | |
94 this(parent, items, defaultStyle); | |
95 } | |
96 | |
97 /** | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
98 * Creates a new cell editor with a combo containing the given list of |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
99 * choices and parented under the given control. The cell editor value is |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
100 * the zero-based index of the selected item. Initially, the cell editor has |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
101 * no cell validator and the first item in the list is selected. |
10 | 102 * |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
103 * @param parent |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
104 * the parent control |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
105 * @param items |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
106 * the list of strings for the combo box |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
107 * @param style |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
108 * the style bits |
10 | 109 * @since 2.1 |
110 */ | |
111 public this(Composite parent, String[] items, int style) { | |
112 super(parent, style); | |
113 setItems(items); | |
114 } | |
115 | |
116 /** | |
117 * Returns the list of choices for the combo box | |
118 * | |
119 * @return the list of choices for the combo box | |
120 */ | |
121 public String[] getItems() { | |
122 return this.items; | |
123 } | |
124 | |
125 /** | |
126 * Sets the list of choices for the combo box | |
127 * | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
128 * @param items |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
129 * the list of choices for the combo box |
10 | 130 */ |
131 public void setItems(String[] items) { | |
132 // Assert.isNotNull(items); | |
133 this.items = items; | |
134 populateComboBoxItems(); | |
135 } | |
136 | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
137 /* |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
138 * (non-Javadoc) Method declared on CellEditor. |
10 | 139 */ |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
140 protected override Control createControl(Composite parent) { |
10 | 141 |
142 comboBox = new CCombo(parent, getStyle()); | |
143 comboBox.setFont(parent.getFont()); | |
144 | |
145 populateComboBoxItems(); | |
146 | |
147 comboBox.addKeyListener(new class KeyAdapter { | |
148 // hook key pressed - see PR 14201 | |
149 public void keyPressed(KeyEvent e) { | |
150 keyReleaseOccured(e); | |
151 } | |
152 }); | |
153 | |
154 comboBox.addSelectionListener(new class SelectionAdapter { | |
155 public void widgetDefaultSelected(SelectionEvent event) { | |
156 applyEditorValueAndDeactivate(); | |
157 } | |
158 | |
159 public void widgetSelected(SelectionEvent event) { | |
160 selection = comboBox.getSelectionIndex(); | |
161 } | |
162 }); | |
163 | |
164 comboBox.addTraverseListener(new class TraverseListener { | |
165 public void keyTraversed(TraverseEvent e) { | |
166 if (e.detail is DWT.TRAVERSE_ESCAPE | |
167 || e.detail is DWT.TRAVERSE_RETURN) { | |
168 e.doit = false; | |
169 } | |
170 } | |
171 }); | |
172 | |
173 comboBox.addFocusListener(new class FocusAdapter { | |
174 public void focusLost(FocusEvent e) { | |
175 this.outer.focusLost(); | |
176 } | |
177 }); | |
178 return comboBox; | |
179 } | |
180 | |
181 /** | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
182 * The <code>ComboBoxCellEditor</code> implementation of this |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
183 * <code>CellEditor</code> framework method returns the zero-based index |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
184 * of the current selection. |
10 | 185 * |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
186 * @return the zero-based index of the current selection wrapped as an |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
187 * <code>Integer</code> |
10 | 188 */ |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
189 protected override Object doGetValue() { |
10 | 190 return new ValueWrapperInt(selection); |
191 } | |
192 | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
193 /* |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
194 * (non-Javadoc) Method declared on CellEditor. |
10 | 195 */ |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
196 protected override void doSetFocus() { |
10 | 197 comboBox.setFocus(); |
198 } | |
199 | |
200 /** | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
201 * The <code>ComboBoxCellEditor</code> implementation of this |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
202 * <code>CellEditor</code> framework method sets the minimum width of the |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
203 * cell. The minimum width is 10 characters if <code>comboBox</code> is |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
204 * not <code>null</code> or <code>disposed</code> else it is 60 pixels |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
205 * to make sure the arrow button and some text is visible. The list of |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
206 * CCombo will be wide enough to show its longest item. |
10 | 207 */ |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
208 public override LayoutData getLayoutData() { |
10 | 209 LayoutData layoutData = super.getLayoutData(); |
210 if ((comboBox is null) || comboBox.isDisposed()) { | |
211 layoutData.minimumWidth = 60; | |
212 } else { | |
213 // make the comboBox 10 characters wide | |
214 GC gc = new GC(comboBox); | |
215 layoutData.minimumWidth = (gc.getFontMetrics() | |
216 .getAverageCharWidth() * 10) + 10; | |
217 gc.dispose(); | |
218 } | |
219 return layoutData; | |
220 } | |
221 | |
222 /** | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
223 * The <code>ComboBoxCellEditor</code> implementation of this |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
224 * <code>CellEditor</code> framework method accepts a zero-based index of |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
225 * a selection. |
10 | 226 * |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
227 * @param value |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
228 * the zero-based index of the selection wrapped as an |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
229 * <code>Integer</code> |
10 | 230 */ |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
231 protected override void doSetValue(Object value) { |
10 | 232 Assert.isTrue(comboBox !is null && (cast(ValueWrapperInt)value )); |
233 selection = (cast(ValueWrapperInt) value).value; | |
234 comboBox.select(selection); | |
235 } | |
236 | |
237 /** | |
238 * Updates the list of choices for the combo box for the current control. | |
239 */ | |
240 private void populateComboBoxItems() { | |
241 if (comboBox !is null && items !is null) { | |
242 comboBox.removeAll(); | |
243 for (int i = 0; i < items.length; i++) { | |
244 comboBox.add(items[i], i); | |
245 } | |
246 | |
247 setValueValid(true); | |
248 selection = 0; | |
249 } | |
250 } | |
251 | |
252 /** | |
253 * Applies the currently selected value and deactivates the cell editor | |
254 */ | |
255 void applyEditorValueAndDeactivate() { | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
256 // must set the selection before getting value |
10 | 257 selection = comboBox.getSelectionIndex(); |
258 Object newValue = doGetValue(); | |
259 markDirty(); | |
260 bool isValid = isCorrect(newValue); | |
261 setValueValid(isValid); | |
262 | |
263 if (!isValid) { | |
264 // Only format if the 'index' is valid | |
265 if (items.length > 0 && selection >= 0 && selection < items.length) { | |
266 // try to insert the current value into the error message. | |
267 setErrorMessage(Format(getErrorMessage(), | |
268 [ items[selection] ])); | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
269 } else { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
270 // Since we don't have a valid index, assume we're using an |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
271 // 'edit' |
10 | 272 // combo so format using its text value |
273 setErrorMessage(Format(getErrorMessage(), | |
274 [ comboBox.getText() ])); | |
275 } | |
276 } | |
277 | |
278 fireApplyEditorValue(); | |
279 deactivate(); | |
280 } | |
281 | |
282 /* | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
283 * (non-Javadoc) |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
284 * |
10 | 285 * @see dwtx.jface.viewers.CellEditor#focusLost() |
286 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
287 protected override void focusLost() { |
10 | 288 if (isActivated()) { |
289 applyEditorValueAndDeactivate(); | |
290 } | |
291 } | |
292 | |
293 /* | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
294 * (non-Javadoc) |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
295 * |
10 | 296 * @see dwtx.jface.viewers.CellEditor#keyReleaseOccured(dwt.events.KeyEvent) |
297 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
298 protected override void keyReleaseOccured(KeyEvent keyEvent) { |
10 | 299 if (keyEvent.character is '\u001b') { // Escape character |
300 fireCancelEditor(); | |
301 } else if (keyEvent.character is '\t') { // tab key | |
302 applyEditorValueAndDeactivate(); | |
303 } | |
304 } | |
305 } |