I have a TableViewer and want the selection to go down one cell when I press the enter key, much like in MS Excel. I implemented my own CellNavigationStrategy with the following findSelectedCell.
public ViewerCell findSelectedCell(ColumnViewer viewer,
ViewerCell currentSelectedCell, Event event) {
if (event.type == ColumnViewerEditorActivationEvent.KEY_PRESSED) {
if (event.keyCode == SWT.CR
|| event.keyCode == SWT.KEYPAD_CR) {
ViewerCell nextCell = currentSelectedCell
.getNeighbor(ViewerCell.BELOW, false);
return nextCell;
}
}
return null;
}
This works pretty well as long as I have ViewerCell.LEFT or ViewerCell.RIGHT.
When I try ViewerCell.ABOVE or ViewerCell.BELOW nextCell is actually set to the
cell above or below, but in the GUI the selection stays at currentSelectedCell.
The API-Documentation for findSelectedCell says:
Returns:
the cell which is highlighted next or null if the default
implementation is taken. E.g. it’s fairly impossible to react on
PAGE_DOWN requests
I do not understand what that sentence means. Can anyone exlain to me why it is not possible to set the selection to a cell below or above?
You have to explicitly set the current selection after the
KEY_PRESSEDevent. Now there are two ways to do it.v.getTable().showColumn(v.getTable().getColumn(nextCell.getColumnIndex()));where is thetable viewerobject. Now this approach normally works with the simple keys likeSWT.ARROW_DOWN,SWT.ARROW_UPetc. But carriage return i.e.SWT.CRusually has some special meaning like submitting a form, pressing a default button on the composite etc. I haven’t checked thoroughly but my gut feeling says it is handled by some other handler and hence you lose focus.SWT.CRuse this:v.getTable().setSelection(((TableItem)nextCell.getItem()));Also you have to override the
CellNavigationStrategy.isNavigationEvent(), otherwiseSWT.CRandSWT.KEYPAD_CRwould be ignored. For example:It means that if you are going to use the default implementation of
CellNavigationStrategy, which is shipped with JFace then it is not possible to handle theSWT.PAGE_DOWNkey press event. The reason is that it is not handled inCellNavigationStrategy.isNavigationEvent()(see its implementation for more details).See the full working code below: