1 12 13 package org.eclipse.jface.viewers; 14 15 import java.text.MessageFormat ; 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 40 public class ComboBoxCellEditor extends CellEditor { 41 42 45 private String [] items; 46 47 50 int selection; 51 52 55 CCombo comboBox; 56 57 60 private static final int defaultStyle = SWT.NONE; 61 62 72 public ComboBoxCellEditor() { 73 setStyle(defaultStyle); 74 } 75 76 86 public ComboBoxCellEditor(Composite parent, String [] items) { 87 this(parent, items, defaultStyle); 88 } 89 90 102 public ComboBoxCellEditor(Composite parent, String [] items, int style) { 103 super(parent, style); 104 setItems(items); 105 } 106 107 112 public String [] getItems() { 113 return this.items; 114 } 115 116 121 public void setItems(String [] items) { 122 Assert.isNotNull(items); 123 this.items = items; 124 populateComboBoxItems(); 125 } 126 127 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 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 179 protected Object doGetValue() { 180 return new Integer (selection); 181 } 182 183 186 protected void doSetFocus() { 187 comboBox.setFocus(); 188 } 189 190 198 public LayoutData getLayoutData() { 199 LayoutData layoutData = super.getLayoutData(); 200 if ((comboBox == null) || comboBox.isDisposed()) { 201 layoutData.minimumWidth = 60; 202 } else { 203 GC gc = new GC(comboBox); 205 layoutData.minimumWidth = (gc.getFontMetrics() 206 .getAverageCharWidth() * 10) + 10; 207 gc.dispose(); 208 } 209 return layoutData; 210 } 211 212 220 protected void doSetValue(Object value) { 221 Assert.isTrue(comboBox != null && (value instanceof Integer )); 222 selection = ((Integer ) value).intValue(); 223 comboBox.select(selection); 224 } 225 226 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 244 void applyEditorValueAndDeactivate() { 245 selection = comboBox.getSelectionIndex(); 247 Object newValue = doGetValue(); 248 markDirty(); 249 boolean isValid = isCorrect(newValue); 250 setValueValid(isValid); 251 252 if (!isValid) { 253 if (items.length > 0 && selection >= 0 && selection < items.length) { 255 setErrorMessage(MessageFormat.format(getErrorMessage(), 257 new Object [] { items[selection] })); 258 } 259 else { 260 setErrorMessage(MessageFormat.format(getErrorMessage(), 263 new Object [] { comboBox.getText() })); 264 } 265 } 266 267 fireApplyEditorValue(); 268 deactivate(); 269 } 270 271 275 protected void focusLost() { 276 if (isActivated()) { 277 applyEditorValueAndDeactivate(); 278 } 279 } 280 281 285 protected void keyReleaseOccured(KeyEvent keyEvent) { 286 if (keyEvent.character == '\u001b') { fireCancelEditor(); 288 } else if (keyEvent.character == '\t') { applyEditorValueAndDeactivate(); 290 } 291 } 292 } 293 | Popular Tags |