Search code examples
javaeclipsenattable

How to custamize table cell navigation features of NatTable for "TAB" and "ARROW" keys for an acceptable usability


Using standard keyboard navigation "Up", "Down", "Left", "Right" arrow keys should move the "selection mode" cell into respective direction. No doubt this feature is working after adding this code.

gridLayer.addConfiguration(new DefaultEditBindings());

gridLayer.addConfiguration(new DefaultEditConfiguration());

Now i want to implement Edit bindings for special case. "special move cases": When moving the "selection mode" cell, following special cases should be considered for movements in all modes:

  • If the right most cell of a row is reached, and a movement to the right is requested, the "selection mode" should be moved to the left most cell in the next row

  • If the left most cell of a row is reached, and a movement to the left is requested, the "selection mode" should be moved to the right most cell in the previous row

  • If the right most cell in the last row is reached, and a movement to the right is requested, the "selection mode" should be moved to the left most cell in the first row

  • If the left most cell in the first row is reached, and a movement to the left is requested, the "selection mode" should be moved to the right most cell in the last row

  • If a cell in the last row is reached, and a movement down is requested, the "selection mode" should be moved to the cell in the first row in the next column to the right

  • If a cell in the first row is reached, and a movement up is requested, the "selection mode" should be moved to the cell in the last row in the previous column to the left

  • If the right most cell in the last row is reached, and a movement down is requested, the "selection mode" should be moved to the left most cell in the first row

  • If the left most cell in the first row is reached, and a movement up is requested, the "selection mode" should be moved to the right most cell in the last row

How to implement this, example would be useful?

 // === Body layer ===
final DataLayer bodyDataLayer = new DataLayer(this.bodyDataProvider);
// add configuration tags (ConfigLabels) for body cells
bodyDataLayer.setConfigLabelAccumulator(this.configLabelAccumulator);

// remaining standard body layers
final DefaultBodyLayerStack bodyLayerStack = new DefaultBodyLayerStack(bodyDataLayer);
this.selectionLayer = bodyLayerStack.getSelectionLayer();

ViewportLayer viewportLayer = new ViewportLayer(this.selectionLayer);
// as the selection mouse bindings are registered for the region label
// GridRegion.BODY we need to set that region label to the viewport so

final FreezeLayer freezeLayer = new FreezeLayer(this.selectionLayer);
final CompositeFreezeLayer compositeFreezeLayer = new CompositeFreezeLayer(freezeLayer, bodyLayerStack.getViewportLayer(), this.selectionLayer);
final ILayer bodyLayer = compositeFreezeLayer;

// === Column header layer ===
final ILayer columnHeaderLayer = new ColumnHeaderLayer(new DefaultColumnHeaderDataLayer(this.columnHeaderDataProvider), bodyLayer, this.selectionLayer);

// === Row header layer ===
final ILayer rowHeaderLayer = new RowHeaderLayer(new DefaultRowHeaderDataLayer(this.rowHeaderDataProvider), bodyLayer, this.selectionLayer);

// === Corner layer ===
final CornerLayer cornerLayer = new CornerLayer(new DataLayer(this.cornerDataProvider), rowHeaderLayer, columnHeaderLayer);

// === Grid layer ===
final GridLayer gridLayer = new GridLayer(bodyLayer, columnHeaderLayer, rowHeaderLayer, cornerLayer, false);
// overwrite default AutoResizeColumnCommandHandler, which only supported auto-resizing of body layer columns
// gridLayer.registerCommandHandler(new GridLayerAutoResizeColumnCommandHandler(gridLayer));
gridLayer.registerCommandHandler(new AutoResizeColumnCommandHandler(gridLayer));

// the selection via mouse is working correctly gridLayer.setRegionName(GridRegion.BODY);

// register a MoveCellSelectionCommandHandler with
// TABLE_CYCLE_TRAVERSAL_STRATEGY
gridLayer.registerCommandHandler(
    new MoveCellSelectionCommandHandler(this.selectionLayer, ITraversalStrategy.TABLE_CYCLE_TRAVERSAL_STRATEGY));

// NatTable
this.natTable = new NatTable(this, SWT.H_SCROLL | SWT.V_SCROLL |   SWT.BORDER, gridLayer, false);

Solution

  • What you are looking for is the traversal strategy configuration that was introduced with NatTable 1.2. From your explanation you need to configure the TABLE_CYCLE traversal strategy to meet your requirements.

    There is the SelectionTraversalExample provided in the NatTable Examples application that should show what options you have and how to configure them.

    P.S. The selection movement has nothing to do with the edit configurations!