diff options
author | Leif Åstrand <leif@vaadin.com> | 2015-02-05 21:04:36 +0200 |
---|---|---|
committer | Leif Åstrand <leif@vaadin.com> | 2015-02-06 12:59:49 +0200 |
commit | fa5f781429fe06f6c662f1a44d0514f9477f5ce0 (patch) | |
tree | c251cefb51d6570631d7309d3afebe9df80d968a /client | |
parent | 276797fad8642c68b2201f3752d025a231519e8f (diff) | |
download | vaadin-framework-fa5f781429fe06f6c662f1a44d0514f9477f5ce0.tar.gz vaadin-framework-fa5f781429fe06f6c662f1a44d0514f9477f5ce0.zip |
Add API for controlling deselection for single select model (#16567)
Change-Id: Ieb245205b3a311a4563f39bc48baadc44e218b61
Diffstat (limited to 'client')
5 files changed, 88 insertions, 3 deletions
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<JsonObject> model = getWidget().getSelectionModel(); + if (model instanceof SelectionModel.Single<?>) { + ((SelectionModel.Single<?>) model) + .setDeselectAllowed(getState().singleSelectDeselectAllowed); + } + } + private void updateColumnOrderFromState(List<String> 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<T> { private Grid<T> grid; private HandlerRegistration clickHandler; + private boolean deselectAllowed = true; private class RowClickHandler implements BodyClickHandler { @@ -38,6 +39,8 @@ public class ClickSelectHandler<T> { 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<T> { public void removeHandler() { clickHandler.removeHandler(); } + + /** + * Sets whether clicking the currently selected row should deselect the row. + * + * @param deselectAllowed + * <code>true</code> to allow deselecting the selected row; + * otherwise <code>false</code> + */ + 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<T> { */ public T getSelectedRow(); + /** + * Sets whether it's allowed to deselect the selected row through the + * UI. Deselection is allowed by default. + * + * @param deselectAllowed + * <code>true</code> if the selected row can be deselected + * without selecting another row instead; otherwise + * <code>false</code>. + */ + public void setDeselectAllowed(boolean deselectAllowed); + + /** + * Sets whether it's allowed to deselect the selected row through the + * UI. + * + * @return <code>true</code> if deselection is allowed; otherwise + * <code>false</code> + */ + 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<T> extends AbstractRowHandleSelectionModel<T> /** Event handling for selection by clicking cells */ private ClickSelectHandler<T> clickSelectHandler; + private boolean deselectAllowed = true; + @Override public boolean isSelected(T row) { return selectedRow != null @@ -66,6 +68,7 @@ public class SelectionModelSingle<T> extends AbstractRowHandleSelectionModel<T> if (this.grid != null) { spaceSelectHandler = new SpaceSelectHandler<T>(grid); clickSelectHandler = new ClickSelectHandler<T>(grid); + updateHandlerDeselectAllowed(); } else { spaceSelectHandler.removeHandler(); clickSelectHandler.removeHandler(); @@ -148,4 +151,25 @@ public class SelectionModelSingle<T> extends AbstractRowHandleSelectionModel<T> 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<T> { protected void setSelected(Grid<T> 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<T> { private Grid<T> 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<T> { spaceDownHandler.removeHandler(); spaceUpHandler.removeHandler(); } + + /** + * Sets whether pressing space for the currently selected row should + * deselect the row. + * + * @param deselectAllowed + * <code>true</code> to allow deselecting the selected row; + * otherwise <code>false</code> + */ + public void setDeselectAllowed(boolean deselectAllowed) { + this.deselectAllowed = deselectAllowed; + } }
\ No newline at end of file |