1 11 12 package org.eclipse.jface.viewers; 13 14 import org.eclipse.core.runtime.ListenerList; 15 import org.eclipse.core.runtime.Assert; 16 import org.eclipse.jface.util.IPropertyChangeListener; 17 import org.eclipse.jface.util.PropertyChangeEvent; 18 import org.eclipse.jface.util.SafeRunnable; 19 import org.eclipse.swt.SWT; 20 import org.eclipse.swt.events.KeyEvent; 21 import org.eclipse.swt.widgets.Composite; 22 import org.eclipse.swt.widgets.Control; 23 24 39 public abstract class CellEditor { 40 41 44 private ListenerList listeners = new ListenerList(); 45 46 50 private ListenerList propertyChangeListeners = new ListenerList(); 51 52 55 private boolean valid = false; 56 57 60 private ICellEditorValidator validator = null; 61 62 66 private String errorMessage = null; 67 68 71 private boolean dirty = false; 72 73 77 private Control control = null; 78 79 82 private static final int defaultStyle = SWT.NONE; 83 84 87 private int style = defaultStyle; 88 89 93 public static class LayoutData { 94 97 public int horizontalAlignment = SWT.LEFT; 98 99 102 public boolean grabHorizontal = true; 103 104 107 public int minimumWidth = 50; 108 } 109 110 113 public static final String COPY = "copy"; 115 118 public static final String CUT = "cut"; 120 123 public static final String DELETE = "delete"; 125 128 public static final String FIND = "find"; 130 133 public static final String PASTE = "paste"; 135 138 public static final String REDO = "redo"; 140 143 public static final String SELECT_ALL = "selectall"; 145 148 public static final String UNDO = "undo"; 150 155 protected CellEditor() { 156 } 157 158 164 protected CellEditor(Composite parent) { 165 this(parent, defaultStyle); 166 } 167 168 176 protected CellEditor(Composite parent, int style) { 177 this.style = style; 178 create(parent); 179 } 180 181 188 public void activate() { 189 } 190 191 197 public void addListener(ICellEditorListener listener) { 198 listeners.add(listener); 199 } 200 201 208 public void addPropertyChangeListener(IPropertyChangeListener listener) { 209 propertyChangeListeners.add(listener); 210 } 211 212 222 protected abstract Control createControl(Composite parent); 223 224 230 public void create(Composite parent) { 231 Assert.isTrue(control == null); 232 control = createControl(parent); 233 deactivate(); 239 } 240 241 245 public void deactivate() { 246 if (control != null && !control.isDisposed()) { 247 control.setVisible(false); 248 } 249 } 250 251 254 public void dispose() { 255 if (control != null && !control.isDisposed()) { 256 control.dispose(); 257 } 258 control = null; 259 } 260 261 270 protected abstract Object doGetValue(); 271 272 280 protected abstract void doSetFocus(); 281 282 291 protected abstract void doSetValue(Object value); 292 293 299 protected void fireApplyEditorValue() { 300 Object [] array = listeners.getListeners(); 301 for (int i = 0; i < array.length; i++) { 302 final ICellEditorListener l = (ICellEditorListener) array[i]; 303 SafeRunnable.run(new SafeRunnable() { 304 public void run() { 305 l.applyEditorValue(); 306 } 307 }); 308 } 309 } 310 311 317 protected void fireCancelEditor() { 318 Object [] array = listeners.getListeners(); 319 for (int i = 0; i < array.length; i++) { 320 final ICellEditorListener l = (ICellEditorListener) array[i]; 321 SafeRunnable.run(new SafeRunnable() { 322 public void run() { 323 l.cancelEditor(); 324 } 325 }); 326 } 327 } 328 329 336 protected void fireEditorValueChanged(final boolean oldValidState, 337 final boolean newValidState) { 338 Object [] array = listeners.getListeners(); 339 for (int i = 0; i < array.length; i++) { 340 final ICellEditorListener l = (ICellEditorListener) array[i]; 341 SafeRunnable.run(new SafeRunnable() { 342 public void run() { 343 l.editorValueChanged(oldValidState, newValidState); 344 } 345 }); 346 } 347 } 348 349 355 protected void fireEnablementChanged(final String actionId) { 356 Object [] array = propertyChangeListeners.getListeners(); 357 for (int i = 0; i < array.length; i++) { 358 final IPropertyChangeListener l = (IPropertyChangeListener) array[i]; 359 SafeRunnable.run(new SafeRunnable() { 360 public void run() { 361 l.propertyChange(new PropertyChangeEvent(this, actionId, 362 null, null)); 363 } 364 }); 365 } 366 } 367 368 374 public void setStyle(int style) { 375 this.style = style; 376 } 377 378 384 public int getStyle() { 385 return style; 386 } 387 388 393 public Control getControl() { 394 return control; 395 } 396 397 403 public String getErrorMessage() { 404 return errorMessage; 405 } 406 407 419 public LayoutData getLayoutData() { 420 LayoutData result = new LayoutData(); 421 Control control = getControl(); 422 if (control != null) { 423 result.minimumWidth = control.computeSize(SWT.DEFAULT, SWT.DEFAULT, 424 true).x; 425 } 426 return result; 427 } 428 429 434 public ICellEditorValidator getValidator() { 435 return validator; 436 } 437 438 444 public final Object getValue() { 445 if (!valid) { 446 return null; 447 } 448 449 return doGetValue(); 450 } 451 452 458 public boolean isActivated() { 459 return control != null && control.getVisible(); 463 } 464 465 478 public boolean isCopyEnabled() { 479 return false; 480 } 481 482 490 protected boolean isCorrect(Object value) { 491 errorMessage = null; 492 if (validator == null) { 493 return true; 494 } 495 496 errorMessage = validator.isValid(value); 497 return (errorMessage == null || errorMessage.equals("")); } 499 500 513 public boolean isCutEnabled() { 514 return false; 515 } 516 517 530 public boolean isDeleteEnabled() { 531 return false; 532 } 533 534 541 public boolean isDirty() { 542 return dirty; 543 } 544 545 549 protected void markDirty() { 550 dirty = true; 551 } 552 553 566 public boolean isFindEnabled() { 567 return false; 568 } 569 570 583 public boolean isPasteEnabled() { 584 return false; 585 } 586 587 600 public boolean isRedoEnabled() { 601 return false; 602 } 603 604 617 public boolean isSelectAllEnabled() { 618 return false; 619 } 620 621 634 public boolean isUndoEnabled() { 635 return false; 636 } 637 638 647 public boolean isValueValid() { 648 return valid; 649 } 650 651 663 protected void keyReleaseOccured(KeyEvent keyEvent) { 664 if (keyEvent.character == '\u001b') { fireCancelEditor(); 666 } else if (keyEvent.character == '\r') { fireApplyEditorValue(); 668 deactivate(); 669 } 670 } 671 672 681 protected void focusLost() { 682 if (isActivated()) { 683 fireApplyEditorValue(); 684 deactivate(); 685 } 686 } 687 688 695 public void performCopy() { 696 } 697 698 705 public void performCut() { 706 } 707 708 715 public void performDelete() { 716 } 717 718 725 public void performFind() { 726 } 727 728 735 public void performPaste() { 736 } 737 738 745 public void performRedo() { 746 } 747 748 755 public void performSelectAll() { 756 } 757 758 765 public void performUndo() { 766 } 767 768 774 public void removeListener(ICellEditorListener listener) { 775 listeners.remove(listener); 776 } 777 778 785 public void removePropertyChangeListener(IPropertyChangeListener listener) { 786 propertyChangeListeners.remove(listener); 787 } 788 789 797 protected void setErrorMessage(String message) { 798 errorMessage = message; 799 } 800 801 804 public void setFocus() { 805 doSetFocus(); 806 } 807 808 813 public void setValidator(ICellEditorValidator validator) { 814 this.validator = validator; 815 } 816 817 822 public final void setValue(Object value) { 823 valid = isCorrect(value); 824 dirty = false; 825 doSetValue(value); 826 } 827 828 838 protected void setValueValid(boolean valid) { 839 this.valid = valid; 840 } 841 842 851 protected void valueChanged(boolean oldValidState, boolean newValidState) { 852 valid = newValidState; 853 dirty = true; 854 fireEditorValueChanged(oldValidState, newValidState); 855 } 856 857 864 public void activate(ColumnViewerEditorActivationEvent activationEvent) { 865 activate(); 866 } 867 868 874 boolean dependsOnExternalFocusListener() { 875 return true; 876 } 877 } 878 | Popular Tags |