Skip to content

Commit

Permalink
GUVNOR-2557: Add keyboard navigation through the table (apache#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
manstis committed Jul 21, 2016
1 parent 4d9f64b commit 2ac5479
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
import org.uberfire.ext.wires.core.grids.client.model.GridData;
import org.uberfire.ext.wires.core.grids.client.util.CoordinateUtilities;
import org.uberfire.ext.wires.core.grids.client.widget.dnd.IsRowDragHandle;
import org.uberfire.ext.wires.core.grids.client.widget.grid.selections.CellSelectionManager;
import org.uberfire.ext.wires.core.grids.client.widget.grid.selections.impl.CellRangeSelectionManager;
import org.uberfire.ext.wires.core.grids.client.widget.grid.selections.CellSelectionStrategy;
import org.uberfire.ext.wires.core.grids.client.widget.grid.selections.impl.RangeSelectionStrategy;

@Dependent
public class GuidedDecisionTableModellerContextMenuSupport {
Expand Down Expand Up @@ -92,13 +92,14 @@ public void onContextMenu( final ContextMenuEvent event ) {
final Point2D ap = CoordinateUtilities.convertDOMToGridCoordinate( gridView,
new Point2D( canvasX,
canvasY ) );
final GridData.SelectedCell sc = CoordinateUtilities.getCell( gridView,
ap );
if ( sc == null ) {
continue;
final Integer uiRowIndex = CoordinateUtilities.getUiRowIndex( gridView,
ap.getY() );
final Integer uiColumnIndex = CoordinateUtilities.getUiColumnIndex( gridView,
ap.getX() );
if ( uiRowIndex == null || uiColumnIndex == null ) {
return;
}

final int uiColumnIndex = sc.getColumnIndex();
final GridColumn<?> column = gridModel.getColumns().get( uiColumnIndex );
if ( column instanceof IsRowDragHandle ) {
rowContextMenu.show( eventX,
Expand All @@ -108,7 +109,8 @@ public void onContextMenu( final ContextMenuEvent event ) {
cellContextMenu.show( eventX,
eventY );
}
selectCell( sc,
selectCell( uiRowIndex,
uiColumnIndex,
gridView,
isShiftKeyDown,
isControlKeyDown );
Expand All @@ -127,33 +129,32 @@ private int getRelativeY( final ContextMenuEvent event ) {
return e.getClientY() - target.getAbsoluteTop() + target.getScrollTop() + target.getOwnerDocument().getScrollTop();
}

private void selectCell( final GridData.SelectedCell sc,
private void selectCell( final int uiRowIndex,
final int uiColumnIndex,
final GuidedDecisionTableView gridView,
final boolean isShiftKeyDown,
final boolean isControlKeyDown ) {
//Lookup CellSelectionManager for cell
final int uiRowIndex = sc.getRowIndex();
final int uiColumnIndex = sc.getColumnIndex();
final GridData gridModel = gridView.getModel();

CellSelectionManager selectionManager;
CellSelectionStrategy selectionStrategy;
final GridCell<?> cell = gridModel.getCell( uiRowIndex,
uiColumnIndex );
if ( cell == null ) {
selectionManager = CellRangeSelectionManager.INSTANCE;
selectionStrategy = RangeSelectionStrategy.INSTANCE;
} else {
selectionManager = cell.getSelectionManager();
selectionStrategy = cell.getSelectionManager();
}
if ( selectionManager == null ) {
if ( selectionStrategy == null ) {
return;
}

//Handle selection
if ( selectionManager.handleSelection( gridModel,
uiRowIndex,
uiColumnIndex,
isShiftKeyDown,
isControlKeyDown ) ) {
if ( selectionStrategy.handleSelection( gridModel,
uiRowIndex,
uiColumnIndex,
isShiftKeyDown,
isControlKeyDown ) ) {
gridView.getLayer().batch();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
import javax.inject.Inject;

import com.ait.lienzo.client.core.types.Point2D;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.Command;
import org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52;
Expand All @@ -46,6 +43,16 @@
import org.uberfire.backend.vfs.ObservablePath;
import org.uberfire.ext.wires.core.grids.client.model.Bounds;
import org.uberfire.ext.wires.core.grids.client.widget.grid.GridWidget;
import org.uberfire.ext.wires.core.grids.client.widget.grid.impl.BaseGridWidgetKeyboardHandler;
import org.uberfire.ext.wires.core.grids.client.widget.grid.impl.KeyboardOperationClearCell;
import org.uberfire.ext.wires.core.grids.client.widget.grid.impl.KeyboardOperationEditCell;
import org.uberfire.ext.wires.core.grids.client.widget.grid.impl.KeyboardOperationMoveDown;
import org.uberfire.ext.wires.core.grids.client.widget.grid.impl.KeyboardOperationMoveLeft;
import org.uberfire.ext.wires.core.grids.client.widget.grid.impl.KeyboardOperationMoveRight;
import org.uberfire.ext.wires.core.grids.client.widget.grid.impl.KeyboardOperationMoveUp;
import org.uberfire.ext.wires.core.grids.client.widget.grid.impl.KeyboardOperationSelectBottomRightCell;
import org.uberfire.ext.wires.core.grids.client.widget.grid.impl.KeyboardOperationSelectTopLeftCell;
import org.uberfire.ext.wires.core.grids.client.widget.layer.GridLayer;
import org.uberfire.ext.wires.core.grids.client.widget.layer.pinning.TransformMediator;
import org.uberfire.mvp.ParameterizedCommand;
import org.uberfire.mvp.PlaceRequest;
Expand Down Expand Up @@ -74,15 +81,23 @@ public GuidedDecisionTableModellerPresenter( final GuidedDecisionTableModellerVi
this.pinnedEvent = pinnedEvent;
this.view.init( this );

//Add support for deleting cells' content with the DELETE key
handlerRegistrations.add( view.addKeyDownHandler( new KeyDownHandler() {
@Override
public void onKeyDown( final KeyDownEvent event ) {
if ( event.getNativeKeyCode() == KeyCodes.KEY_DELETE ) {
getActiveDecisionTable().onDeleteSelectedCells();
}
}
} ) );
//Add support for keyboard operations
final GridLayer layer = view.getGridLayerView();
final BaseGridWidgetKeyboardHandler handler = new BaseGridWidgetKeyboardHandler( layer );
handler.addOperation( new KeyboardOperationClearCell( layer ) {
@Override
protected void clearCells( final GridWidget gridWidget ) {
getActiveDecisionTable().onDeleteSelectedCells();
}
},
new KeyboardOperationEditCell( layer ),
new KeyboardOperationMoveLeft( layer ),
new KeyboardOperationMoveRight( layer ),
new KeyboardOperationMoveUp( layer ),
new KeyboardOperationMoveDown( layer ),
new KeyboardOperationSelectTopLeftCell( layer ),
new KeyboardOperationSelectBottomRightCell( layer ) );
handlerRegistrations.add( view.addKeyDownHandler( handler ) );

//Add support for context menus
handlerRegistrations.add( view.addContextMenuHandler( contextMenuSupport.getContextMenuHandler( this ) ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
import org.uberfire.ext.wires.core.grids.client.model.impl.BaseGridRow;
import org.uberfire.ext.wires.core.grids.client.widget.dom.HasDOMElementResources;
import org.uberfire.ext.wires.core.grids.client.widget.grid.GridWidget;
import org.uberfire.ext.wires.core.grids.client.widget.grid.selections.impl.RowSelectionManager;
import org.uberfire.ext.wires.core.grids.client.widget.grid.selections.impl.RowSelectionStrategy;
import org.uberfire.ext.wires.core.grids.client.widget.layer.pinning.TransformMediator;
import org.uberfire.mvp.ParameterizedCommand;
import org.uberfire.mvp.PlaceRequest;
Expand Down Expand Up @@ -1212,7 +1212,7 @@ private void initialiseRow( final List<BaseColumn> columns,
//Set-up SelectionManager for Row Number column, to select entire row.
if ( modelColumn instanceof RowNumberCol52 ) {
uiModel.getCell( rowIndex,
iModelColumn ).setSelectionManager( RowSelectionManager.INSTANCE );
iModelColumn ).setSelectionManager( RowSelectionStrategy.INSTANCE );
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.uberfire.ext.wires.core.grids.client.model.GridData;
import org.uberfire.ext.wires.core.grids.client.model.GridRow;
import org.uberfire.ext.wires.core.grids.client.model.impl.BaseGridRow;
import org.uberfire.ext.wires.core.grids.client.widget.grid.selections.impl.RowSelectionManager;
import org.uberfire.ext.wires.core.grids.client.widget.grid.selections.impl.RowSelectionStrategy;

import static org.drools.workbench.screens.guided.dtable.client.widget.table.model.synchronizers.impl.RowSynchronizer.*;

Expand Down Expand Up @@ -126,7 +126,7 @@ private void initialiseRowData( final int rowIndex ) {
//Set-up SelectionManager for Row Number column, to select entire row.
if ( modelColumn instanceof RowNumberCol52 ) {
uiModel.getCell( rowIndex,
columnIndex ).setSelectionManager( RowSelectionManager.INSTANCE );
columnIndex ).setSelectionManager( RowSelectionStrategy.INSTANCE );
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
import org.uberfire.ext.wires.core.grids.client.widget.grid.renderers.columns.GridColumnRenderer;
import org.uberfire.ext.wires.core.grids.client.widget.grid.renderers.grids.GridRenderer;
import org.uberfire.ext.wires.core.grids.client.widget.grid.renderers.grids.impl.BaseGridRendererHelper;
import org.uberfire.ext.wires.core.grids.client.widget.grid.selections.CellSelectionManager;
import org.uberfire.ext.wires.core.grids.client.widget.grid.selections.CellSelectionStrategy;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
Expand Down Expand Up @@ -177,25 +177,25 @@ public void onContextMenu_CellContextMenu() {
public void onContextMenuWithCellSelectionManagerWithChangeInSelection() {
final GridCell uiCell = mock( GridCell.class );
final GridColumn uiColumn = new RowNumberColumn();
final CellSelectionManager cellSelectionManager = mock( CellSelectionManager.class );
final CellSelectionStrategy cellSelectionStrategy = mock( CellSelectionStrategy.class );

uiModel.appendColumn( uiColumn );

when( columnInformation.getColumn() ).thenReturn( uiColumn );
when( uiModel.getCell( any( Integer.class ),
any( Integer.class ) ) ).thenReturn( uiCell );
when( uiCell.getSelectionManager() ).thenReturn( cellSelectionManager );
when( cellSelectionManager.handleSelection( any( GridData.class ),
any( Integer.class ),
any( Integer.class ),
any( Boolean.class ),
any( Boolean.class ) ) ).thenReturn( true );
when( uiCell.getSelectionManager() ).thenReturn( cellSelectionStrategy );
when( cellSelectionStrategy.handleSelection( any( GridData.class ),
any( Integer.class ),
any( Integer.class ),
any( Boolean.class ),
any( Boolean.class ) ) ).thenReturn( true );

final ContextMenuHandler handler = contextMenuSupport.getContextMenuHandler( modellerPresenter );

handler.onContextMenu( event );

verify( cellSelectionManager,
verify( cellSelectionStrategy,
times( 1 ) ).handleSelection( eq( uiModel ),
eq( 0 ),
eq( 0 ),
Expand All @@ -210,20 +210,20 @@ public void onContextMenuWithCellSelectionManagerWithChangeInSelection() {
public void onContextMenuWithCellSelectionManagerWithoutChangeInSelection() {
final GridCell uiCell = mock( GridCell.class );
final GridColumn uiColumn = new RowNumberColumn();
final CellSelectionManager cellSelectionManager = mock( CellSelectionManager.class );
final CellSelectionStrategy cellSelectionStrategy = mock( CellSelectionStrategy.class );

uiModel.appendColumn( uiColumn );

when( columnInformation.getColumn() ).thenReturn( uiColumn );
when( uiModel.getCell( any( Integer.class ),
any( Integer.class ) ) ).thenReturn( uiCell );
when( uiCell.getSelectionManager() ).thenReturn( cellSelectionManager );
when( uiCell.getSelectionManager() ).thenReturn( cellSelectionStrategy );

final ContextMenuHandler handler = contextMenuSupport.getContextMenuHandler( modellerPresenter );

handler.onContextMenu( event );

verify( cellSelectionManager,
verify( cellSelectionStrategy,
times( 1 ) ).handleSelection( eq( uiModel ),
eq( 0 ),
eq( 0 ),
Expand Down

0 comments on commit 2ac5479

Please sign in to comment.