comparison dwtx/jface/viewers/ComboBoxViewerCellEditor.d @ 70:46a6e0e6ccd4

Merge with d-fied sources of 3.4M7
author Frank Benoit <benoit@tionex.de>
date Thu, 22 May 2008 01:36:46 +0200
parents
children 4878bef4a38e
comparison
equal deleted inserted replaced
69:07b9d96fd764 70:46a6e0e6ccd4
1 /*******************************************************************************
2 * Copyright (c) 2006 Tom Schindl 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 * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation
10 * bugfix in 174739
11 * Eric Rizzo - bug 213315
12 * Port to the D programming language:
13 * Frank Benoit <benoit@tionex.de>
14 *******************************************************************************/
15
16 module dwtx.jface.viewers.ComboBoxViewerCellEditor;
17
18 import java.text.MessageFormat;
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 /**
36 * A cell editor that presents a list of items in a combo box. In contrast to
37 * {@link ComboBoxCellEditor} it wraps the underlying {@link CCombo} using a
38 * {@link ComboViewer}
39 */
40 public class ComboBoxViewerCellEditor extends AbstractComboBoxCellEditor {
41
42 /**
43 * The custom combo box control.
44 */
45 ComboViewer viewer;
46
47 Object selectedValue;
48
49 /**
50 * Default ComboBoxCellEditor style
51 */
52 private static final int defaultStyle = DWT.NONE;
53
54 /**
55 * Creates a new cell editor with a combo viewer and a default style
56 *
57 * @param parent
58 * the parent control
59 */
60 public ComboBoxViewerCellEditor(Composite parent) {
61 this(parent, defaultStyle);
62 }
63
64 /**
65 * Creates a new cell editor with a combo viewer and the given style
66 *
67 * @param parent
68 * the parent control
69 * @param style
70 * the style bits
71 */
72 public ComboBoxViewerCellEditor(Composite parent, int style) {
73 super(parent, style);
74 setValueValid(true);
75 }
76
77 /*
78 * (non-Javadoc) Method declared on CellEditor.
79 */
80 protected Control createControl(Composite parent) {
81
82 CCombo comboBox = new CCombo(parent, getStyle());
83 comboBox.setFont(parent.getFont());
84 viewer = new ComboViewer(comboBox);
85
86 comboBox.addKeyListener(new KeyAdapter() {
87 // hook key pressed - see PR 14201
88 public void keyPressed(KeyEvent e) {
89 keyReleaseOccured(e);
90 }
91 });
92
93 comboBox.addSelectionListener(new SelectionAdapter() {
94 public void widgetDefaultSelected(SelectionEvent event) {
95 applyEditorValueAndDeactivate();
96 }
97
98 public void widgetSelected(SelectionEvent event) {
99 ISelection selection = viewer.getSelection();
100 if (selection.isEmpty()) {
101 selectedValue = null;
102 } else {
103 selectedValue = ((IStructuredSelection) selection)
104 .getFirstElement();
105 }
106 }
107 });
108
109 comboBox.addTraverseListener(new TraverseListener() {
110 public void keyTraversed(TraverseEvent e) {
111 if (e.detail is DWT.TRAVERSE_ESCAPE
112 || e.detail is DWT.TRAVERSE_RETURN) {
113 e.doit = false;
114 }
115 }
116 });
117
118 comboBox.addFocusListener(new FocusAdapter() {
119 public void focusLost(FocusEvent e) {
120 ComboBoxViewerCellEditor.this.focusLost();
121 }
122 });
123 return comboBox;
124 }
125
126 /**
127 * The <code>ComboBoxCellEditor</code> implementation of this
128 * <code>CellEditor</code> framework method returns the zero-based index
129 * of the current selection.
130 *
131 * @return the zero-based index of the current selection wrapped as an
132 * <code>Integer</code>
133 */
134 protected Object doGetValue() {
135 return selectedValue;
136 }
137
138 /*
139 * (non-Javadoc) Method declared on CellEditor.
140 */
141 protected void doSetFocus() {
142 viewer.getControl().setFocus();
143 }
144
145 /**
146 * The <code>ComboBoxCellEditor</code> implementation of this
147 * <code>CellEditor</code> framework method sets the minimum width of the
148 * cell. The minimum width is 10 characters if <code>comboBox</code> is
149 * not <code>null</code> or <code>disposed</code> eles it is 60 pixels
150 * to make sure the arrow button and some text is visible. The list of
151 * CCombo will be wide enough to show its longest item.
152 */
153 public LayoutData getLayoutData() {
154 LayoutData layoutData = super.getLayoutData();
155 if ((viewer.getControl() is null) || viewer.getControl().isDisposed()) {
156 layoutData.minimumWidth = 60;
157 } else {
158 // make the comboBox 10 characters wide
159 GC gc = new GC(viewer.getControl());
160 layoutData.minimumWidth = (gc.getFontMetrics()
161 .getAverageCharWidth() * 10) + 10;
162 gc.dispose();
163 }
164 return layoutData;
165 }
166
167 /**
168 * Set a new value
169 *
170 * @param value
171 * the new value
172 */
173 protected void doSetValue(Object value) {
174 Assert.isTrue(viewer !is null);
175 selectedValue = value;
176 if (value is null) {
177 viewer.setSelection(StructuredSelection.EMPTY);
178 } else {
179 viewer.setSelection(new StructuredSelection(value));
180 }
181 }
182
183 /**
184 * @param labelProvider
185 * the label provider used
186 * @see StructuredViewer#setLabelProvider(IBaseLabelProvider)
187 */
188 public void setLabelProvider(IBaseLabelProvider labelProvider) {
189 viewer.setLabelProvider(labelProvider);
190 }
191
192 /**
193 * @param provider
194 * the content provider used
195 * @see StructuredViewer#setContentProvider(IContentProvider)
196 */
197 public void setContenProvider(IStructuredContentProvider provider) {
198 viewer.setContentProvider(provider);
199 }
200
201 /**
202 * @param input
203 * the input used
204 * @see StructuredViewer#setInput(Object)
205 */
206 public void setInput(Object input) {
207 viewer.setInput(input);
208 }
209
210 /**
211 * @return get the viewer
212 */
213 public ComboViewer getViewer() {
214 return viewer;
215 }
216
217 /**
218 * Applies the currently selected value and deactiavates the cell editor
219 */
220 void applyEditorValueAndDeactivate() {
221 // must set the selection before getting value
222 ISelection selection = viewer.getSelection();
223 if (selection.isEmpty()) {
224 selectedValue = null;
225 } else {
226 selectedValue = ((IStructuredSelection) selection)
227 .getFirstElement();
228 }
229
230 Object newValue = doGetValue();
231 markDirty();
232 bool isValid = isCorrect(newValue);
233 setValueValid(isValid);
234
235 if (!isValid) {
236 MessageFormat.format(getErrorMessage(),
237 new Object[] { selectedValue });
238 }
239
240 fireApplyEditorValue();
241 deactivate();
242 }
243
244 /*
245 * (non-Javadoc)
246 *
247 * @see dwtx.jface.viewers.CellEditor#focusLost()
248 */
249 protected void focusLost() {
250 if (isActivated()) {
251 applyEditorValueAndDeactivate();
252 }
253 }
254
255 /*
256 * (non-Javadoc)
257 *
258 * @see dwtx.jface.viewers.CellEditor#keyReleaseOccured(dwt.events.KeyEvent)
259 */
260 protected void keyReleaseOccured(KeyEvent keyEvent) {
261 if (keyEvent.character is '\u001b') { // Escape character
262 fireCancelEditor();
263 } else if (keyEvent.character is '\t') { // tab key
264 applyEditorValueAndDeactivate();
265 }
266 }
267 }