Skip to content

Commit

Permalink
Make SelectionModel API only server side.
Browse files Browse the repository at this point in the history
Client side doesn't use selection model anymore.
Fixes vaadin/framework8-issues#421

Change-Id: If3ecb1c2f3a0024df9bfdfd182eaf8cf8625ac75
  • Loading branch information
Denis Anisimov committed Nov 2, 2016
1 parent dd20841 commit 13747f7
Show file tree
Hide file tree
Showing 29 changed files with 126 additions and 122 deletions.
Expand Up @@ -18,7 +18,6 @@
import com.google.gwt.event.dom.client.HasAllFocusHandlers;
import com.google.gwt.user.client.ui.Widget;
import com.vaadin.client.ui.ConnectorFocusAndBlurHandler;
import com.vaadin.shared.data.selection.SelectionModel;

/**
* Abstract class for listing widget connectors that contains focusable children
Expand All @@ -28,11 +27,9 @@
*
* @param <WIDGET>
* widget type which has to allow to register focus/blur handlers
* @param <SELECTIONMODEL>
* the client-side selection model type
*/
public abstract class AbstractFocusableListingConnector<WIDGET extends Widget & HasAllFocusHandlers, SELECTIONMODEL extends SelectionModel<?>>
extends AbstractListingConnector<SELECTIONMODEL> {
public abstract class AbstractFocusableListingConnector<WIDGET extends Widget & HasAllFocusHandlers>
extends AbstractListingConnector {

private ConnectorFocusAndBlurHandler handler;

Expand Down
Expand Up @@ -19,7 +19,6 @@
import com.vaadin.client.data.DataSource;
import com.vaadin.client.ui.AbstractFieldConnector;
import com.vaadin.shared.data.DataCommunicatorConstants;
import com.vaadin.shared.data.selection.SelectionModel;
import com.vaadin.ui.AbstractListing;

import elemental.json.JsonObject;
Expand All @@ -30,18 +29,13 @@
*
* @author Vaadin Ltd.
*
* @param <SELECTIONMODEL>
* the client-side selection model type
*
* @since 8.0
*/
public abstract class AbstractListingConnector<SELECTIONMODEL extends SelectionModel<?>>
extends AbstractFieldConnector implements HasDataSource {
public abstract class AbstractListingConnector extends AbstractFieldConnector
implements HasDataSource {

private DataSource<JsonObject> dataSource = null;

private SELECTIONMODEL selectionModel = null;

@Override
public void setDataSource(DataSource<JsonObject> dataSource) {
this.dataSource = dataSource;
Expand All @@ -52,25 +46,6 @@ public DataSource<JsonObject> getDataSource() {
return dataSource;
}

/**
* Sets the selection model to use. Passing {@code null} disables selection.
*
* @param selectionModel
* the selection model or null to disable
*/
public void setSelectionModel(SELECTIONMODEL selectionModel) {
this.selectionModel = selectionModel;
}

/**
* Returns the selection model instance used.
*
* @return the selection model
*/
public SELECTIONMODEL getSelectionModel() {
return selectionModel;
}

/**
* Returns the key of the given data row.
*
Expand Down
Expand Up @@ -30,7 +30,6 @@
import com.vaadin.shared.Range;
import com.vaadin.shared.Registration;
import com.vaadin.shared.data.selection.MultiSelectServerRpc;
import com.vaadin.shared.data.selection.SelectionModel;
import com.vaadin.shared.ui.ListingJsonConstants;

import elemental.json.JsonObject;
Expand All @@ -46,8 +45,7 @@
* @since 8.0
*/
public abstract class AbstractMultiSelectConnector
extends AbstractListingConnector<SelectionModel.Multi<?>>
implements HasRequiredIndicator {
extends AbstractListingConnector implements HasRequiredIndicator {

/**
* Abstraction layer to help populate different multiselect widgets based on
Expand Down
Expand Up @@ -18,7 +18,6 @@
import com.google.gwt.event.dom.client.HasAllFocusHandlers;
import com.google.gwt.user.client.ui.Widget;
import com.vaadin.client.ui.HasRequiredIndicator;
import com.vaadin.shared.data.selection.SelectionModel;
import com.vaadin.shared.ui.AbstractSingleSelectState;

/**
Expand All @@ -28,8 +27,7 @@
* @since 8.0.0
*/
public abstract class AbstractSingleSelectConnector<WIDGET extends Widget & HasAllFocusHandlers>
extends
AbstractFocusableListingConnector<WIDGET, SelectionModel.Single<?>>
extends AbstractFocusableListingConnector<WIDGET>
implements HasRequiredIndicator {

@Override
Expand Down
Expand Up @@ -21,7 +21,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import com.google.gwt.dom.client.Element;
Expand All @@ -40,6 +40,7 @@
import com.vaadin.client.connectors.AbstractListingConnector;
import com.vaadin.client.connectors.grid.ColumnConnector.CustomColumn;
import com.vaadin.client.data.DataSource;
import com.vaadin.client.data.SelectionModel;
import com.vaadin.client.ui.SimpleManagedLayout;
import com.vaadin.client.widget.grid.CellReference;
import com.vaadin.client.widget.grid.EventCellReference;
Expand All @@ -58,7 +59,6 @@
import com.vaadin.client.widgets.Grid.HeaderRow;
import com.vaadin.shared.MouseEventDetails;
import com.vaadin.shared.data.DataCommunicatorConstants;
import com.vaadin.shared.data.selection.SelectionModel;
import com.vaadin.shared.data.selection.SelectionServerRpc;
import com.vaadin.shared.data.sort.SortDirection;
import com.vaadin.shared.ui.Connect;
Expand All @@ -79,8 +79,7 @@
* @since 8.0
*/
@Connect(com.vaadin.ui.Grid.class)
public class GridConnector
extends AbstractListingConnector<SelectionModel<JsonObject>>
public class GridConnector extends AbstractListingConnector
implements HasComponentsConnector, SimpleManagedLayout, DeferredWorker {

private class ItemClickHandler
Expand Down Expand Up @@ -205,7 +204,7 @@ protected void init() {
/* Item click events */
getWidget().addBodyClickHandler(itemClickHandler);
getWidget().addBodyDoubleClickHandler(itemClickHandler);
getWidget().setSelectionModel(new SelectionModel.Single<JsonObject>() {
getWidget().setSelectionModel(new SelectionModel<JsonObject>() {

@Override
public void select(JsonObject item) {
Expand All @@ -220,7 +219,7 @@ public void deselect(JsonObject item) {
}

@Override
public Optional<JsonObject> getSelectedItem() {
public Set<JsonObject> getSelectedItems() {
throw new UnsupportedOperationException(
"Selected item not known on the client side");
}
Expand All @@ -230,6 +229,7 @@ public boolean isSelected(JsonObject item) {
return item.hasKey(DataCommunicatorConstants.SELECTED)
&& item.getBoolean(DataCommunicatorConstants.SELECTED);
}

});

layout();
Expand Down Expand Up @@ -321,12 +321,6 @@ public void setDataSource(DataSource<JsonObject> dataSource) {
getWidget().setDataSource(dataSource);
}

@Override
public void setSelectionModel(SelectionModel<JsonObject> selectionModel) {
throw new UnsupportedOperationException(
"Cannot set a selection model for GridConnector");
}

/**
* Adds a column to the Grid widget. For each column a communication id
* stored for client to server communication.
Expand Down
Expand Up @@ -19,7 +19,6 @@
import com.vaadin.client.connectors.AbstractListingConnector;
import com.vaadin.client.extensions.AbstractExtensionConnector;
import com.vaadin.shared.data.DataCommunicatorConstants;
import com.vaadin.shared.data.selection.SelectionModel;

import elemental.json.JsonObject;

Expand All @@ -28,15 +27,11 @@
*
* @author Vaadin Ltd.
*
* @param <SELECTIONMODEL>
* the supported client-side selection model
* @since 8.0
*/
public abstract class AbstractSelectionConnector<SELECTIONMODEL extends SelectionModel<?>>
public abstract class AbstractSelectionConnector
extends AbstractExtensionConnector {

private SELECTIONMODEL model = null;

@Override
@SuppressWarnings("unchecked")
protected void extend(ServerConnector target) {
Expand All @@ -45,31 +40,12 @@ protected void extend(ServerConnector target) {
"Cannot extend a connector that is not an "
+ AbstractListingConnector.class.getSimpleName());
}
model = createSelectionModel();
((AbstractListingConnector<SELECTIONMODEL>) target)
.setSelectionModel(model);
}

/**
* Creates a selection model object to be used by the Connector.
*
* @return created selection model
*/
protected abstract SELECTIONMODEL createSelectionModel();

@Override
@SuppressWarnings("unchecked")
public AbstractListingConnector<SELECTIONMODEL> getParent() {
return (AbstractListingConnector<SELECTIONMODEL>) super.getParent();
}

/**
* Returns the client-side selection model associated with this connector.
*
* @return the selection model in use
*/
protected SELECTIONMODEL getSelectionModel() {
return model;
public AbstractListingConnector getParent() {
return (AbstractListingConnector) super.getParent();
}

/**
Expand Down
73 changes: 73 additions & 0 deletions client/src/main/java/com/vaadin/client/data/SelectionModel.java
@@ -0,0 +1,73 @@
/*
* 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.data;

import java.util.Set;

/**
* Models the selection logic of a {@code Grid} component. Determines how items
* can be selected and deselected.
*
* @author Vaadin Ltd.
*
* @param <T>
* the type of the items to select
* @since 8.0
*/
public interface SelectionModel<T> {

/**
* Selects the given item. If another item was already selected, that item
* is deselected.
*
* @param item
* the item to select, not null
*/
void select(T item);

/**
* Deselects the given item. If the item is not currently selected, does
* nothing.
*
* @param item
* the item to deselect, not null
*/
void deselect(T item);

/**
* Returns a set of the currently selected items. It is safe to invoke other
* {@code SelectionModel} methods while iterating over the set.
*
* @return the items in the current selection, not null
*/
Set<T> getSelectedItems();

/**
* Returns whether the given item is currently selected.
*
* @param item
* the item to check, not null
* @return {@code true} if the item is selected, {@code false} otherwise
*/
boolean isSelected(T item);

/**
* Deselects all currently selected items.
*/
default void deselectAll() {
getSelectedItems().forEach(this::deselect);
}
}
Expand Up @@ -30,7 +30,6 @@
import com.vaadin.shared.Registration;
import com.vaadin.shared.communication.FieldRpc.FocusAndBlurServerRpc;
import com.vaadin.shared.data.DataCommunicatorConstants;
import com.vaadin.shared.data.selection.SelectionModel;
import com.vaadin.shared.data.selection.SelectionServerRpc;
import com.vaadin.shared.ui.Connect;
import com.vaadin.shared.ui.combobox.ComboBoxConstants;
Expand All @@ -41,8 +40,7 @@
import elemental.json.JsonObject;

@Connect(ComboBox.class)
public class ComboBoxConnector
extends AbstractListingConnector<SelectionModel.Single<?>>
public class ComboBoxConnector extends AbstractListingConnector
implements HasRequiredIndicator, HasDataSource, SimpleManagedLayout,
HasErrorIndicator {

Expand Down
Expand Up @@ -27,7 +27,6 @@
import com.vaadin.client.ui.HasRequiredIndicator;
import com.vaadin.client.ui.VCheckBoxGroup;
import com.vaadin.shared.data.selection.MultiSelectServerRpc;
import com.vaadin.shared.data.selection.SelectionModel;
import com.vaadin.shared.ui.Connect;
import com.vaadin.shared.ui.optiongroup.CheckBoxGroupState;
import com.vaadin.ui.CheckBoxGroup;
Expand All @@ -38,8 +37,8 @@
// We don't care about the framework-provided selection model at this point
// TODO refactor to extend AbstractMultiSelectConnector, maybe when
// SelectionModel is removed from client side framwork8-issues#421
public class CheckBoxGroupConnector extends
AbstractFocusableListingConnector<VCheckBoxGroup, SelectionModel<?>>
public class CheckBoxGroupConnector
extends AbstractFocusableListingConnector<VCheckBoxGroup>
implements HasRequiredIndicator {

@Override
Expand Down
Expand Up @@ -16,7 +16,7 @@
package com.vaadin.client.widget.grid.events;

import com.google.gwt.event.shared.GwtEvent;
import com.vaadin.shared.data.selection.SelectionModel;
import com.vaadin.client.data.SelectionModel;

/**
* A select all event, fired by the Grid when it needs all rows in data source
Expand All @@ -32,9 +32,9 @@ public class SelectAllEvent<T> extends GwtEvent<SelectAllHandler<T>> {
*/
private final static Type<SelectAllHandler<?>> TYPE = new Type<>();;

private SelectionModel.Multi<T> selectionModel;
private SelectionModel selectionModel;

public SelectAllEvent(SelectionModel.Multi<T> selectionModel) {
public SelectAllEvent(SelectionModel selectionModel) {
this.selectionModel = selectionModel;
}

Expand All @@ -53,7 +53,7 @@ protected void dispatch(SelectAllHandler<T> handler) {
handler.onSelectAll(this);
}

public SelectionModel.Multi<T> getSelectionModel() {
public SelectionModel getSelectionModel() {
return selectionModel;
}
}

0 comments on commit 13747f7

Please sign in to comment.