diff options
author | Aleksi Hietanen <aleksi@vaadin.com> | 2016-10-03 10:30:05 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2016-10-06 08:43:28 +0000 |
commit | 6abb9c5c64c60db47e8f2dde87afaf0e76b2228c (patch) | |
tree | d7831d622a019523d22e63da8ada12075b79d590 /client/src | |
parent | 45082d1abcfec7f281c986c08fe0c1f065af29f2 (diff) | |
download | vaadin-framework-6abb9c5c64c60db47e8f2dde87afaf0e76b2228c.tar.gz vaadin-framework-6abb9c5c64c60db47e8f2dde87afaf0e76b2228c.zip |
Reintroduce reordering of grid columns
Change-Id: If8a23427ef5500a0177081c4be8065d2d5a0ca4c
Diffstat (limited to 'client/src')
3 files changed, 79 insertions, 3 deletions
diff --git a/client/src/main/java/com/vaadin/client/connectors/grid/GridConnector.java b/client/src/main/java/com/vaadin/client/connectors/grid/GridConnector.java index 6596137d9d..02909e1fc3 100644 --- a/client/src/main/java/com/vaadin/client/connectors/grid/GridConnector.java +++ b/client/src/main/java/com/vaadin/client/connectors/grid/GridConnector.java @@ -20,7 +20,9 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Optional; +import java.util.stream.Collectors; import com.google.gwt.dom.client.Element; import com.google.gwt.dom.client.EventTarget; @@ -132,7 +134,7 @@ public class GridConnector * the id of the column to get * @return the column with the given id */ - public Column<?, ?> getColumn(String columnId) { + public Column<?, JsonObject> getColumn(String columnId) { return idToColumn.get(columnId); } @@ -180,6 +182,16 @@ public class GridConnector getColumnId(event.getColumn()), event.isHidden()); } }); + getWidget().addColumnReorderHandler(event -> { + if (event.isUserOriginated()) { + List<String> newColumnOrder = mapColumnsToIds( + event.getNewColumnOrder()); + List<String> oldColumnOrder = mapColumnsToIds( + event.getOldColumnOrder()); + getRpcProxy(GridServerRpc.class) + .columnsReordered(newColumnOrder, oldColumnOrder); + } + }); getWidget().addColumnResizeHandler(event -> { Column<?, JsonObject> column = event.getColumn(); getRpcProxy(GridServerRpc.class).columnResized(getColumnId(column), @@ -219,6 +231,13 @@ public class GridConnector layout(); } + @SuppressWarnings("unchecked") + @OnStateChange("columnOrder") + void updateColumnOrder() { + getWidget().setColumnOrder(getState().columnOrder.stream() + .map(this::getColumn).toArray(size -> new Column[size])); + } + /** * Updates the grid header section on state change. */ @@ -446,4 +465,9 @@ public class GridConnector } return false; } + + private List<String> mapColumnsToIds(List<Column<?, JsonObject>> columns) { + return columns.stream().map(this::getColumnId).filter(Objects::nonNull) + .collect(Collectors.toList()); + } } diff --git a/client/src/main/java/com/vaadin/client/widget/grid/events/ColumnReorderEvent.java b/client/src/main/java/com/vaadin/client/widget/grid/events/ColumnReorderEvent.java index 72c0eb5878..0063c20b28 100644 --- a/client/src/main/java/com/vaadin/client/widget/grid/events/ColumnReorderEvent.java +++ b/client/src/main/java/com/vaadin/client/widget/grid/events/ColumnReorderEvent.java @@ -15,7 +15,10 @@ */ package com.vaadin.client.widget.grid.events; +import java.util.List; + import com.google.gwt.event.shared.GwtEvent; +import com.vaadin.client.widgets.Grid.Column; /** * An event for notifying that the columns in the Grid have been reordered. @@ -37,6 +40,48 @@ public class ColumnReorderEvent<T> extends GwtEvent<ColumnReorderHandler<T>> { return TYPE; } + private final List<Column<?, T>> oldColumnOrder; + + private final List<Column<?, T>> newColumnOrder; + + private final boolean userOriginated; + + public ColumnReorderEvent(List<Column<?, T>> oldColumnOrder, + List<Column<?, T>> newColumnOrder, boolean userOriginated) { + this.oldColumnOrder = oldColumnOrder; + this.newColumnOrder = newColumnOrder; + this.userOriginated = userOriginated; + } + + /** + * Gets the ordering of columns prior to this event. + * + * @return the list of columns in the grid's order prior to this event + */ + public List<Column<?, T>> getOldColumnOrder() { + return oldColumnOrder; + } + + /** + * Gets the new ordering of columns. + * + * @return the list of columns in the grid's current order + */ + public List<Column<?, T>> getNewColumnOrder() { + return newColumnOrder; + } + + /** + * Check whether this event originated from the user reordering columns or + * via API call. + * + * @return {@code true} if columns were reordered by the user, {@code false} + * if not + */ + public boolean isUserOriginated() { + return userOriginated; + } + @SuppressWarnings({ "rawtypes", "unchecked" }) @Override public Type<ColumnReorderHandler<T>> getAssociatedType() { diff --git a/client/src/main/java/com/vaadin/client/widgets/Grid.java b/client/src/main/java/com/vaadin/client/widgets/Grid.java index eac958fdce..7d6c1369b8 100644 --- a/client/src/main/java/com/vaadin/client/widgets/Grid.java +++ b/client/src/main/java/com/vaadin/client/widgets/Grid.java @@ -4372,7 +4372,7 @@ public class Grid<T> extends ResizeComposite implements HasSelectionHandlers<T>, Column<?, T>[] array = reordered .toArray(new Column[reordered.size()]); - setColumnOrder(array); + setColumnOrder(true, array); transferCellFocusOnDrop(); } // else no reordering } @@ -8171,6 +8171,12 @@ public class Grid<T> extends ResizeComposite implements HasSelectionHandlers<T>, * array of columns in wanted order */ public void setColumnOrder(Column<?, T>... orderedColumns) { + setColumnOrder(false, orderedColumns); + } + + private void setColumnOrder(boolean isUserOriginated, + Column<?, T>... orderedColumns) { + List<Column<?, T>> oldOrder = new ArrayList<>(columns); ColumnConfiguration conf = getEscalator().getColumnConfiguration(); // Trigger ComplexRenderer.destroy for old content @@ -8221,7 +8227,8 @@ public class Grid<T> extends ResizeComposite implements HasSelectionHandlers<T>, columnHider.updateTogglesOrder(); - fireEvent(new ColumnReorderEvent<T>()); + fireEvent(new ColumnReorderEvent<T>(oldOrder, newOrder, + isUserOriginated)); } /** |