Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refs #35 : TableSelectionModel overrides onDeselectAll - does not inv… #36

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
4 changes: 2 additions & 2 deletions GridExtensionPack-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<groupId>org.vaadin.teemusa</groupId>
<artifactId>gridextensionpack-demo</artifactId>
<packaging>war</packaging>
<version>2.0-SNAPSHOT</version>
<version>2.0.3</version>
<name>GridExtensionPack Add-on Demo</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<vaadin.version>8.0.0</vaadin.version>
<vaadin.version>8.8.6</vaadin.version>
<vaadin.plugin.version>${vaadin.version}</vaadin.plugin.version>
</properties>

Expand Down
4 changes: 2 additions & 2 deletions GridExtensionPack/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<groupId>org.vaadin.teemusa</groupId>
<artifactId>gridextensionpack</artifactId>
<packaging>jar</packaging>
<version>2.0-SNAPSHOT</version>
<version>2.0.3</version>
<name>GridExtensionPack Add-on</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<vaadin.version>8.0.0</vaadin.version>
<vaadin.version>8.8.6</vaadin.version>
<vaadin.plugin.version>${vaadin.version}</vaadin.plugin.version>

<!-- ZIP Manifest fields -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package org.vaadin.teemusa.gridextensions.client.tableselection;

import org.vaadin.teemusa.gridextensions.tableselection.TableSelectionModel;

import com.google.gwt.event.shared.HandlerRegistration;
import com.vaadin.client.ServerConnector;
import com.vaadin.client.annotations.OnStateChange;
import com.vaadin.client.connectors.grid.MultiSelectionModelConnector;
import com.vaadin.client.renderers.Renderer;
import com.vaadin.client.widget.grid.events.BodyClickHandler;
import com.vaadin.client.widgets.Grid;
import com.vaadin.shared.Range;
import com.vaadin.shared.ui.Connect;

import elemental.json.JsonObject;
import org.vaadin.teemusa.gridextensions.tableselection.TableSelectionModel;

@Connect(TableSelectionModel.class)
public class TableSelectionModelConnector extends MultiSelectionModelConnector {
Expand All @@ -29,10 +26,8 @@ public Renderer<Boolean> getRenderer() {
private HandlerRegistration clickHandler;

@Override
protected void initSelectionModel() {
super.initSelectionModel();

getGrid().setSelectionModel(new CustomSelectionModel());
protected MultiSelectionModel createSelectionModel() {
return new CustomSelectionModel();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
package org.vaadin.teemusa.gridextensions.tableselection;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.BinaryOperator;

import org.vaadin.teemusa.gridextensions.SelectGrid;
import org.vaadin.teemusa.gridextensions.client.tableselection.ShiftSelectRpc;
import org.vaadin.teemusa.gridextensions.client.tableselection.TableSelectionState;
import org.vaadin.teemusa.gridextensions.client.tableselection.TableSelectionState.TableSelectionMode;

import com.vaadin.data.provider.Query;
import com.vaadin.data.provider.QuerySortOrder;
import com.vaadin.server.AbstractClientConnector;
import com.vaadin.server.SerializableComparator;
import com.vaadin.ui.Grid;
import com.vaadin.ui.components.grid.GridSelectionModel;
import com.vaadin.ui.components.grid.MultiSelectionModelImpl;
import org.vaadin.teemusa.gridextensions.SelectGrid;
import org.vaadin.teemusa.gridextensions.client.tableselection.ShiftSelectRpc;
import org.vaadin.teemusa.gridextensions.client.tableselection.TableSelectionState;
import org.vaadin.teemusa.gridextensions.client.tableselection.TableSelectionState.TableSelectionMode;

import java.lang.reflect.Field;
import java.util.*;
import java.util.function.BinaryOperator;

/**
* TableSelectModel provides {@link Grid} selection UX options to make it behave
Expand All @@ -26,14 +23,14 @@
* <p>
* This is a SelectionModel for SelectGrid, use
* {@link SelectGrid#setSelectionModel(GridSelectionModel)} to take it into use.
*
*
* @author Teemu Suo-Anttila
*/
public class TableSelectionModel<T> extends MultiSelectionModelImpl<T> {

/**
* Set the TableSelectionMode to use with this extension.
*
*
* @param mode
* table-like selection mode
*/
Expand Down Expand Up @@ -70,15 +67,44 @@ public void selectRange(int start, int length) {
*/
return comparator1.thenComparing(comparator2)::compare;
};
Comparator<T> inMemorySorting = getParent().getSortOrder().stream()
final Grid<T> grid = getParent();
Comparator<T> inMemorySorting = grid.getSortOrder().stream()
.map(order -> order.getSorted().getComparator(order.getDirection()))
.reduce((x, y) -> 0, operator);

List<QuerySortOrder> sortProperties = new ArrayList<>();
getParent().getSortOrder().stream().map(order -> order.getSorted().getSortOrder(order.getDirection()))
grid.getSortOrder().stream().map(order -> order.getSorted().getSortOrder(order.getDirection()))
.forEach(s -> s.forEach(sortProperties::add));
getParent().getDataProvider().fetch(new Query<>(start, length, sortProperties, inMemorySorting, null));
grid.getDataProvider().fetch(new Query<>(start, length, sortProperties, inMemorySorting, null))
.forEach(grid::select);

}
});
}

/**
* In this SelectionModel we want to avoid the IllegalStateException thrown by super call
* verifyUserCanSelectAll();
*/
@Override
protected void onDeselectAll(boolean userOriginated) {
if (userOriginated) {
// all selected state has been update in client side already
getState(false).allSelected = false;
getUI().getConnectorTracker().getDiffState(this).put("allSelected",
false);
} else {
getState().allSelected = false;
}
Field fs = null;
try {
fs = this.getClass().getSuperclass().getDeclaredField("selection");
fs.setAccessible(true);

List<T> selection = (List<T>) fs.get(this);
updateSelection(Collections.emptySet(), new LinkedHashSet<>(selection), userOriginated);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>org.vaadin.teemusa</groupId>
<artifactId>gridextensionpack-root</artifactId>
<packaging>pom</packaging>
<version>2.0-SNAPSHOT</version>
<version>2.0.3</version>
<name>GridExtensionPack Add-on Root Project</name>

<modules>
Expand Down