aboutsummaryrefslogtreecommitdiffstats
path: root/client
diff options
context:
space:
mode:
authorDenis <denis@vaadin.com>2017-01-13 12:37:54 +0200
committerPekka Hyvönen <pekka@vaadin.com>2017-01-13 12:37:54 +0200
commit40cb64fbab832279ab13bd853bf1096da92abc39 (patch)
treecf1660013f272fc3252cb4593277f827386a3955 /client
parent8fca887996e063470379dcf3b47d6bcff3c9839c (diff)
downloadvaadin-framework-40cb64fbab832279ab13bd853bf1096da92abc39.tar.gz
vaadin-framework-40cb64fbab832279ab13bd853bf1096da92abc39.zip
Add "deselect allowed" feature to the Grid. (#8227)
* Add "deselect allowed" feature to the Grid. Fixes #8149
Diffstat (limited to 'client')
-rw-r--r--client/src/main/java/com/vaadin/client/connectors/grid/AbstractSelectionModelConnector.java25
-rw-r--r--client/src/main/java/com/vaadin/client/connectors/grid/GridConnector.java19
-rw-r--r--client/src/main/java/com/vaadin/client/connectors/grid/SingleSelectionModelConnector.java51
3 files changed, 76 insertions, 19 deletions
diff --git a/client/src/main/java/com/vaadin/client/connectors/grid/AbstractSelectionModelConnector.java b/client/src/main/java/com/vaadin/client/connectors/grid/AbstractSelectionModelConnector.java
index 114bc313d6..2210317c7c 100644
--- a/client/src/main/java/com/vaadin/client/connectors/grid/AbstractSelectionModelConnector.java
+++ b/client/src/main/java/com/vaadin/client/connectors/grid/AbstractSelectionModelConnector.java
@@ -19,6 +19,7 @@ import com.vaadin.client.ServerConnector;
import com.vaadin.client.annotations.OnStateChange;
import com.vaadin.client.extensions.AbstractExtensionConnector;
import com.vaadin.client.widget.grid.selection.SelectionModel;
+import com.vaadin.client.widget.grid.selection.SpaceSelectHandler;
import com.vaadin.client.widgets.Grid;
import com.vaadin.shared.ui.grid.AbstractSelectionModelState;
@@ -34,9 +35,24 @@ import elemental.json.JsonObject;
public abstract class AbstractSelectionModelConnector
extends AbstractExtensionConnector {
+ private SpaceSelectHandler<JsonObject> spaceSelectHandler;
+
@Override
protected void extend(ServerConnector target) {
initSelectionModel();
+
+ // Default selection style is space key.
+ spaceSelectHandler = new SpaceSelectHandler<>(getGrid());
+ }
+
+ @Override
+ public void onUnregister() {
+ super.onUnregister();
+
+ if (spaceSelectHandler != null) {
+ spaceSelectHandler.removeHandler();
+ spaceSelectHandler = null;
+ }
}
/**
@@ -62,6 +78,15 @@ public abstract class AbstractSelectionModelConnector
return getParent().getWidget();
}
+ /**
+ * Gets space selection handler registered for the Grid.
+ *
+ * @return space selection handler
+ */
+ protected SpaceSelectHandler<JsonObject> getSpaceSelectionHandler() {
+ return spaceSelectHandler;
+ }
+
@OnStateChange("selectionAllowed")
private void onSelectionAllowedChange() {
getGrid().getSelectionModel()
diff --git a/client/src/main/java/com/vaadin/client/connectors/grid/GridConnector.java b/client/src/main/java/com/vaadin/client/connectors/grid/GridConnector.java
index 828c795777..2e48e3a64a 100644
--- a/client/src/main/java/com/vaadin/client/connectors/grid/GridConnector.java
+++ b/client/src/main/java/com/vaadin/client/connectors/grid/GridConnector.java
@@ -47,8 +47,6 @@ import com.vaadin.client.widget.grid.events.BodyClickHandler;
import com.vaadin.client.widget.grid.events.BodyDoubleClickHandler;
import com.vaadin.client.widget.grid.events.GridClickEvent;
import com.vaadin.client.widget.grid.events.GridDoubleClickEvent;
-import com.vaadin.client.widget.grid.selection.ClickSelectHandler;
-import com.vaadin.client.widget.grid.selection.SpaceSelectHandler;
import com.vaadin.client.widget.grid.sort.SortEvent;
import com.vaadin.client.widget.grid.sort.SortOrder;
import com.vaadin.client.widgets.Grid;
@@ -111,8 +109,6 @@ public class GridConnector extends AbstractListingConnector
/* Child component list for HasComponentsConnector */
private List<ComponentConnector> childComponents;
- private SpaceSelectHandler<JsonObject> spaceSelectHandler;
- private ClickSelectHandler<JsonObject> clickSelectHandler;
private ItemClickHandler itemClickHandler = new ItemClickHandler();
/**
@@ -147,8 +143,6 @@ public class GridConnector extends AbstractListingConnector
protected void init() {
super.init();
- // Default selection style is space key.
- spaceSelectHandler = new SpaceSelectHandler<>(getWidget());
getWidget().addSortHandler(this::handleSortEvent);
getWidget().setRowStyleGenerator(rowRef -> {
JsonObject json = rowRef.getRow();
@@ -344,12 +338,6 @@ public class GridConnector extends AbstractListingConnector
super.onUnregister();
columnToIdMap.clear();
- removeClickHandler();
-
- if (spaceSelectHandler != null) {
- spaceSelectHandler.removeHandler();
- spaceSelectHandler = null;
- }
}
@Override
@@ -415,13 +403,6 @@ public class GridConnector extends AbstractListingConnector
return (GridState) super.getState();
}
- private void removeClickHandler() {
- if (clickSelectHandler != null) {
- clickSelectHandler.removeHandler();
- clickSelectHandler = null;
- }
- }
-
@Override
public boolean hasTooltip() {
// Always check for generated descriptions.
diff --git a/client/src/main/java/com/vaadin/client/connectors/grid/SingleSelectionModelConnector.java b/client/src/main/java/com/vaadin/client/connectors/grid/SingleSelectionModelConnector.java
index 9b1d7cc27a..560ccb2d85 100644
--- a/client/src/main/java/com/vaadin/client/connectors/grid/SingleSelectionModelConnector.java
+++ b/client/src/main/java/com/vaadin/client/connectors/grid/SingleSelectionModelConnector.java
@@ -15,12 +15,14 @@
*/
package com.vaadin.client.connectors.grid;
+import com.vaadin.client.annotations.OnStateChange;
import com.vaadin.client.widget.grid.events.GridSelectionAllowedEvent;
import com.vaadin.client.widget.grid.selection.ClickSelectHandler;
import com.vaadin.client.widget.grid.selection.SelectionModel;
import com.vaadin.shared.data.DataCommunicatorConstants;
import com.vaadin.shared.data.selection.SelectionServerRpc;
import com.vaadin.shared.ui.Connect;
+import com.vaadin.shared.ui.grid.SingleSelectionModelState;
import elemental.json.JsonObject;
@@ -44,6 +46,8 @@ public class SingleSelectionModelConnector
private boolean isSelectionAllowed = true;
+ private boolean deselectAllowed = true;
+
@Override
public void select(JsonObject item) {
getRpcProxy(SelectionServerRpc.class)
@@ -77,6 +81,39 @@ public class SingleSelectionModelConnector
public boolean isSelectionAllowed() {
return isSelectionAllowed;
}
+
+ /**
+ * 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) {
+ this.deselectAllowed = deselectAllowed;
+ updateHandlerDeselectAllowed();
+ }
+
+ /**
+ * Gets 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() {
+ return deselectAllowed;
+ }
+
+ private void updateHandlerDeselectAllowed() {
+ if (clickSelectHandler != null) {
+ clickSelectHandler.setDeselectAllowed(deselectAllowed);
+ }
+ getSpaceSelectionHandler().setDeselectAllowed(deselectAllowed);
+ }
+
}
@Override
@@ -94,4 +131,18 @@ public class SingleSelectionModelConnector
}
}
+ @Override
+ public SingleSelectionModelState getState() {
+ return (SingleSelectionModelState) super.getState();
+ }
+
+ @OnStateChange("deselectAllowed")
+ private void updateDeselectAllowed() {
+ getSelectionModel().setDeselectAllowed(getState().deselectAllowed);
+ }
+
+ private SingleSelectionModel getSelectionModel() {
+ return (SingleSelectionModel) getGrid().getSelectionModel();
+ }
+
}