1 11 12 package org.eclipse.jface.viewers; 13 14 import java.text.MessageFormat ; 16 import org.eclipse.core.runtime.Assert; 17 import org.eclipse.swt.SWT; 18 import org.eclipse.swt.events.FocusAdapter; 19 import org.eclipse.swt.events.FocusEvent; 20 import org.eclipse.swt.events.KeyAdapter; 21 import org.eclipse.swt.events.KeyEvent; 22 import org.eclipse.swt.events.ModifyEvent; 23 import org.eclipse.swt.events.ModifyListener; 24 import org.eclipse.swt.events.MouseAdapter; 25 import org.eclipse.swt.events.MouseEvent; 26 import org.eclipse.swt.events.SelectionAdapter; 27 import org.eclipse.swt.events.SelectionEvent; 28 import org.eclipse.swt.events.TraverseEvent; 29 import org.eclipse.swt.events.TraverseListener; 30 import org.eclipse.swt.widgets.Composite; 31 import org.eclipse.swt.widgets.Control; 32 import org.eclipse.swt.widgets.Text; 33 34 41 public class TextCellEditor extends CellEditor { 42 43 46 protected Text text; 47 48 private ModifyListener modifyListener; 49 50 53 private boolean isSelection = false; 54 55 private boolean isDeleteable = false; 56 57 private boolean isSelectable = false; 58 59 64 private static final int defaultStyle = SWT.SINGLE; 65 66 73 public TextCellEditor() { 74 setStyle(defaultStyle); 75 } 76 77 84 public TextCellEditor(Composite parent) { 85 this(parent, defaultStyle); 86 } 87 88 97 public TextCellEditor(Composite parent, int style) { 98 super(parent, style); 99 } 100 101 106 private void checkDeleteable() { 107 boolean oldIsDeleteable = isDeleteable; 108 isDeleteable = isDeleteEnabled(); 109 if (oldIsDeleteable != isDeleteable) { 110 fireEnablementChanged(DELETE); 111 } 112 } 113 114 118 private void checkSelectable() { 119 boolean oldIsSelectable = isSelectable; 120 isSelectable = isSelectAllEnabled(); 121 if (oldIsSelectable != isSelectable) { 122 fireEnablementChanged(SELECT_ALL); 123 } 124 } 125 126 131 private void checkSelection() { 132 boolean oldIsSelection = isSelection; 133 isSelection = text.getSelectionCount() > 0; 134 if (oldIsSelection != isSelection) { 135 fireEnablementChanged(COPY); 136 fireEnablementChanged(CUT); 137 } 138 } 139 140 143 protected Control createControl(Composite parent) { 144 text = new Text(parent, getStyle()); 145 text.addSelectionListener(new SelectionAdapter() { 146 public void widgetDefaultSelected(SelectionEvent e) { 147 handleDefaultSelection(e); 148 } 149 }); 150 text.addKeyListener(new KeyAdapter() { 151 public void keyPressed(KeyEvent e) { 153 keyReleaseOccured(e); 154 155 if ((getControl() == null) || getControl().isDisposed()) { 158 return; 159 } 160 checkSelection(); checkDeleteable(); 162 checkSelectable(); 163 } 164 }); 165 text.addTraverseListener(new TraverseListener() { 166 public void keyTraversed(TraverseEvent e) { 167 if (e.detail == SWT.TRAVERSE_ESCAPE 168 || e.detail == SWT.TRAVERSE_RETURN) { 169 e.doit = false; 170 } 171 } 172 }); 173 text.addMouseListener(new MouseAdapter() { 177 public void mouseUp(MouseEvent e) { 178 checkSelection(); 179 checkDeleteable(); 180 checkSelectable(); 181 } 182 }); 183 text.addFocusListener(new FocusAdapter() { 184 public void focusLost(FocusEvent e) { 185 TextCellEditor.this.focusLost(); 186 } 187 }); 188 text.setFont(parent.getFont()); 189 text.setBackground(parent.getBackground()); 190 text.setText(""); text.addModifyListener(getModifyListener()); 192 return text; 193 } 194 195 202 protected Object doGetValue() { 203 return text.getText(); 204 } 205 206 209 protected void doSetFocus() { 210 if (text != null) { 211 text.selectAll(); 212 text.setFocus(); 213 checkSelection(); 214 checkDeleteable(); 215 checkSelectable(); 216 } 217 } 218 219 226 protected void doSetValue(Object value) { 227 Assert.isTrue(text != null && (value instanceof String )); 228 text.removeModifyListener(getModifyListener()); 229 text.setText((String ) value); 230 text.addModifyListener(getModifyListener()); 231 } 232 233 242 protected void editOccured(ModifyEvent e) { 243 String value = text.getText(); 244 if (value == null) { 245 value = ""; } 247 Object typedValue = value; 248 boolean oldValidState = isValueValid(); 249 boolean newValidState = isCorrect(typedValue); 250 if (typedValue == null && newValidState) { 251 Assert.isTrue(false, 252 "Validator isn't limiting the cell editor's type range"); } 254 if (!newValidState) { 255 setErrorMessage(MessageFormat.format(getErrorMessage(), 257 new Object [] { value })); 258 } 259 valueChanged(oldValidState, newValidState); 260 } 261 262 266 public LayoutData getLayoutData() { 267 return new LayoutData(); 268 } 269 270 273 private ModifyListener getModifyListener() { 274 if (modifyListener == null) { 275 modifyListener = new ModifyListener() { 276 public void modifyText(ModifyEvent e) { 277 editOccured(e); 278 } 279 }; 280 } 281 return modifyListener; 282 } 283 284 292 protected void handleDefaultSelection(SelectionEvent event) { 293 fireApplyEditorValue(); 295 deactivate(); 296 } 297 298 303 public boolean isCopyEnabled() { 304 if (text == null || text.isDisposed()) { 305 return false; 306 } 307 return text.getSelectionCount() > 0; 308 } 309 310 315 public boolean isCutEnabled() { 316 if (text == null || text.isDisposed()) { 317 return false; 318 } 319 return text.getSelectionCount() > 0; 320 } 321 322 328 public boolean isDeleteEnabled() { 329 if (text == null || text.isDisposed()) { 330 return false; 331 } 332 return text.getSelectionCount() > 0 333 || text.getCaretPosition() < text.getCharCount(); 334 } 335 336 340 public boolean isPasteEnabled() { 341 if (text == null || text.isDisposed()) { 342 return false; 343 } 344 return true; 345 } 346 347 351 public boolean isSaveAllEnabled() { 352 if (text == null || text.isDisposed()) { 353 return false; 354 } 355 return true; 356 } 357 358 371 public boolean isSelectAllEnabled() { 372 if (text == null || text.isDisposed()) { 373 return false; 374 } 375 return text.getCharCount() > 0; 376 } 377 378 390 protected void keyReleaseOccured(KeyEvent keyEvent) { 391 if (keyEvent.character == '\r') { if (text != null && !text.isDisposed() 401 && (text.getStyle() & SWT.MULTI) != 0) { 402 if ((keyEvent.stateMask & SWT.CTRL) != 0) { 403 super.keyReleaseOccured(keyEvent); 404 } 405 } 406 return; 407 } 408 super.keyReleaseOccured(keyEvent); 409 } 410 411 416 public void performCopy() { 417 text.copy(); 418 } 419 420 425 public void performCut() { 426 text.cut(); 427 checkSelection(); 428 checkDeleteable(); 429 checkSelectable(); 430 } 431 432 438 public void performDelete() { 439 if (text.getSelectionCount() > 0) { 440 text.insert(""); } else { 443 int pos = text.getCaretPosition(); 445 if (pos < text.getCharCount()) { 446 text.setSelection(pos, pos + 1); 447 text.insert(""); } 449 } 450 checkSelection(); 451 checkDeleteable(); 452 checkSelectable(); 453 } 454 455 460 public void performPaste() { 461 text.paste(); 462 checkSelection(); 463 checkDeleteable(); 464 checkSelectable(); 465 } 466 467 472 public void performSelectAll() { 473 text.selectAll(); 474 checkSelection(); 475 checkDeleteable(); 476 } 477 478 boolean dependsOnExternalFocusListener() { 479 return getClass() != TextCellEditor.class; 480 } 481 } 482 | Popular Tags |