KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > viewers > ComboBoxCellEditor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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  * Tom Schindl <tom.schindl@bestsolution.at> - bugfix in 199775
11  *******************************************************************************/

12
13 package org.eclipse.jface.viewers;
14
15 import java.text.MessageFormat JavaDoc; // Not using ICU to support standalone JFace scenario
16

17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.custom.CCombo;
20 import org.eclipse.swt.events.FocusAdapter;
21 import org.eclipse.swt.events.FocusEvent;
22 import org.eclipse.swt.events.KeyAdapter;
23 import org.eclipse.swt.events.KeyEvent;
24 import org.eclipse.swt.events.SelectionAdapter;
25 import org.eclipse.swt.events.SelectionEvent;
26 import org.eclipse.swt.events.TraverseEvent;
27 import org.eclipse.swt.events.TraverseListener;
28 import org.eclipse.swt.graphics.GC;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Control;
31
32 /**
33  * A cell editor that presents a list of items in a combo box.
34  * The cell editor's value is the zero-based index of the selected
35  * item.
36  * <p>
37  * This class may be instantiated; it is not intended to be subclassed.
38  * </p>
39  */

40 public class ComboBoxCellEditor extends CellEditor {
41
42     /**
43      * The list of items to present in the combo box.
44      */

45     private String JavaDoc[] items;
46
47     /**
48      * The zero-based index of the selected item.
49      */

50     int selection;
51
52     /**
53      * The custom combo box control.
54      */

55     CCombo comboBox;
56
57     /**
58      * Default ComboBoxCellEditor style
59      */

60     private static final int defaultStyle = SWT.NONE;
61
62     /**
63      * Creates a new cell editor with no control and no st of choices. Initially,
64      * the cell editor has no cell validator.
65      *
66      * @since 2.1
67      * @see CellEditor#setStyle
68      * @see CellEditor#create
69      * @see ComboBoxCellEditor#setItems
70      * @see CellEditor#dispose
71      */

72     public ComboBoxCellEditor() {
73         setStyle(defaultStyle);
74     }
75
76     /**
77      * Creates a new cell editor with a combo containing the given
78      * list of choices and parented under the given control. The cell
79      * editor value is the zero-based index of the selected item.
80      * Initially, the cell editor has no cell validator and
81      * the first item in the list is selected.
82      *
83      * @param parent the parent control
84      * @param items the list of strings for the combo box
85      */

86     public ComboBoxCellEditor(Composite parent, String JavaDoc[] items) {
87         this(parent, items, defaultStyle);
88     }
89
90     /**
91      * Creates a new cell editor with a combo containing the given
92      * list of choices and parented under the given control. The cell
93      * editor value is the zero-based index of the selected item.
94      * Initially, the cell editor has no cell validator and
95      * the first item in the list is selected.
96      *
97      * @param parent the parent control
98      * @param items the list of strings for the combo box
99      * @param style the style bits
100      * @since 2.1
101      */

102     public ComboBoxCellEditor(Composite parent, String JavaDoc[] items, int style) {
103         super(parent, style);
104         setItems(items);
105     }
106
107     /**
108      * Returns the list of choices for the combo box
109      *
110      * @return the list of choices for the combo box
111      */

112     public String JavaDoc[] getItems() {
113         return this.items;
114     }
115
116     /**
117      * Sets the list of choices for the combo box
118      *
119      * @param items the list of choices for the combo box
120      */

121     public void setItems(String JavaDoc[] items) {
122         Assert.isNotNull(items);
123         this.items = items;
124         populateComboBoxItems();
125     }
126
127     /* (non-Javadoc)
128      * Method declared on CellEditor.
129      */

130     protected Control createControl(Composite parent) {
131
132         comboBox = new CCombo(parent, getStyle());
133         comboBox.setFont(parent.getFont());
134         
135         populateComboBoxItems();
136
137         comboBox.addKeyListener(new KeyAdapter() {
138             // hook key pressed - see PR 14201
139
public void keyPressed(KeyEvent e) {
140                 keyReleaseOccured(e);
141             }
142         });
143
144         comboBox.addSelectionListener(new SelectionAdapter() {
145             public void widgetDefaultSelected(SelectionEvent event) {
146                 applyEditorValueAndDeactivate();
147             }
148
149             public void widgetSelected(SelectionEvent event) {
150                 selection = comboBox.getSelectionIndex();
151             }
152         });
153
154         comboBox.addTraverseListener(new TraverseListener() {
155             public void keyTraversed(TraverseEvent e) {
156                 if (e.detail == SWT.TRAVERSE_ESCAPE
157                         || e.detail == SWT.TRAVERSE_RETURN) {
158                     e.doit = false;
159                 }
160             }
161         });
162
163         comboBox.addFocusListener(new FocusAdapter() {
164             public void focusLost(FocusEvent e) {
165                 ComboBoxCellEditor.this.focusLost();
166             }
167         });
168         return comboBox;
169     }
170
171     /**
172      * The <code>ComboBoxCellEditor</code> implementation of
173      * this <code>CellEditor</code> framework method returns
174      * the zero-based index of the current selection.
175      *
176      * @return the zero-based index of the current selection wrapped
177      * as an <code>Integer</code>
178      */

179     protected Object JavaDoc doGetValue() {
180         return new Integer JavaDoc(selection);
181     }
182
183     /* (non-Javadoc)
184      * Method declared on CellEditor.
185      */

186     protected void doSetFocus() {
187         comboBox.setFocus();
188     }
189
190     /**
191      * The <code>ComboBoxCellEditor</code> implementation of
192      * this <code>CellEditor</code> framework method sets the
193      * minimum width of the cell. The minimum width is 10 characters
194      * if <code>comboBox</code> is not <code>null</code> or <code>disposed</code>
195      * else it is 60 pixels to make sure the arrow button and some text is visible.
196      * The list of CCombo will be wide enough to show its longest item.
197      */

198     public LayoutData getLayoutData() {
199         LayoutData layoutData = super.getLayoutData();
200         if ((comboBox == null) || comboBox.isDisposed()) {
201             layoutData.minimumWidth = 60;
202         } else {
203             // make the comboBox 10 characters wide
204
GC gc = new GC(comboBox);
205             layoutData.minimumWidth = (gc.getFontMetrics()
206                     .getAverageCharWidth() * 10) + 10;
207             gc.dispose();
208         }
209         return layoutData;
210     }
211
212     /**
213      * The <code>ComboBoxCellEditor</code> implementation of
214      * this <code>CellEditor</code> framework method
215      * accepts a zero-based index of a selection.
216      *
217      * @param value the zero-based index of the selection wrapped
218      * as an <code>Integer</code>
219      */

220     protected void doSetValue(Object JavaDoc value) {
221         Assert.isTrue(comboBox != null && (value instanceof Integer JavaDoc));
222         selection = ((Integer JavaDoc) value).intValue();
223         comboBox.select(selection);
224     }
225
226     /**
227      * Updates the list of choices for the combo box for the current control.
228      */

229     private void populateComboBoxItems() {
230         if (comboBox != null && items != null) {
231             comboBox.removeAll();
232             for (int i = 0; i < items.length; i++) {
233                 comboBox.add(items[i], i);
234             }
235
236             setValueValid(true);
237             selection = 0;
238         }
239     }
240
241     /**
242      * Applies the currently selected value and deactivates the cell editor
243      */

244     void applyEditorValueAndDeactivate() {
245         // must set the selection before getting value
246
selection = comboBox.getSelectionIndex();
247         Object JavaDoc newValue = doGetValue();
248         markDirty();
249         boolean isValid = isCorrect(newValue);
250         setValueValid(isValid);
251         
252         if (!isValid) {
253             // Only format if the 'index' is valid
254
if (items.length > 0 && selection >= 0 && selection < items.length) {
255                 // try to insert the current value into the error message.
256
setErrorMessage(MessageFormat.format(getErrorMessage(),
257                         new Object JavaDoc[] { items[selection] }));
258             }
259             else {
260                 // Since we don't have a valid index, assume we're using an 'edit'
261
// combo so format using its text value
262
setErrorMessage(MessageFormat.format(getErrorMessage(),
263                         new Object JavaDoc[] { comboBox.getText() }));
264             }
265         }
266
267         fireApplyEditorValue();
268         deactivate();
269     }
270
271     /*
272      * (non-Javadoc)
273      * @see org.eclipse.jface.viewers.CellEditor#focusLost()
274      */

275     protected void focusLost() {
276         if (isActivated()) {
277             applyEditorValueAndDeactivate();
278         }
279     }
280
281     /*
282      * (non-Javadoc)
283      * @see org.eclipse.jface.viewers.CellEditor#keyReleaseOccured(org.eclipse.swt.events.KeyEvent)
284      */

285     protected void keyReleaseOccured(KeyEvent keyEvent) {
286         if (keyEvent.character == '\u001b') { // Escape character
287
fireCancelEditor();
288         } else if (keyEvent.character == '\t') { // tab key
289
applyEditorValueAndDeactivate();
290         }
291     }
292 }
293
Popular Tags