diff options
author | Pekka Hyvönen <pekka@vaadin.com> | 2016-11-29 16:43:25 +0200 |
---|---|---|
committer | Pekka Hyvönen <pekka@vaadin.com> | 2016-11-30 14:24:12 +0200 |
commit | 2de9aca9e5f9fe0d637f05c7198a93f93e0689e1 (patch) | |
tree | 1e203674a81608526546d6b100ed231f0f979eef /client | |
parent | c3ad14183d72ed48088b7a5bb528e896a9797e32 (diff) | |
download | vaadin-framework-2de9aca9e5f9fe0d637f05c7198a93f93e0689e1.tar.gz vaadin-framework-2de9aca9e5f9fe0d637f05c7198a93f93e0689e1.zip |
Add AbstractSelectionModel for Grid
Also adds shared state for grid's selection models.
This is mostly for making it possible later to modify the selection models,
without breaking backwards API compatibility and having to duplicate code.
Change-Id: If93720df282bf5ca3aff17a20b455d60b33f764c
Diffstat (limited to 'client')
3 files changed, 114 insertions, 58 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 new file mode 100644 index 0000000000..5f705cb237 --- /dev/null +++ b/client/src/main/java/com/vaadin/client/connectors/grid/AbstractSelectionModelConnector.java @@ -0,0 +1,79 @@ +/* + * Copyright 2000-2016 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.connectors.grid; + +import com.vaadin.client.ServerConnector; +import com.vaadin.client.extensions.AbstractExtensionConnector; +import com.vaadin.client.widget.grid.selection.SelectionModel; +import com.vaadin.client.widgets.Grid; +import com.vaadin.shared.ui.grid.AbstractSelectionModelState; + +import elemental.json.JsonObject; + +/** + * Abstract base class for grid's selection models. + * + * @author Vaadin Ltd + * + * @since 8.0 + */ +public abstract class AbstractSelectionModelConnector + extends AbstractExtensionConnector { + + @Override + protected void extend(ServerConnector target) { + initSelectionModel(); + } + + /** + * Initializes the selection model and sets it to the grid. + * <p> + * This method is only invoked once by {@link #extend(ServerConnector)} in + * {@link AbstractSelectionModelConnector} when the grid is available via + * {@link #getGrid()} and the selection model should be taken into use. + */ + protected abstract void initSelectionModel(); + + @Override + public GridConnector getParent() { + return (GridConnector) super.getParent(); + } + + /** + * Shorthand for fetching the grid this selection model is bound to. + * + * @return the grid + */ + protected Grid<JsonObject> getGrid() { + return getParent().getWidget(); + } + + @Override + public AbstractSelectionModelState getState() { + return (AbstractSelectionModelState) super.getState(); + } + + /** + * Returns whether the given item selected in grid or not. + * + * @param item + * the item to check + * @return {@code true} if selected {@code false} if not + */ + protected boolean isSelected(JsonObject item) { + return SelectionModel.isItemSelected(item); + } +} diff --git a/client/src/main/java/com/vaadin/client/connectors/grid/MultiSelectionModelConnector.java b/client/src/main/java/com/vaadin/client/connectors/grid/MultiSelectionModelConnector.java index b24cb54e63..8ad86f1541 100644 --- a/client/src/main/java/com/vaadin/client/connectors/grid/MultiSelectionModelConnector.java +++ b/client/src/main/java/com/vaadin/client/connectors/grid/MultiSelectionModelConnector.java @@ -18,11 +18,9 @@ package com.vaadin.client.connectors.grid; import java.util.Optional; import com.google.gwt.event.shared.HandlerRegistration; -import com.vaadin.client.ServerConnector; import com.vaadin.client.annotations.OnStateChange; import com.vaadin.client.data.DataSource; import com.vaadin.client.data.DataSource.RowHandle; -import com.vaadin.client.extensions.AbstractExtensionConnector; import com.vaadin.client.renderers.Renderer; import com.vaadin.client.widget.grid.events.SelectAllEvent; import com.vaadin.client.widget.grid.selection.MultiSelectionRenderer; @@ -53,7 +51,8 @@ import elemental.json.JsonObject; * */ @Connect(com.vaadin.ui.components.grid.MultiSelectionModelImpl.class) -public class MultiSelectionModelConnector extends AbstractExtensionConnector { +public class MultiSelectionModelConnector + extends AbstractSelectionModelConnector { private HandlerRegistration selectAllHandler; private HandlerRegistration dataAvailable; @@ -110,7 +109,7 @@ public class MultiSelectionModelConnector extends AbstractExtensionConnector { } @Override - protected void extend(ServerConnector target) { + protected void initSelectionModel() { getGrid().setSelectionModel(new MultiSelectionModel()); // capture current rows so that can show selection update immediately dataAvailable = getGrid().addDataAvailableHandler( @@ -128,24 +127,10 @@ public class MultiSelectionModelConnector extends AbstractExtensionConnector { } @Override - public GridConnector getParent() { - return (GridConnector) super.getParent(); - } - - @Override public MultiSelectionModelState getState() { return (MultiSelectionModelState) super.getState(); } - /** - * Shorthand for fetching the grid this selection model is bound to. - * - * @return the grid - */ - protected Grid<JsonObject> getGrid() { - return getParent().getWidget(); - } - @OnStateChange({ "selectAllCheckBoxVisible", "allSelected" }) void onSelectAllCheckboxStateUpdates() { // in case someone wants to override this, moved the actual updating to @@ -232,15 +217,9 @@ public class MultiSelectionModelConnector extends AbstractExtensionConnector { } } - /** - * Returns whether the given item selected in grid or not. - * - * @param item - * the item to check - * @return {@code true} if selected {@code false} if not - */ + @Override protected boolean isSelected(JsonObject item) { - return getState().allSelected || SelectionModel.isItemSelected(item); + return getState().allSelected || super.isSelected(item); } /** 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 aba6d906b3..d18f372a2c 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,8 +15,6 @@ */ package com.vaadin.client.connectors.grid; -import com.vaadin.client.ServerConnector; -import com.vaadin.client.extensions.AbstractExtensionConnector; import com.vaadin.client.widget.grid.selection.ClickSelectHandler; import com.vaadin.client.widget.grid.selection.SelectionModel; import com.vaadin.shared.data.DataCommunicatorConstants; @@ -33,38 +31,43 @@ import elemental.json.JsonObject; * @since 8.0 */ @Connect(com.vaadin.ui.components.grid.SingleSelectionModelImpl.class) -public class SingleSelectionModelConnector extends AbstractExtensionConnector { +public class SingleSelectionModelConnector + extends AbstractSelectionModelConnector { - private ClickSelectHandler clickSelectHandler; + private ClickSelectHandler<JsonObject> clickSelectHandler; - @Override - protected void extend(ServerConnector target) { - getParent().getWidget() - .setSelectionModel(new SelectionModel<JsonObject>() { + /** + * Single selection model for grid. + */ + protected class SingleSelectionModel implements SelectionModel<JsonObject> { - @Override - public void select(JsonObject item) { - getRpcProxy(SelectionServerRpc.class).select( - item.getString(DataCommunicatorConstants.KEY)); - } + @Override + public void select(JsonObject item) { + getRpcProxy(SelectionServerRpc.class) + .select(item.getString(DataCommunicatorConstants.KEY)); + } - @Override - public void deselect(JsonObject item) { - getRpcProxy(SelectionServerRpc.class).deselect( - item.getString(DataCommunicatorConstants.KEY)); - } + @Override + public void deselect(JsonObject item) { + getRpcProxy(SelectionServerRpc.class) + .deselect(item.getString(DataCommunicatorConstants.KEY)); + } - @Override - public boolean isSelected(JsonObject item) { - return SelectionModel.isItemSelected(item); - } + @Override + public boolean isSelected(JsonObject item) { + return SingleSelectionModelConnector.this.isSelected(item); + } - @Override - public void deselectAll() { - getRpcProxy(SelectionServerRpc.class).select(null); - } + @Override + public void deselectAll() { + getRpcProxy(SelectionServerRpc.class).select(null); + } + } + + @Override + protected void initSelectionModel() { + getGrid().setSelectionModel(new SingleSelectionModel()); - }); clickSelectHandler = new ClickSelectHandler<>(getParent().getWidget()); } @@ -76,9 +79,4 @@ public class SingleSelectionModelConnector extends AbstractExtensionConnector { } } - @Override - public GridConnector getParent() { - return (GridConnector) super.getParent(); - } - } |