Search code examples
widgetswtjfacetableviewer

SWT Move through TableViewer with TAB


Another similar StackOverflow question helped me up to a point.

My TableViewer cells contain boolean widget editors(not your usual check-button widget, but a toggling button, that can be "Checked"/"Unchecked").




The table must:
- always show the editor when a cell is selected;
- disable the editor when cell focus is lost;
- properly move to the next row when TAB is pressed;




Apparently easy, hard in practice. A lot of listener conflicts (TAB doesn't really work).

Later edit:

I've managed to solved the majority of bugs, but this one bug still boggles my mind. If I change the value of the widget with the mouse, then press TAB to traverse, the focus jumps to the second next row, not the next one. BUT if I change the value of the editor using space bar, then the traversing works just fine; it jumps to the next row as it should.

How should I debug those damn listeners?


Solution

  • For ease of development you can use some helper classes in JFace and you will not have to deal with the event handling of tabbing or mouse clicks. Here is a snippet that should help you get started:

    TableViewer viewer = ...
    
    TableViewerFocusCellManager focusCellManager = new TableViewerFocusCellManager(viewer, new FocusCellOwnerDrawHighlighter(viewer));
    
    ColumnViewerEditorActivationStrategy activationSupport = new ColumnViewerEditorActivationStrategy(viewer) {
        protected boolean isEditorActivationEvent(ColumnViewerEditorActivationEvent event) {
            if (event.eventType == ColumnViewerEditorActivationEvent.MOUSE_CLICK_SELECTION) {
                EventObject source = event.sourceEvent;
                if (source instanceof MouseEvent && ((MouseEvent)source).button == 3)
                    return false;
            }
            return super.isEditorActivationEvent(event) || (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED && event.keyCode == SWT.CR);
        }
    };
    
    TableViewerEditor.create(viewer, focusCellManager, activationSupport, ColumnViewerEditor.TABBING_HORIZONTAL | 
        ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR | 
        ColumnViewerEditor.TABBING_VERTICAL |
        ColumnViewerEditor.KEYBOARD_ACTIVATION);