summaryrefslogtreecommitdiffstats
path: root/client
diff options
context:
space:
mode:
authorArtur <artur@vaadin.com>2017-01-30 13:47:55 +0200
committerGitHub <noreply@github.com>2017-01-30 13:47:55 +0200
commit131e4f34bddf99d61a76fcd49890490c78c3efa8 (patch)
tree1276e94ddbec6a8fc612bedf87835cd366e5f0a5 /client
parent870a4d9ab386de41b0c5e344d16ebfd9c13b2951 (diff)
downloadvaadin-framework-131e4f34bddf99d61a76fcd49890490c78c3efa8.tar.gz
vaadin-framework-131e4f34bddf99d61a76fcd49890490c78c3efa8.zip
Make it possible to disallow user selection in Grid (#8144)
Fixes #7880
Diffstat (limited to 'client')
-rw-r--r--client/src/main/java/com/vaadin/client/connectors/MultiSelectionModelConnector.java45
-rw-r--r--client/src/main/java/com/vaadin/client/connectors/SingleSelectionModelConnector.java36
-rw-r--r--client/src/main/java/com/vaadin/client/widget/grid/selection/ClickSelectHandler.java4
-rw-r--r--client/src/main/java/com/vaadin/client/widget/grid/selection/HasUserSelectionAllowed.java44
-rw-r--r--client/src/main/java/com/vaadin/client/widget/grid/selection/MultiSelectionRenderer.java6
-rw-r--r--client/src/main/java/com/vaadin/client/widget/grid/selection/SelectionModelMulti.java13
-rw-r--r--client/src/main/java/com/vaadin/client/widget/grid/selection/SelectionModelSingle.java13
-rw-r--r--client/src/main/java/com/vaadin/client/widget/grid/selection/SpaceSelectHandler.java4
-rwxr-xr-xclient/src/main/java/com/vaadin/client/widgets/Grid.java57
9 files changed, 216 insertions, 6 deletions
diff --git a/client/src/main/java/com/vaadin/client/connectors/MultiSelectionModelConnector.java b/client/src/main/java/com/vaadin/client/connectors/MultiSelectionModelConnector.java
index 748e9b1acf..5c0a84bd5f 100644
--- a/client/src/main/java/com/vaadin/client/connectors/MultiSelectionModelConnector.java
+++ b/client/src/main/java/com/vaadin/client/connectors/MultiSelectionModelConnector.java
@@ -22,6 +22,7 @@ import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
+import java.util.logging.Logger;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.CheckBox;
@@ -35,12 +36,15 @@ import com.vaadin.client.widget.grid.DataAvailableEvent;
import com.vaadin.client.widget.grid.DataAvailableHandler;
import com.vaadin.client.widget.grid.events.SelectAllEvent;
import com.vaadin.client.widget.grid.events.SelectAllHandler;
+import com.vaadin.client.widget.grid.selection.HasUserSelectionAllowed;
import com.vaadin.client.widget.grid.selection.MultiSelectionRenderer;
import com.vaadin.client.widget.grid.selection.SelectionModel;
import com.vaadin.client.widget.grid.selection.SelectionModel.Multi;
import com.vaadin.client.widget.grid.selection.SpaceSelectHandler;
import com.vaadin.client.widgets.Grid;
+import com.vaadin.client.widgets.Grid.Column;
import com.vaadin.client.widgets.Grid.HeaderCell;
+import com.vaadin.client.widgets.Grid.SelectionColumn;
import com.vaadin.shared.ui.Connect;
import com.vaadin.shared.ui.grid.GridState;
import com.vaadin.shared.ui.grid.Range;
@@ -94,8 +98,30 @@ public class MultiSelectionModelConnector extends
}
}
+ @OnStateChange("userSelectionAllowed")
+ void updateUserSelectionAllowed() {
+ if (selectionModel instanceof HasUserSelectionAllowed) {
+ ((HasUserSelectionAllowed) selectionModel)
+ .setUserSelectionAllowed(getState().userSelectionAllowed);
+ } else {
+ getLogger().warning("userSelectionAllowed set to "
+ + getState().userSelectionAllowed
+ + " but the selection model does not implement "
+ + HasUserSelectionAllowed.class.getSimpleName());
+ }
+ }
+
+ private static Logger getLogger() {
+ return Logger.getLogger(MultiSelectionModelConnector.class.getName());
+ }
+
+ /**
+ * The default multi selection model used for this connector.
+ *
+ */
protected class MultiSelectionModel extends AbstractSelectionModel
- implements SelectionModel.Multi.Batched<JsonObject> {
+ implements SelectionModel.Multi.Batched<JsonObject>,
+ HasUserSelectionAllowed<JsonObject> {
private ComplexRenderer<Boolean> renderer = null;
private Set<RowHandle<JsonObject>> selected = new HashSet<RowHandle<JsonObject>>();
@@ -104,6 +130,7 @@ public class MultiSelectionModelConnector extends
private HandlerRegistration dataAvailable;
private Range availableRows;
private boolean batchSelect = false;
+ private boolean userSelectionAllowed = true;
@Override
public void setGrid(Grid<JsonObject> grid) {
@@ -382,5 +409,21 @@ public class MultiSelectionModelConnector extends
public Collection<JsonObject> getDeselectedRowsBatch() {
return Collections.unmodifiableSet(getRows(deselected));
}
+
+ @Override
+ public boolean isUserSelectionAllowed() {
+ return userSelectionAllowed;
+ }
+
+ @Override
+ public void setUserSelectionAllowed(boolean userSelectionAllowed) {
+ this.userSelectionAllowed = userSelectionAllowed;
+ for (Column<?, ?> c : getGrid().getColumns()) {
+ if (c instanceof SelectionColumn) {
+ ((SelectionColumn) c)
+ .setUserSelectionAllowed(userSelectionAllowed);
+ }
+ }
+ }
}
}
diff --git a/client/src/main/java/com/vaadin/client/connectors/SingleSelectionModelConnector.java b/client/src/main/java/com/vaadin/client/connectors/SingleSelectionModelConnector.java
index 55c1eddf61..7cd30e40ef 100644
--- a/client/src/main/java/com/vaadin/client/connectors/SingleSelectionModelConnector.java
+++ b/client/src/main/java/com/vaadin/client/connectors/SingleSelectionModelConnector.java
@@ -15,11 +15,14 @@
*/
package com.vaadin.client.connectors;
+import java.util.logging.Logger;
+
import com.vaadin.client.ServerConnector;
import com.vaadin.client.annotations.OnStateChange;
import com.vaadin.client.data.DataSource.RowHandle;
import com.vaadin.client.renderers.Renderer;
import com.vaadin.client.widget.grid.selection.ClickSelectHandler;
+import com.vaadin.client.widget.grid.selection.HasUserSelectionAllowed;
import com.vaadin.client.widget.grid.selection.SelectionModel;
import com.vaadin.client.widget.grid.selection.SelectionModel.Single;
import com.vaadin.client.widget.grid.selection.SpaceSelectHandler;
@@ -75,14 +78,34 @@ public class SingleSelectionModelConnector extends
selectionModel.setDeselectAllowed(getState().deselectAllowed);
}
+ @OnStateChange("userSelectionAllowed")
+ void updateUserSelectionAllowed() {
+
+ if (selectionModel instanceof HasUserSelectionAllowed) {
+ ((HasUserSelectionAllowed) selectionModel)
+ .setUserSelectionAllowed(getState().userSelectionAllowed);
+ } else {
+ getLogger().warning("userSelectionAllowed set to "
+ + getState().userSelectionAllowed
+ + " but the selection model does not implement "
+ + HasUserSelectionAllowed.class.getSimpleName());
+ }
+ }
+
+ private static Logger getLogger() {
+ return Logger.getLogger(SingleSelectionModelConnector.class.getName());
+ }
+
/**
* SingleSelectionModel without a selection column renderer.
*/
public class SingleSelectionModel extends AbstractSelectionModel
- implements SelectionModel.Single<JsonObject> {
+ implements SelectionModel.Single<JsonObject>,
+ HasUserSelectionAllowed<JsonObject> {
private RowHandle<JsonObject> selectedRow;
private boolean deselectAllowed;
+ private boolean userSelectionAllowed = true;
@Override
public Renderer<Boolean> getSelectionColumnRenderer() {
@@ -182,5 +205,16 @@ public class SingleSelectionModelConnector extends
public boolean isDeselectAllowed() {
return deselectAllowed;
}
+
+ @Override
+ public boolean isUserSelectionAllowed() {
+ return userSelectionAllowed;
+ }
+
+ @Override
+ public void setUserSelectionAllowed(boolean userSelectionAllowed) {
+ this.userSelectionAllowed = userSelectionAllowed;
+ }
+
}
} \ No newline at end of file
diff --git a/client/src/main/java/com/vaadin/client/widget/grid/selection/ClickSelectHandler.java b/client/src/main/java/com/vaadin/client/widget/grid/selection/ClickSelectHandler.java
index e7be08f141..7ace1eb4a4 100644
--- a/client/src/main/java/com/vaadin/client/widget/grid/selection/ClickSelectHandler.java
+++ b/client/src/main/java/com/vaadin/client/widget/grid/selection/ClickSelectHandler.java
@@ -36,6 +36,10 @@ public class ClickSelectHandler<T> {
@Override
public void onClick(GridClickEvent event) {
+ if (!grid.isUserSelectionAllowed()) {
+ return;
+ }
+
T row = (T) event.getTargetCell().getRow();
if (!grid.isSelected(row)) {
grid.select(row);
diff --git a/client/src/main/java/com/vaadin/client/widget/grid/selection/HasUserSelectionAllowed.java b/client/src/main/java/com/vaadin/client/widget/grid/selection/HasUserSelectionAllowed.java
new file mode 100644
index 0000000000..de4eda36ca
--- /dev/null
+++ b/client/src/main/java/com/vaadin/client/widget/grid/selection/HasUserSelectionAllowed.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.client.widget.grid.selection;
+
+/**
+ * Interface implemented by selection models which support disabling client side
+ * selection while still allowing programmatic selection on the server.
+ *
+ * @param <T>
+ * Grid's row type
+ */
+public interface HasUserSelectionAllowed<T> extends SelectionModel<T> {
+
+ /**
+ * Checks if the user is allowed to change the selection.
+ *
+ * @return <code>true</code> if the user is allowed to change the selection,
+ * <code>false</code> otherwise
+ */
+ public boolean isUserSelectionAllowed();
+
+ /**
+ * Sets whether the user is allowed to change the selection.
+ *
+ * @param userSelectionAllowed
+ * <code>true</code> if the user is allowed to change the
+ * selection, <code>false</code> otherwise
+ */
+ public void setUserSelectionAllowed(boolean userSelectionAllowed);
+
+}
diff --git a/client/src/main/java/com/vaadin/client/widget/grid/selection/MultiSelectionRenderer.java b/client/src/main/java/com/vaadin/client/widget/grid/selection/MultiSelectionRenderer.java
index b27a4a2eed..9c8f60c0cd 100644
--- a/client/src/main/java/com/vaadin/client/widget/grid/selection/MultiSelectionRenderer.java
+++ b/client/src/main/java/com/vaadin/client/widget/grid/selection/MultiSelectionRenderer.java
@@ -632,7 +632,8 @@ public class MultiSelectionRenderer<T>
public void render(final RendererCellReference cell, final Boolean data,
CheckBox checkBox) {
checkBox.setValue(data, false);
- checkBox.setEnabled(grid.isEnabled() && !grid.isEditorActive());
+ checkBox.setEnabled(grid.isEnabled() && !grid.isEditorActive()
+ && grid.isUserSelectionAllowed());
}
@Override
@@ -770,6 +771,9 @@ public class MultiSelectionRenderer<T>
}
protected void setSelected(final int logicalRow, final boolean select) {
+ if (!grid.isUserSelectionAllowed()) {
+ return;
+ }
T row = grid.getDataSource().getRow(logicalRow);
if (select) {
grid.select(row);
diff --git a/client/src/main/java/com/vaadin/client/widget/grid/selection/SelectionModelMulti.java b/client/src/main/java/com/vaadin/client/widget/grid/selection/SelectionModelMulti.java
index 00c115c5c2..7874e03006 100644
--- a/client/src/main/java/com/vaadin/client/widget/grid/selection/SelectionModelMulti.java
+++ b/client/src/main/java/com/vaadin/client/widget/grid/selection/SelectionModelMulti.java
@@ -33,7 +33,7 @@ import com.vaadin.client.widgets.Grid;
* @since 7.4
*/
public class SelectionModelMulti<T> extends AbstractRowHandleSelectionModel<T>
- implements SelectionModel.Multi.Batched<T> {
+ implements SelectionModel.Multi.Batched<T>, HasUserSelectionAllowed<T> {
private final LinkedHashSet<RowHandle<T>> selectedRows;
private Renderer<Boolean> renderer;
@@ -45,6 +45,7 @@ public class SelectionModelMulti<T> extends AbstractRowHandleSelectionModel<T>
/* Event handling for selection with space key */
private SpaceSelectHandler<T> spaceSelectHandler;
+ private boolean userSelectionAllowed = true;
public SelectionModelMulti() {
grid = null;
@@ -270,4 +271,14 @@ public class SelectionModelMulti<T> extends AbstractRowHandleSelectionModel<T>
}
return rows;
}
+
+ @Override
+ public boolean isUserSelectionAllowed() {
+ return userSelectionAllowed;
+ }
+
+ @Override
+ public void setUserSelectionAllowed(boolean userSelectionAllowed) {
+ this.userSelectionAllowed = userSelectionAllowed;
+ }
}
diff --git a/client/src/main/java/com/vaadin/client/widget/grid/selection/SelectionModelSingle.java b/client/src/main/java/com/vaadin/client/widget/grid/selection/SelectionModelSingle.java
index f3df892623..217682bcd1 100644
--- a/client/src/main/java/com/vaadin/client/widget/grid/selection/SelectionModelSingle.java
+++ b/client/src/main/java/com/vaadin/client/widget/grid/selection/SelectionModelSingle.java
@@ -29,7 +29,7 @@ import com.vaadin.client.widgets.Grid;
* @since 7.4
*/
public class SelectionModelSingle<T> extends AbstractRowHandleSelectionModel<T>
- implements SelectionModel.Single<T> {
+ implements SelectionModel.Single<T>, HasUserSelectionAllowed<T> {
private Grid<T> grid;
private RowHandle<T> selectedRow;
@@ -41,6 +41,7 @@ public class SelectionModelSingle<T> extends AbstractRowHandleSelectionModel<T>
private ClickSelectHandler<T> clickSelectHandler;
private boolean deselectAllowed = true;
+ private boolean userSelectionAllowed = true;
@Override
public boolean isSelected(T row) {
@@ -172,4 +173,14 @@ public class SelectionModelSingle<T> extends AbstractRowHandleSelectionModel<T>
}
}
+ @Override
+ public boolean isUserSelectionAllowed() {
+ return userSelectionAllowed;
+ }
+
+ @Override
+ public void setUserSelectionAllowed(boolean userSelectionAllowed) {
+ this.userSelectionAllowed = userSelectionAllowed;
+ }
+
}
diff --git a/client/src/main/java/com/vaadin/client/widget/grid/selection/SpaceSelectHandler.java b/client/src/main/java/com/vaadin/client/widget/grid/selection/SpaceSelectHandler.java
index 456f08c5b3..476f342838 100644
--- a/client/src/main/java/com/vaadin/client/widget/grid/selection/SpaceSelectHandler.java
+++ b/client/src/main/java/com/vaadin/client/widget/grid/selection/SpaceSelectHandler.java
@@ -44,6 +44,10 @@ public class SpaceSelectHandler<T> {
@Override
public void onKeyDown(GridKeyDownEvent event) {
+ if (!grid.isUserSelectionAllowed()) {
+ return;
+ }
+
if (event.getNativeKeyCode() != KeyCodes.KEY_SPACE || spaceDown) {
return;
}
diff --git a/client/src/main/java/com/vaadin/client/widgets/Grid.java b/client/src/main/java/com/vaadin/client/widgets/Grid.java
index f796529aa7..d215b3c565 100755
--- a/client/src/main/java/com/vaadin/client/widgets/Grid.java
+++ b/client/src/main/java/com/vaadin/client/widgets/Grid.java
@@ -158,6 +158,7 @@ import com.vaadin.client.widget.grid.events.ScrollHandler;
import com.vaadin.client.widget.grid.events.SelectAllEvent;
import com.vaadin.client.widget.grid.events.SelectAllHandler;
import com.vaadin.client.widget.grid.selection.HasSelectionHandlers;
+import com.vaadin.client.widget.grid.selection.HasUserSelectionAllowed;
import com.vaadin.client.widget.grid.selection.MultiSelectionRenderer;
import com.vaadin.client.widget.grid.selection.SelectionEvent;
import com.vaadin.client.widget.grid.selection.SelectionHandler;
@@ -1922,6 +1923,9 @@ public class Grid<T> extends ResizeComposite implements HasSelectionHandlers<T>,
checkBox.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
+ if (!grid.isUserSelectionAllowed()) {
+ return;
+ }
T row = pinnedRowHandle.getRow();
if (grid.isSelected(row)) {
grid.deselect(row);
@@ -2885,6 +2889,8 @@ public class Grid<T> extends ResizeComposite implements HasSelectionHandlers<T>,
private boolean initDone = false;
private boolean selected = false;
private CheckBox selectAllCheckBox;
+ private boolean userSelectionAllowed = true;
+ private boolean enabled = true;
SelectionColumn(final Renderer<Boolean> selectColumnRenderer) {
super(selectColumnRenderer);
@@ -2915,6 +2921,7 @@ public class Grid<T> extends ResizeComposite implements HasSelectionHandlers<T>,
if (selectAllCheckBox == null) {
selectAllCheckBox = GWT.create(CheckBox.class);
+ selectAllCheckBox.setEnabled(enabled && userSelectionAllowed);
selectAllCheckBox.setStylePrimaryName(
getStylePrimaryName() + SELECT_ALL_CHECKBOX_CLASSNAME);
selectAllCheckBox.addValueChangeHandler(
@@ -2923,6 +2930,9 @@ public class Grid<T> extends ResizeComposite implements HasSelectionHandlers<T>,
@Override
public void onValueChange(
ValueChangeEvent<Boolean> event) {
+ if (!isUserSelectionAllowed()) {
+ return;
+ }
if (event.getValue()) {
fireEvent(new SelectAllEvent<T>(model));
selected = true;
@@ -2937,6 +2947,10 @@ public class Grid<T> extends ResizeComposite implements HasSelectionHandlers<T>,
addHeaderClickHandler(new HeaderClickHandler() {
@Override
public void onClick(GridClickEvent event) {
+ if (!userSelectionAllowed) {
+ return;
+ }
+
CellReference<?> targetCell = event.getTargetCell();
int defaultRowIndex = getHeader().getRows()
.indexOf(getDefaultHeaderRow());
@@ -2956,6 +2970,10 @@ public class Grid<T> extends ResizeComposite implements HasSelectionHandlers<T>,
if (event.getNativeKeyCode() != KeyCodes.KEY_SPACE) {
return;
}
+ if (!isUserSelectionAllowed()) {
+ return;
+ }
+
HeaderRow targetHeaderRow = getHeader()
.getRow(event.getFocusedCell().getRowIndex());
if (!targetHeaderRow.isDefault()) {
@@ -3051,8 +3069,9 @@ public class Grid<T> extends ResizeComposite implements HasSelectionHandlers<T>,
* to disable it.
*/
public void setEnabled(boolean enabled) {
+ this.enabled = enabled;
if (selectAllCheckBox != null) {
- selectAllCheckBox.setEnabled(enabled);
+ selectAllCheckBox.setEnabled(enabled && userSelectionAllowed);
}
}
@@ -3060,6 +3079,26 @@ public class Grid<T> extends ResizeComposite implements HasSelectionHandlers<T>,
public void onEnabled(boolean enabled) {
setEnabled(enabled);
}
+
+ /**
+ * Sets whether the user is allowed to change the selection.
+ *
+ * @param userSelectionAllowed
+ * <code>true</code> if the user is allowed to change the
+ * selection, <code>false</code> otherwise
+ */
+ public void setUserSelectionAllowed(boolean userSelectionAllowed) {
+ if (userSelectionAllowed == this.userSelectionAllowed) {
+ return;
+ }
+
+ this.userSelectionAllowed = userSelectionAllowed;
+ // Update checkbox state
+ setEnabled(enabled);
+ // Re-render select checkboxes
+ getEscalator().getBody().refreshRows(0,
+ getEscalator().getBody().getRowCount());
+ }
}
/**
@@ -9205,4 +9244,20 @@ public class Grid<T> extends ResizeComposite implements HasSelectionHandlers<T>,
}
return null;
}
+
+ /**
+ * Checks if selection by the user is allowed in the grid.
+ *
+ * @return <code>true</code> if selection by the user is allowed by the
+ * selection model (the default), <code>false</code> otherwise
+ */
+ public boolean isUserSelectionAllowed() {
+ if (!(getSelectionModel() instanceof HasUserSelectionAllowed)) {
+ // Selection model does not support toggling user selection allowed
+ // - old default is to always allow selection
+ return true;
+ }
+ return ((HasUserSelectionAllowed) getSelectionModel())
+ .isUserSelectionAllowed();
+ }
}