From fa5f781429fe06f6c662f1a44d0514f9477f5ce0 Mon Sep 17 00:00:00 2001 From: Leif Åstrand Date: Thu, 5 Feb 2015 21:04:36 +0200 Subject: Add API for controlling deselection for single select model (#16567) Change-Id: Ieb245205b3a311a4563f39bc48baadc44e218b61 --- .../vaadin/client/connectors/GridConnector.java | 14 +++++++++++++ .../widget/grid/selection/ClickSelectHandler.java | 14 +++++++++++++ .../widget/grid/selection/SelectionModel.java | 20 ++++++++++++++++++ .../grid/selection/SelectionModelSingle.java | 24 ++++++++++++++++++++++ .../widget/grid/selection/SpaceSelectHandler.java | 19 ++++++++++++++--- 5 files changed, 88 insertions(+), 3 deletions(-) (limited to 'client') diff --git a/client/src/com/vaadin/client/connectors/GridConnector.java b/client/src/com/vaadin/client/connectors/GridConnector.java index f8aa044a8d..f263b47642 100644 --- a/client/src/com/vaadin/client/connectors/GridConnector.java +++ b/client/src/com/vaadin/client/connectors/GridConnector.java @@ -57,6 +57,7 @@ import com.vaadin.client.widget.grid.events.SelectAllHandler; import com.vaadin.client.widget.grid.selection.AbstractRowHandleSelectionModel; import com.vaadin.client.widget.grid.selection.SelectionEvent; import com.vaadin.client.widget.grid.selection.SelectionHandler; +import com.vaadin.client.widget.grid.selection.SelectionModel; import com.vaadin.client.widget.grid.selection.SelectionModelMulti; import com.vaadin.client.widget.grid.selection.SelectionModelNone; import com.vaadin.client.widget.grid.selection.SelectionModelSingle; @@ -536,7 +537,12 @@ public class GridConnector extends AbstractHasComponentsConnector implements // Selection if (stateChangeEvent.hasPropertyChanged("selectionMode")) { onSelectionModeChange(); + updateSelectDeselectAllowed(); + } else if (stateChangeEvent + .hasPropertyChanged("singleSelectDeselectAllowed")) { + updateSelectDeselectAllowed(); } + if (stateChangeEvent.hasPropertyChanged("selectedKeys")) { updateSelectionFromState(); } @@ -567,6 +573,14 @@ public class GridConnector extends AbstractHasComponentsConnector implements } } + private void updateSelectDeselectAllowed() { + SelectionModel model = getWidget().getSelectionModel(); + if (model instanceof SelectionModel.Single) { + ((SelectionModel.Single) model) + .setDeselectAllowed(getState().singleSelectDeselectAllowed); + } + } + private void updateColumnOrderFromState(List stateColumnOrder) { CustomGridColumn[] columns = new CustomGridColumn[stateColumnOrder .size()]; diff --git a/client/src/com/vaadin/client/widget/grid/selection/ClickSelectHandler.java b/client/src/com/vaadin/client/widget/grid/selection/ClickSelectHandler.java index 0a1154e787..c6bc52dd1c 100644 --- a/client/src/com/vaadin/client/widget/grid/selection/ClickSelectHandler.java +++ b/client/src/com/vaadin/client/widget/grid/selection/ClickSelectHandler.java @@ -30,6 +30,7 @@ public class ClickSelectHandler { private Grid grid; private HandlerRegistration clickHandler; + private boolean deselectAllowed = true; private class RowClickHandler implements BodyClickHandler { @@ -38,6 +39,8 @@ public class ClickSelectHandler { T row = (T) event.getTargetCell().getRow(); if (!grid.isSelected(row)) { grid.select(row); + } else if (deselectAllowed) { + grid.deselect(row); } } } @@ -60,4 +63,15 @@ public class ClickSelectHandler { public void removeHandler() { clickHandler.removeHandler(); } + + /** + * Sets whether clicking the currently selected row should deselect the row. + * + * @param deselectAllowed + * true to allow deselecting the selected row; + * otherwise false + */ + public void setDeselectAllowed(boolean deselectAllowed) { + this.deselectAllowed = deselectAllowed; + } } diff --git a/client/src/com/vaadin/client/widget/grid/selection/SelectionModel.java b/client/src/com/vaadin/client/widget/grid/selection/SelectionModel.java index 37f6fb48c3..ec36ab52e8 100644 --- a/client/src/com/vaadin/client/widget/grid/selection/SelectionModel.java +++ b/client/src/com/vaadin/client/widget/grid/selection/SelectionModel.java @@ -116,6 +116,26 @@ public interface SelectionModel { */ public T getSelectedRow(); + /** + * Sets whether it's allowed to deselect the selected row through the + * UI. Deselection is allowed by default. + * + * @param deselectAllowed + * true if the selected row can be deselected + * without selecting another row instead; otherwise + * false. + */ + public void setDeselectAllowed(boolean deselectAllowed); + + /** + * Sets whether it's allowed to deselect the selected row through the + * UI. + * + * @return true if deselection is allowed; otherwise + * false + */ + public boolean isDeselectAllowed(); + } /** diff --git a/client/src/com/vaadin/client/widget/grid/selection/SelectionModelSingle.java b/client/src/com/vaadin/client/widget/grid/selection/SelectionModelSingle.java index 20eb3c1e63..38605db12c 100644 --- a/client/src/com/vaadin/client/widget/grid/selection/SelectionModelSingle.java +++ b/client/src/com/vaadin/client/widget/grid/selection/SelectionModelSingle.java @@ -40,6 +40,8 @@ public class SelectionModelSingle extends AbstractRowHandleSelectionModel /** Event handling for selection by clicking cells */ private ClickSelectHandler clickSelectHandler; + private boolean deselectAllowed = true; + @Override public boolean isSelected(T row) { return selectedRow != null @@ -66,6 +68,7 @@ public class SelectionModelSingle extends AbstractRowHandleSelectionModel if (this.grid != null) { spaceSelectHandler = new SpaceSelectHandler(grid); clickSelectHandler = new ClickSelectHandler(grid); + updateHandlerDeselectAllowed(); } else { spaceSelectHandler.removeHandler(); clickSelectHandler.removeHandler(); @@ -148,4 +151,25 @@ public class SelectionModelSingle extends AbstractRowHandleSelectionModel return false; } } + + @Override + public void setDeselectAllowed(boolean deselectAllowed) { + this.deselectAllowed = deselectAllowed; + updateHandlerDeselectAllowed(); + } + + @Override + public boolean isDeselectAllowed() { + return deselectAllowed; + } + + private void updateHandlerDeselectAllowed() { + if (spaceSelectHandler != null) { + spaceSelectHandler.setDeselectAllowed(deselectAllowed); + } + if (clickSelectHandler != null) { + clickSelectHandler.setDeselectAllowed(deselectAllowed); + } + } + } diff --git a/client/src/com/vaadin/client/widget/grid/selection/SpaceSelectHandler.java b/client/src/com/vaadin/client/widget/grid/selection/SpaceSelectHandler.java index 7a1bf2dc06..3e04a6dfac 100644 --- a/client/src/com/vaadin/client/widget/grid/selection/SpaceSelectHandler.java +++ b/client/src/com/vaadin/client/widget/grid/selection/SpaceSelectHandler.java @@ -79,10 +79,10 @@ public class SpaceSelectHandler { protected void setSelected(Grid grid, int rowIndex) { T row = grid.getDataSource().getRow(rowIndex); - if (grid.isSelected(row)) { - grid.deselect(row); - } else { + if (!grid.isSelected(row)) { grid.select(row); + } else if (deselectAllowed) { + grid.deselect(row); } } } @@ -91,6 +91,7 @@ public class SpaceSelectHandler { private Grid grid; private HandlerRegistration spaceUpHandler; private HandlerRegistration spaceDownHandler; + private boolean deselectAllowed = true; /** * Constructor for SpaceSelectHandler. This constructor will add all @@ -121,4 +122,16 @@ public class SpaceSelectHandler { spaceDownHandler.removeHandler(); spaceUpHandler.removeHandler(); } + + /** + * Sets whether pressing space for the currently selected row should + * deselect the row. + * + * @param deselectAllowed + * true to allow deselecting the selected row; + * otherwise false + */ + public void setDeselectAllowed(boolean deselectAllowed) { + this.deselectAllowed = deselectAllowed; + } } \ No newline at end of file -- cgit v1.2.3