comparison dwtx/jface/viewers/CellEditor.d @ 10:b6c35faf97c8

Viewers
author Frank Benoit <benoit@tionex.de>
date Mon, 31 Mar 2008 00:47:19 +0200
parents
children 644f1334b451
comparison
equal deleted inserted replaced
9:6c14e54dfc11 10:b6c35faf97c8
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 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13
14 module dwtx.jface.viewers.CellEditor;
15
16 import dwtx.jface.viewers.ICellEditorValidator;
17 import dwtx.jface.viewers.ICellEditorListener;
18 import dwtx.jface.viewers.ColumnViewerEditorActivationEvent;
19
20 import dwt.DWT;
21 import dwt.events.KeyEvent;
22 import dwt.widgets.Composite;
23 import dwt.widgets.Control;
24 import dwtx.core.runtime.Assert;
25 import dwtx.core.runtime.ListenerList;
26 import dwtx.jface.util.IPropertyChangeListener;
27 import dwtx.jface.util.PropertyChangeEvent;
28 import dwtx.jface.util.SafeRunnable;
29
30 import dwt.dwthelper.utils;
31
32 /**
33 * Abstract base class for cell editors. Implements property change listener handling,
34 * and DWT window management.
35 * <p>
36 * Subclasses implement particular kinds of cell editors. This package contains various
37 * specialized cell editors:
38 * <ul>
39 * <li><code>TextCellEditor</code> - for simple text strings</li>
40 * <li><code>ColorCellEditor</code> - for colors</li>
41 * <li><code>ComboBoxCellEditor</code> - value selected from drop-down combo box</li>
42 * <li><code>CheckboxCellEditor</code> - bool valued checkbox</li>
43 * <li><code>DialogCellEditor</code> - value from arbitrary dialog</li>
44 * </ul>
45 * </p>
46 */
47 public abstract class CellEditor {
48
49 /**
50 * List of cell editor listeners (element type: <code>ICellEditorListener</code>).
51 */
52 private ListenerList listeners;
53
54 /**
55 * List of cell editor property change listeners
56 * (element type: <code>IPropertyChangeListener</code>).
57 */
58 private ListenerList propertyChangeListeners;
59
60 /**
61 * Indicates whether this cell editor's current value is valid.
62 */
63 private bool valid = false;
64
65 /**
66 * Optional cell editor validator; <code>null</code> if none.
67 */
68 private ICellEditorValidator validator = null;
69
70 /**
71 * The error message string to display for invalid values;
72 * <code>null</code> if none (that is, the value is valid).
73 */
74 private String errorMessage = null;
75
76 /**
77 * Indicates whether this cell editor has been changed recently.
78 */
79 private bool dirty = false;
80
81 /**
82 * This cell editor's control, or <code>null</code>
83 * if not created yet.
84 */
85 private Control control = null;
86
87 /**
88 * Default cell editor style
89 */
90 private static const int defaultStyle = DWT.NONE;
91
92 /**
93 * This cell editor's style
94 */
95 private int style = defaultStyle;
96
97 /**
98 * Struct-like layout data for cell editors, with reasonable defaults
99 * for all fields.
100 */
101 public static class LayoutData {
102 /**
103 * Horizontal alignment; <code>DWT.LEFT</code> by default.
104 */
105 public int horizontalAlignment = DWT.LEFT;
106
107 /**
108 * Indicates control grabs additional space; <code>true</code> by default.
109 */
110 public bool grabHorizontal = true;
111
112 /**
113 * Minimum width in pixels; <code>50</code> pixels by default.
114 */
115 public int minimumWidth = 50;
116 }
117
118 /**
119 * Property name for the copy action
120 */
121 public static const String COPY = "copy"; //$NON-NLS-1$
122
123 /**
124 * Property name for the cut action
125 */
126 public static const String CUT = "cut"; //$NON-NLS-1$
127
128 /**
129 * Property name for the delete action
130 */
131 public static const String DELETE = "delete"; //$NON-NLS-1$
132
133 /**
134 * Property name for the find action
135 */
136 public static const String FIND = "find"; //$NON-NLS-1$
137
138 /**
139 * Property name for the paste action
140 */
141 public static const String PASTE = "paste"; //$NON-NLS-1$
142
143 /**
144 * Property name for the redo action
145 */
146 public static const String REDO = "redo"; //$NON-NLS-1$
147
148 /**
149 * Property name for the select all action
150 */
151 public static const String SELECT_ALL = "selectall"; //$NON-NLS-1$
152
153 /**
154 * Property name for the undo action
155 */
156 public static const String UNDO = "undo"; //$NON-NLS-1$
157
158 /**
159 * Creates a new cell editor with no control
160 * The cell editor has no cell validator.
161 * @since 2.1
162 */
163 protected this() {
164 propertyChangeListeners = new ListenerList();
165 listeners = new ListenerList();
166 }
167
168 /**
169 * Creates a new cell editor under the given parent control.
170 * The cell editor has no cell validator.
171 *
172 * @param parent the parent control
173 */
174 protected this(Composite parent) {
175 this(parent, defaultStyle);
176 }
177
178 /**
179 * Creates a new cell editor under the given parent control.
180 * The cell editor has no cell validator.
181 *
182 * @param parent the parent control
183 * @param style the style bits
184 * @since 2.1
185 */
186 protected this(Composite parent, int style) {
187 propertyChangeListeners = new ListenerList();
188 listeners = new ListenerList();
189 this.style = style;
190 create(parent);
191 }
192
193 /**
194 * Activates this cell editor.
195 * <p>
196 * The default implementation of this framework method
197 * does nothing. Subclasses may reimplement.
198 * </p>
199 */
200 public void activate() {
201 }
202
203 /**
204 * Adds a listener to this cell editor.
205 * Has no effect if an identical listener is already registered.
206 *
207 * @param listener a cell editor listener
208 */
209 public void addListener(ICellEditorListener listener) {
210 listeners.add(cast(Object)listener);
211 }
212
213 /**
214 * Adds a property change listener to this cell editor.
215 * Has no effect if an identical property change listener
216 * is already registered.
217 *
218 * @param listener a property change listener
219 */
220 public void addPropertyChangeListener(IPropertyChangeListener listener) {
221 propertyChangeListeners.add(cast(Object)listener);
222 }
223
224 /**
225 * Creates the control for this cell editor under the given parent control.
226 * <p>
227 * This framework method must be implemented by concrete
228 * subclasses.
229 * </p>
230 *
231 * @param parent the parent control
232 * @return the new control, or <code>null</code> if this cell editor has no control
233 */
234 protected abstract Control createControl(Composite parent);
235
236 /**
237 * Creates the control for this cell editor under the given parent control.
238 *
239 * @param parent the parent control
240 * @since 2.1
241 */
242 public void create(Composite parent) {
243 Assert.isTrue(control is null);
244 control = createControl(parent);
245 // See 1GD5CA6: ITPUI:ALL - TaskView.setSelection does not work
246 // Control is created with getVisible()istrue by default.
247 // This causes composite.setFocus() to work incorrectly.
248 // The cell editor's control grabs focus instead, even if it is not active.
249 // Make the control invisible here by default.
250 deactivate();
251 }
252
253 /**
254 * Hides this cell editor's control. Does nothing if this
255 * cell editor is not visible.
256 */
257 public void deactivate() {
258 if (control !is null && !control.isDisposed()) {
259 control.setVisible(false);
260 }
261 }
262
263 /**
264 * Disposes of this cell editor and frees any associated DWT resources.
265 */
266 public void dispose() {
267 if (control !is null && !control.isDisposed()) {
268 control.dispose();
269 }
270 control = null;
271 }
272
273 /**
274 * Returns this cell editor's value.
275 * <p>
276 * This framework method must be implemented by concrete subclasses.
277 * </p>
278 *
279 * @return the value of this cell editor
280 * @see #getValue
281 */
282 protected abstract Object doGetValue();
283
284 /**
285 * Sets the focus to the cell editor's control.
286 * <p>
287 * This framework method must be implemented by concrete subclasses.
288 * </p>
289 *
290 * @see #setFocus
291 */
292 protected abstract void doSetFocus();
293
294 /**
295 * Sets this cell editor's value.
296 * <p>
297 * This framework method must be implemented by concrete subclasses.
298 * </p>
299 *
300 * @param value the value of this cell editor
301 * @see #setValue
302 */
303 protected abstract void doSetValue(Object value);
304
305 /**
306 * Notifies all registered cell editor listeners of an apply event.
307 * Only listeners registered at the time this method is called are notified.
308 *
309 * @see ICellEditorListener#applyEditorValue
310 */
311 protected void fireApplyEditorValue() {
312 Object[] array = listeners.getListeners();
313 for (int i = 0; i < array.length; i++) {
314 SafeRunnable.run(new class SafeRunnable {
315 ICellEditorListener l;
316 this(){
317 l = cast(ICellEditorListener) array[i];
318 }
319 public void run() {
320 l.applyEditorValue();
321 }
322 });
323 }
324 }
325
326 /**
327 * Notifies all registered cell editor listeners that editing has been
328 * canceled.
329 *
330 * @see ICellEditorListener#cancelEditor
331 */
332 protected void fireCancelEditor() {
333 Object[] array = listeners.getListeners();
334 for (int i = 0; i < array.length; i++) {
335 SafeRunnable.run(new class SafeRunnable {
336 ICellEditorListener l;
337 this(){
338 l = cast(ICellEditorListener) array[i];
339 }
340 public void run() {
341 l.cancelEditor();
342 }
343 });
344 }
345 }
346
347 /**
348 * Notifies all registered cell editor listeners of a value change.
349 *
350 * @param oldValidState the valid state before the end user changed the value
351 * @param newValidState the current valid state
352 * @see ICellEditorListener#editorValueChanged
353 */
354 protected void fireEditorValueChanged(bool oldValidState,
355 bool newValidState) {
356 Object[] array = listeners.getListeners();
357 for (int i = 0; i < array.length; i++) {
358 SafeRunnable.run(new class SafeRunnable {
359 bool newValidState_;
360 bool oldValidState_;
361 ICellEditorListener l;
362 this(){
363 newValidState_=newValidState;
364 oldValidState_=oldValidState;
365 l = cast(ICellEditorListener) array[i];
366 }
367 public void run() {
368 l.editorValueChanged(oldValidState_, newValidState_);
369 }
370 });
371 }
372 }
373
374 /**
375 * Notifies all registered property listeners
376 * of an enablement change.
377 *
378 * @param actionId the id indicating what action's enablement has changed.
379 */
380 protected void fireEnablementChanged(String actionId) {
381 Object[] array = propertyChangeListeners.getListeners();
382 for (int i = 0; i < array.length; i++) {
383 SafeRunnable.run(new class SafeRunnable {
384 String actionId_;
385 IPropertyChangeListener l;
386 this(){
387 actionId_=actionId;
388 l = cast(IPropertyChangeListener) array[i];
389 }
390 public void run() {
391 l.propertyChange(new PropertyChangeEvent(this, actionId_,
392 null, null));
393 }
394 });
395 }
396 }
397
398 /**
399 * Sets the style bits for this cell editor.
400 *
401 * @param style the DWT style bits for this cell editor
402 * @since 2.1
403 */
404 public void setStyle(int style) {
405 this.style = style;
406 }
407
408 /**
409 * Returns the style bits for this cell editor.
410 *
411 * @return the style for this cell editor
412 * @since 2.1
413 */
414 public int getStyle() {
415 return style;
416 }
417
418 /**
419 * Returns the control used to implement this cell editor.
420 *
421 * @return the control, or <code>null</code> if this cell editor has no control
422 */
423 public Control getControl() {
424 return control;
425 }
426
427 /**
428 * Returns the current error message for this cell editor.
429 *
430 * @return the error message if the cell editor is in an invalid state,
431 * and <code>null</code> if the cell editor is valid
432 */
433 public String getErrorMessage() {
434 return errorMessage;
435 }
436
437 /**
438 * Returns a layout data object for this cell editor.
439 * This is called each time the cell editor is activated
440 * and controls the layout of the DWT table editor.
441 * <p>
442 * The default implementation of this method sets the
443 * minimum width to the control's preferred width.
444 * Subclasses may extend or reimplement.
445 * </p>
446 *
447 * @return the layout data object
448 */
449 public LayoutData getLayoutData() {
450 LayoutData result = new LayoutData();
451 Control control = getControl();
452 if (control !is null) {
453 result.minimumWidth = control.computeSize(DWT.DEFAULT, DWT.DEFAULT,
454 true).x;
455 }
456 return result;
457 }
458
459 /**
460 * Returns the input validator for this cell editor.
461 *
462 * @return the input validator, or <code>null</code> if none
463 */
464 public ICellEditorValidator getValidator() {
465 return validator;
466 }
467
468 /**
469 * Returns this cell editor's value provided that it has a valid one.
470 *
471 * @return the value of this cell editor, or <code>null</code>
472 * if the cell editor does not contain a valid value
473 */
474 public final Object getValue() {
475 if (!valid) {
476 return null;
477 }
478
479 return doGetValue();
480 }
481
482 /**
483 * Returns whether this cell editor is activated.
484 *
485 * @return <code>true</code> if this cell editor's control is
486 * currently activated, and <code>false</code> if not activated
487 */
488 public bool isActivated() {
489 // Use the state of the visible style bit (getVisible()) rather than the
490 // window's actual visibility (isVisible()) to get correct handling when
491 // an ancestor control goes invisible, see bug 85331.
492 return control !is null && control.getVisible();
493 }
494
495 /**
496 * Returns <code>true</code> if this cell editor is
497 * able to perform the copy action.
498 * <p>
499 * This default implementation always returns
500 * <code>false</code>.
501 * </p>
502 * <p>
503 * Subclasses may override
504 * </p>
505 * @return <code>true</code> if copy is possible,
506 * <code>false</code> otherwise
507 */
508 public bool isCopyEnabled() {
509 return false;
510 }
511
512 /**
513 * Returns whether the given value is valid for this cell editor.
514 * This cell editor's validator (if any) makes the actual determination.
515 * @param value the value to check for
516 *
517 * @return <code>true</code> if the value is valid, and <code>false</code>
518 * if invalid
519 */
520 protected bool isCorrect(Object value) {
521 errorMessage = null;
522 if (validator is null) {
523 return true;
524 }
525
526 errorMessage = validator.isValid(value);
527 return (errorMessage is null || errorMessage.equals(""));//$NON-NLS-1$
528 }
529
530 /**
531 * Returns <code>true</code> if this cell editor is
532 * able to perform the cut action.
533 * <p>
534 * This default implementation always returns
535 * <code>false</code>.
536 * </p>
537 * <p>
538 * Subclasses may override
539 * </p>
540 * @return <code>true</code> if cut is possible,
541 * <code>false</code> otherwise
542 */
543 public bool isCutEnabled() {
544 return false;
545 }
546
547 /**
548 * Returns <code>true</code> if this cell editor is
549 * able to perform the delete action.
550 * <p>
551 * This default implementation always returns
552 * <code>false</code>.
553 * </p>
554 * <p>
555 * Subclasses may override
556 * </p>
557 * @return <code>true</code> if delete is possible,
558 * <code>false</code> otherwise
559 */
560 public bool isDeleteEnabled() {
561 return false;
562 }
563
564 /**
565 * Returns whether the value of this cell editor has changed since the
566 * last call to <code>setValue</code>.
567 *
568 * @return <code>true</code> if the value has changed, and <code>false</code>
569 * if unchanged
570 */
571 public bool isDirty() {
572 return dirty;
573 }
574
575 /**
576 * Marks this cell editor as dirty.
577 * @since 2.1
578 */
579 protected void markDirty() {
580 dirty = true;
581 }
582
583 /**
584 * Returns <code>true</code> if this cell editor is
585 * able to perform the find action.
586 * <p>
587 * This default implementation always returns
588 * <code>false</code>.
589 * </p>
590 * <p>
591 * Subclasses may override
592 * </p>
593 * @return <code>true</code> if find is possible,
594 * <code>false</code> otherwise
595 */
596 public bool isFindEnabled() {
597 return false;
598 }
599
600 /**
601 * Returns <code>true</code> if this cell editor is
602 * able to perform the paste action.
603 * <p>
604 * This default implementation always returns
605 * <code>false</code>.
606 * </p>
607 * <p>
608 * Subclasses may override
609 * </p>
610 * @return <code>true</code> if paste is possible,
611 * <code>false</code> otherwise
612 */
613 public bool isPasteEnabled() {
614 return false;
615 }
616
617 /**
618 * Returns <code>true</code> if this cell editor is
619 * able to perform the redo action.
620 * <p>
621 * This default implementation always returns
622 * <code>false</code>.
623 * </p>
624 * <p>
625 * Subclasses may override
626 * </p>
627 * @return <code>true</code> if redo is possible,
628 * <code>false</code> otherwise
629 */
630 public bool isRedoEnabled() {
631 return false;
632 }
633
634 /**
635 * Returns <code>true</code> if this cell editor is
636 * able to perform the select all action.
637 * <p>
638 * This default implementation always returns
639 * <code>false</code>.
640 * </p>
641 * <p>
642 * Subclasses may override
643 * </p>
644 * @return <code>true</code> if select all is possible,
645 * <code>false</code> otherwise
646 */
647 public bool isSelectAllEnabled() {
648 return false;
649 }
650
651 /**
652 * Returns <code>true</code> if this cell editor is
653 * able to perform the undo action.
654 * <p>
655 * This default implementation always returns
656 * <code>false</code>.
657 * </p>
658 * <p>
659 * Subclasses may override
660 * </p>
661 * @return <code>true</code> if undo is possible,
662 * <code>false</code> otherwise
663 */
664 public bool isUndoEnabled() {
665 return false;
666 }
667
668 /**
669 * Returns whether this cell editor has a valid value.
670 * The default value is false.
671 *
672 * @return <code>true</code> if the value is valid, and <code>false</code>
673 * if invalid
674 *
675 * @see #setValueValid(bool)
676 */
677 public bool isValueValid() {
678 return valid;
679 }
680
681 /**
682 * Processes a key release event that occurred in this cell editor.
683 * <p>
684 * The default implementation of this framework method cancels editing
685 * when the ESC key is pressed. When the RETURN key is pressed the current
686 * value is applied and the cell editor deactivates.
687 * Subclasses should call this method at appropriate times.
688 * Subclasses may also extend or reimplement.
689 * </p>
690 *
691 * @param keyEvent the key event
692 */
693 protected void keyReleaseOccured(KeyEvent keyEvent) {
694 if (keyEvent.character is '\u001b') { // Escape character
695 fireCancelEditor();
696 } else if (keyEvent.character is '\r') { // Return key
697 fireApplyEditorValue();
698 deactivate();
699 }
700 }
701
702 /**
703 * Processes a focus lost event that occurred in this cell editor.
704 * <p>
705 * The default implementation of this framework method applies the current
706 * value and deactivates the cell editor.
707 * Subclasses should call this method at appropriate times.
708 * Subclasses may also extend or reimplement.
709 * </p>
710 */
711 protected void focusLost() {
712 if (isActivated()) {
713 fireApplyEditorValue();
714 deactivate();
715 }
716 }
717
718 /**
719 * Performs the copy action.
720 * This default implementation does nothing.
721 * <p>
722 * Subclasses may override
723 * </p>
724 */
725 public void performCopy() {
726 }
727
728 /**
729 * Performs the cut action.
730 * This default implementation does nothing.
731 * <p>
732 * Subclasses may override
733 * </p>
734 */
735 public void performCut() {
736 }
737
738 /**
739 * Performs the delete action.
740 * This default implementation does nothing.
741 * <p>
742 * Subclasses may override
743 * </p>
744 */
745 public void performDelete() {
746 }
747
748 /**
749 * Performs the find action.
750 * This default implementation does nothing.
751 * <p>
752 * Subclasses may override
753 * </p>
754 */
755 public void performFind() {
756 }
757
758 /**
759 * Performs the paste action.
760 * This default implementation does nothing.
761 * <p>
762 * Subclasses may override
763 * </p>
764 */
765 public void performPaste() {
766 }
767
768 /**
769 * Performs the redo action.
770 * This default implementation does nothing.
771 * <p>
772 * Subclasses may override
773 * </p>
774 */
775 public void performRedo() {
776 }
777
778 /**
779 * Performs the select all action.
780 * This default implementation does nothing.
781 * <p>
782 * Subclasses may override
783 * </p>
784 */
785 public void performSelectAll() {
786 }
787
788 /**
789 * Performs the undo action.
790 * This default implementation does nothing.
791 * <p>
792 * Subclasses may override
793 * </p>
794 */
795 public void performUndo() {
796 }
797
798 /**
799 * Removes the given listener from this cell editor.
800 * Has no affect if an identical listener is not registered.
801 *
802 * @param listener a cell editor listener
803 */
804 public void removeListener(ICellEditorListener listener) {
805 listeners.remove(cast(Object)listener);
806 }
807
808 /**
809 * Removes the given property change listener from this cell editor.
810 * Has no affect if an identical property change listener is not
811 * registered.
812 *
813 * @param listener a property change listener
814 */
815 public void removePropertyChangeListener(IPropertyChangeListener listener) {
816 propertyChangeListeners.remove(cast(Object)listener);
817 }
818
819 /**
820 * Sets or clears the current error message for this cell editor.
821 * <p>
822 * No formatting is done here, the message to be set is expected to be fully formatted
823 * before being passed in.
824 * </p>
825 * @param message the error message, or <code>null</code> to clear
826 */
827 protected void setErrorMessage(String message) {
828 errorMessage = message;
829 }
830
831 /**
832 * Sets the focus to the cell editor's control.
833 */
834 public void setFocus() {
835 doSetFocus();
836 }
837
838 /**
839 * Sets the input validator for this cell editor.
840 *
841 * @param validator the input validator, or <code>null</code> if none
842 */
843 public void setValidator(ICellEditorValidator validator) {
844 this.validator = validator;
845 }
846
847 /**
848 * Sets this cell editor's value.
849 *
850 * @param value the value of this cell editor
851 */
852 public final void setValue(Object value) {
853 valid = isCorrect(value);
854 dirty = false;
855 doSetValue(value);
856 }
857
858 /**
859 * Sets the valid state of this cell editor.
860 * The default value is false.
861 * Subclasses should call this method on construction.
862 *
863 * @param valid <code>true</code> if the current value is valid,
864 * and <code>false</code> if invalid
865 *
866 * @see #isValueValid
867 */
868 protected void setValueValid(bool valid) {
869 this.valid = valid;
870 }
871
872 /**
873 * The value has changed.
874 * Updates the valid state flag, marks this cell editor as dirty,
875 * and notifies all registered cell editor listeners of a value change.
876 *
877 * @param oldValidState the valid state before the end user changed the value
878 * @param newValidState the current valid state
879 * @see ICellEditorListener#editorValueChanged
880 */
881 protected void valueChanged(bool oldValidState, bool newValidState) {
882 valid = newValidState;
883 dirty = true;
884 fireEditorValueChanged(oldValidState, newValidState);
885 }
886
887 /**
888 * Activate the editor but also inform the editor which event triggered its activation.
889 * <b>The default implementation simply calls {@link #activate()}</b>
890 *
891 * @param activationEvent the editor activation event
892 * @since 3.3
893 */
894 public void activate(ColumnViewerEditorActivationEvent activationEvent) {
895 activate();
896 }
897
898 /**
899 * This method is for interal use in {@link ColumnViewerEditor} to not break clients
900 * who don't implement the {@link ICellEditorListener} appropiately
901 *
902 * @return <code>true</code> to indicate that a focus listener has to be attached
903 */
904 bool dependsOnExternalFocusListener() {
905 return true;
906 }
907 }