diff options
9 files changed, 247 insertions, 106 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(); - } - } diff --git a/server/src/main/java/com/vaadin/ui/components/grid/AbstractSelectionModel.java b/server/src/main/java/com/vaadin/ui/components/grid/AbstractSelectionModel.java new file mode 100644 index 0000000000..dc8ee18f3a --- /dev/null +++ b/server/src/main/java/com/vaadin/ui/components/grid/AbstractSelectionModel.java @@ -0,0 +1,62 @@ +/* + * 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.ui.components.grid; + +import com.vaadin.shared.data.DataCommunicatorConstants; +import com.vaadin.shared.ui.grid.AbstractSelectionModelState; +import com.vaadin.ui.Grid.AbstractGridExtension; +import com.vaadin.ui.Grid.GridSelectionModel; + +import elemental.json.JsonObject; + +/** + * Abstract selection model for grid. + * + * @author Vaadin Ltd + * + * @since 8.0 + * + * @param <T> + * the type of the items in grid. + */ +public abstract class AbstractSelectionModel<T> extends AbstractGridExtension<T> + implements GridSelectionModel<T> { + + @Override + public void generateData(T item, JsonObject jsonObject) { + if (isSelected(item)) { + jsonObject.put(DataCommunicatorConstants.SELECTED, true); + } + } + + @Override + protected AbstractSelectionModelState getState() { + return (AbstractSelectionModelState) super.getState(); + } + + @Override + protected AbstractSelectionModelState getState(boolean markAsDirty) { + return (AbstractSelectionModelState) super.getState(markAsDirty); + } + + @Override + public void remove() { + deselectAll(); + + super.remove(); + } + +} diff --git a/server/src/main/java/com/vaadin/ui/components/grid/MultiSelectionModelImpl.java b/server/src/main/java/com/vaadin/ui/components/grid/MultiSelectionModelImpl.java index 70a056f34c..465115d831 100644 --- a/server/src/main/java/com/vaadin/ui/components/grid/MultiSelectionModelImpl.java +++ b/server/src/main/java/com/vaadin/ui/components/grid/MultiSelectionModelImpl.java @@ -30,17 +30,13 @@ import com.vaadin.event.selection.MultiSelectionListener; import com.vaadin.server.data.DataProvider; import com.vaadin.server.data.Query; import com.vaadin.shared.Registration; -import com.vaadin.shared.data.DataCommunicatorConstants; import com.vaadin.shared.data.selection.GridMultiSelectServerRpc; import com.vaadin.shared.ui.grid.MultiSelectionModelState; import com.vaadin.ui.Grid; -import com.vaadin.ui.Grid.AbstractGridExtension; import com.vaadin.ui.Grid.MultiSelectionModel; import com.vaadin.ui.MultiSelect; import com.vaadin.util.ReflectTools; -import elemental.json.JsonObject; - /** * Multiselection model for grid. * <p> @@ -56,7 +52,7 @@ import elemental.json.JsonObject; * @param <T> * the type of the selected item in grid. */ -public class MultiSelectionModelImpl<T> extends AbstractGridExtension<T> +public class MultiSelectionModelImpl<T> extends AbstractSelectionModel<T> implements MultiSelectionModel<T> { /** @@ -257,13 +253,6 @@ public class MultiSelectionModelImpl<T> extends AbstractGridExtension<T> } } - @Override - public void remove() { - updateSelection(Collections.emptySet(), getSelectedItems(), false); - - super.remove(); - } - /** * Adds a selection listener that will be called when the selection is * changed either by the user or programmatically. @@ -280,15 +269,6 @@ public class MultiSelectionModelImpl<T> extends AbstractGridExtension<T> } @Override - public void generateData(T item, JsonObject jsonObject) { - // in case of all items selected, don't write individual items as - // seleted - if (isSelected(item)) { - jsonObject.put(DataCommunicatorConstants.SELECTED, true); - } - } - - @Override public Set<T> getSelectedItems() { return Collections.unmodifiableSet(new LinkedHashSet<>(selection)); } diff --git a/server/src/main/java/com/vaadin/ui/components/grid/SingleSelectionModelImpl.java b/server/src/main/java/com/vaadin/ui/components/grid/SingleSelectionModelImpl.java index e68487f358..283b8edc50 100644 --- a/server/src/main/java/com/vaadin/ui/components/grid/SingleSelectionModelImpl.java +++ b/server/src/main/java/com/vaadin/ui/components/grid/SingleSelectionModelImpl.java @@ -26,17 +26,14 @@ import java.util.Set; import com.vaadin.event.selection.SingleSelectionEvent; import com.vaadin.event.selection.SingleSelectionListener; import com.vaadin.shared.Registration; -import com.vaadin.shared.data.DataCommunicatorConstants; import com.vaadin.shared.data.selection.SelectionServerRpc; +import com.vaadin.shared.ui.grid.SingleSelectionModelState; import com.vaadin.ui.Component; import com.vaadin.ui.Grid; -import com.vaadin.ui.Grid.AbstractGridExtension; import com.vaadin.ui.Grid.SingleSelectionModel; import com.vaadin.ui.SingleSelect; import com.vaadin.util.ReflectTools; -import elemental.json.JsonObject; - /** * Single selection model for grid. * @@ -46,7 +43,7 @@ import elemental.json.JsonObject; * @param <T> * the type of the selected item in grid. */ -public class SingleSelectionModelImpl<T> extends AbstractGridExtension<T> +public class SingleSelectionModelImpl<T> extends AbstractSelectionModel<T> implements SingleSelectionModel<T> { private static final Method SELECTION_CHANGE_METHOD = ReflectTools @@ -81,6 +78,16 @@ public class SingleSelectionModelImpl<T> extends AbstractGridExtension<T> }); } + @Override + protected SingleSelectionModelState getState() { + return (SingleSelectionModelState) super.getState(); + } + + @Override + protected SingleSelectionModelState getState(boolean markAsDirty) { + return (SingleSelectionModelState) super.getState(markAsDirty); + } + /** * Adds a selection listener to this select. The listener is called when the * value of this select is changed either by the user or programmatically. @@ -224,23 +231,6 @@ public class SingleSelectionModelImpl<T> extends AbstractGridExtension<T> } } - @Override - public void generateData(T item, JsonObject jsonObject) { - if (isSelected(item)) { - jsonObject.put(DataCommunicatorConstants.SELECTED, true); - } - } - - @Override - public void remove() { - // when selection model changes, firing an event for selection change - // event fired before removing so that parent is still intact (in case - // needed) - setSelectedFromServer(null); - - super.remove(); - } - /** * Gets a wrapper for using this grid as a single select in a binder. * diff --git a/shared/src/main/java/com/vaadin/shared/ui/grid/AbstractSelectionModelState.java b/shared/src/main/java/com/vaadin/shared/ui/grid/AbstractSelectionModelState.java new file mode 100644 index 0000000000..9cd29341b1 --- /dev/null +++ b/shared/src/main/java/com/vaadin/shared/ui/grid/AbstractSelectionModelState.java @@ -0,0 +1,28 @@ +/* + * 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.shared.ui.grid; + +import com.vaadin.shared.communication.SharedState; + +/** + * Shared state for grid's abstract selection model. + * + * @author Vaadin Ltd + * + * @since 8.0 + */ +public class AbstractSelectionModelState extends SharedState { +} diff --git a/shared/src/main/java/com/vaadin/shared/ui/grid/MultiSelectionModelState.java b/shared/src/main/java/com/vaadin/shared/ui/grid/MultiSelectionModelState.java index 5d2a1c6094..0f7ccdc102 100644 --- a/shared/src/main/java/com/vaadin/shared/ui/grid/MultiSelectionModelState.java +++ b/shared/src/main/java/com/vaadin/shared/ui/grid/MultiSelectionModelState.java @@ -15,15 +15,14 @@ */ package com.vaadin.shared.ui.grid; -import com.vaadin.shared.communication.SharedState; - /** - * SharedState object for MultiSelectionModel. + * SharedState object for MultiSelectionModelImpl. * - * @since 8.0 * @author Vaadin Ltd + * + * @since 8.0 */ -public class MultiSelectionModelState extends SharedState { +public class MultiSelectionModelState extends AbstractSelectionModelState { /** Is the select all checkbox visible. */ public boolean selectAllCheckBoxVisible; diff --git a/shared/src/main/java/com/vaadin/shared/ui/grid/SingleSelectionModelState.java b/shared/src/main/java/com/vaadin/shared/ui/grid/SingleSelectionModelState.java new file mode 100644 index 0000000000..2ad1a30a3d --- /dev/null +++ b/shared/src/main/java/com/vaadin/shared/ui/grid/SingleSelectionModelState.java @@ -0,0 +1,26 @@ +/* + * 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.shared.ui.grid; + +/** + * Shared state for grid's single selection model. + * + * @author Vaadin Ltd + * + * @since 8.0 + */ +public class SingleSelectionModelState extends AbstractSelectionModelState { +} |