Skip to content

Commit

Permalink
Add MultiSelect support for Grid
Browse files Browse the repository at this point in the history
Still missing following things coming in next patches:
- select all checkbox
- firing an event when data provider is changed in grid
- read only selection models for grid

Part 1 for vaadin/framework8-issues#232

Change-Id: Ib2c7c81a838f43cb7c521a56d50139c91961f54a
  • Loading branch information
pleku authored and Vaadin Code Review committed Nov 29, 2016
1 parent 1344356 commit f2d8f81
Show file tree
Hide file tree
Showing 40 changed files with 1,954 additions and 642 deletions.
@@ -0,0 +1,101 @@
/*
* 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.renderers.Renderer;
import com.vaadin.client.widget.grid.selection.MultiSelectionRenderer;
import com.vaadin.client.widget.grid.selection.SelectionModel;
import com.vaadin.client.widget.grid.selection.SelectionModelWithSelectionColumn;
import com.vaadin.client.widgets.Grid;
import com.vaadin.shared.data.DataCommunicatorConstants;
import com.vaadin.shared.data.selection.GridMultiSelectServerRpc;
import com.vaadin.shared.ui.Connect;

import elemental.json.JsonObject;

/**
* Connector for server side multiselection model implementation.
*
* @author Vaadin Ltd
*
* @since 8.0
*
*/
@Connect(com.vaadin.ui.components.grid.MultiSelectionModelImpl.class)
public class MultiSelectionModelConnector extends AbstractExtensionConnector {

/**
* Client side multiselection model implementation.
*/
protected class MultiSelectionModel implements SelectionModel<JsonObject>,
SelectionModelWithSelectionColumn {

@Override
public Renderer<Boolean> getRenderer() {
// this method is only called once when the selection model is set
// to grid
return new MultiSelectionRenderer<>(getGrid());
}

@Override
public void select(JsonObject item) {
getRpcProxy(GridMultiSelectServerRpc.class)
.select(item.getString(DataCommunicatorConstants.KEY));
}

@Override
public void deselect(JsonObject item) {
// handled by diffstate
getRpcProxy(GridMultiSelectServerRpc.class)
.deselect(item.getString(DataCommunicatorConstants.KEY));
}

@Override
public void deselectAll() {
// TODO Will be added in a separate patch
throw new UnsupportedOperationException(
"Deselect all not supported.");
}

@Override
public boolean isSelected(JsonObject item) {
return SelectionModel.isItemSelected(item);
}

}

@Override
protected void extend(ServerConnector target) {
getGrid().setSelectionModel(new MultiSelectionModel());
}

@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();
}

}
Expand Up @@ -15,11 +15,9 @@
*/
package com.vaadin.client.connectors.grid;

import java.util.Set;

import com.vaadin.client.ServerConnector;
import com.vaadin.client.data.SelectionModel;
import com.vaadin.client.extensions.AbstractExtensionConnector;
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;
Expand All @@ -33,7 +31,7 @@
*
* @since 8.0
*/
@Connect(com.vaadin.ui.components.grid.SingleSelectionModel.class)
@Connect(com.vaadin.ui.components.grid.SingleSelectionModelImpl.class)
public class SingleSelectionModelConnector extends AbstractExtensionConnector {

@Override
Expand All @@ -54,14 +52,13 @@ public void deselect(JsonObject item) {
}

@Override
public Set<JsonObject> getSelectedItems() {
throw new UnsupportedOperationException(
"Selected item not known on the client side");
public boolean isSelected(JsonObject item) {
return SelectionModel.isItemSelected(item);
}

@Override
public boolean isSelected(JsonObject item) {
return SelectionModel.isItemSelected(item);
public void deselectAll() {
getRpcProxy(SelectionServerRpc.class).select(null);
}

});
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.client.data.SelectionModel;
import com.vaadin.client.widget.grid.selection.SelectionModel;

/**
* A select all event, fired by the Grid when it needs all rows in data source
Expand Down
Expand Up @@ -136,15 +136,6 @@ public Collection<T> getRemoved() {
return Collections.unmodifiableCollection(removed);
}

/**
* Gets currently selected rows.
*
* @return a non-null collection containing all currently selected rows.
*/
public Collection<T> getSelected() {
return grid.getSelectedRows();
}

/**
* Gets a type identifier for this event.
*
Expand Down
Expand Up @@ -13,9 +13,7 @@
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.client.data;

import java.util.Set;
package com.vaadin.client.widget.grid.selection;

import com.vaadin.shared.data.DataCommunicatorConstants;

Expand Down Expand Up @@ -51,14 +49,6 @@ public interface SelectionModel<T> {
*/
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.
*
Expand All @@ -71,9 +61,7 @@ public interface SelectionModel<T> {
/**
* Deselects all currently selected items.
*/
default void deselectAll() {
getSelectedItems().forEach(this::deselect);
}
void deselectAll();

/**
* Gets the selected state from a given grid row json object. This is a
Expand Down

0 comments on commit f2d8f81

Please sign in to comment.