2

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Georgian
  • 8,795
  • 8
  • 46
  • 87
  • What exactly is the problem that you are facing. The referenced question should enable you to set selection to the next cells when TAB is pressed. Where are you getting stuck? – Waqas Ilyas Dec 06 '12 at 16:12
  • Focusing out is based on how the widget was changed: via Keyboard, or via Mouse button. When when change via mouse, and focus out with TAB, selection does not go to the next row, but rather on the second next row, leaving the one in between sort of greyed out (like in the last image, first row) – Georgian Dec 06 '12 at 16:54
  • Well the problem you described looks like some bug in the code you have implemented but the technique you are using is correct. – Waqas Ilyas Dec 07 '12 at 12:06

1 Answers1

5

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);
Waqas Ilyas
  • 3,116
  • 1
  • 17
  • 27
  • Editor activates on cell selection. I have no use for the enter key. Futhermore: ESC will not dispose the editor, and TAB should enable the editor on the next row. If there is no selection in the table, no editor will be activated. – Georgian Dec 06 '12 at 16:56
  • Well you can adjust the parameters to suit your needs (like not dipsosing on ESC or doing nothing on enter key). But the logic should work for you whether you move from cell to cell via TAB, mouse click, or arrow keys. As the cells get selected the editor would be created and shown. – Waqas Ilyas Dec 07 '12 at 12:08
  • 1
    For those who are reading this answer: Answer is correct, some details of my internal implementation were wrong. – Georgian Dec 04 '13 at 21:12