From 802bb98b851ab9b15617b283ccdba3dbc517ada6 Mon Sep 17 00:00:00 2001 From: Piotr Wilkin Date: Tue, 10 Oct 2017 12:02:47 +0200 Subject: Edit grid row by index - server side (#10040) Opening grid editor from server side. Fixes #8477. Addressing #8820 will be the user's responsibility as fetching index of item might be slow. --- .../com/vaadin/data/provider/DataCommunicator.java | 2 +- .../provider/HierarchicalDataCommunicator.java | 2 +- .../java/com/vaadin/ui/components/grid/Editor.java | 10 ++++++ .../com/vaadin/ui/components/grid/EditorImpl.java | 37 ++++++++++++++++++++++ 4 files changed, 49 insertions(+), 2 deletions(-) (limited to 'server/src') diff --git a/server/src/main/java/com/vaadin/data/provider/DataCommunicator.java b/server/src/main/java/com/vaadin/data/provider/DataCommunicator.java index 9996d8d1aa..66bf758efe 100644 --- a/server/src/main/java/com/vaadin/data/provider/DataCommunicator.java +++ b/server/src/main/java/com/vaadin/data/provider/DataCommunicator.java @@ -370,7 +370,7 @@ public class DataCommunicator extends AbstractExtension { * @since 8.1 */ @SuppressWarnings({ "rawtypes", "unchecked" }) - protected List fetchItemsWithRange(int offset, int limit) { + public List fetchItemsWithRange(int offset, int limit) { return (List) getDataProvider().fetch(new Query(offset, limit, backEndSorting, inMemorySorting, filter)) .collect(Collectors.toList()); diff --git a/server/src/main/java/com/vaadin/data/provider/HierarchicalDataCommunicator.java b/server/src/main/java/com/vaadin/data/provider/HierarchicalDataCommunicator.java index 627cc28651..b0397ffcc7 100644 --- a/server/src/main/java/com/vaadin/data/provider/HierarchicalDataCommunicator.java +++ b/server/src/main/java/com/vaadin/data/provider/HierarchicalDataCommunicator.java @@ -66,7 +66,7 @@ public class HierarchicalDataCommunicator extends DataCommunicator { } @Override - protected List fetchItemsWithRange(int offset, int limit) { + public List fetchItemsWithRange(int offset, int limit) { // Instead of adding logic to this class, delegate request to the // separate object handling hierarchies. return mapper.fetchItems(Range.withLength(offset, limit)) diff --git a/server/src/main/java/com/vaadin/ui/components/grid/Editor.java b/server/src/main/java/com/vaadin/ui/components/grid/Editor.java index 26ef8101ab..8c34fbdf64 100644 --- a/server/src/main/java/com/vaadin/ui/components/grid/Editor.java +++ b/server/src/main/java/com/vaadin/ui/components/grid/Editor.java @@ -103,6 +103,16 @@ public interface Editor extends Serializable { */ public void cancel(); + /** + * Edits the selected row + * + * @param rowNumber + * the row to edit + * + * @since 8.2 + */ + public void editRow(int rowNumber); + /** * Sets the caption of the save button in buffered mode. * diff --git a/server/src/main/java/com/vaadin/ui/components/grid/EditorImpl.java b/server/src/main/java/com/vaadin/ui/components/grid/EditorImpl.java index 4d5e91b671..7329677ca3 100644 --- a/server/src/main/java/com/vaadin/ui/components/grid/EditorImpl.java +++ b/server/src/main/java/com/vaadin/ui/components/grid/EditorImpl.java @@ -267,6 +267,43 @@ public class EditorImpl extends AbstractGridExtension rpc.cancel(); } + /** + * Opens the editor interface for the provided row. Scrolls the Grid to + * bring the row to view if it is not already visible. + * + * Note that any cell content rendered by a WidgetRenderer will not be + * visible in the editor row. + * + * @param rowNumber + * the row number of the edited item + * @throws IllegalStateException + * if the editor is not enabled or already editing a different item + * in buffered mode + * @throws IllegalArgumentException + * if the {@code rowNumber} is not in the backing data provider + * @see #setEnabled(boolean) + */ + public void editRow(int rowNumber) + throws IllegalStateException, IllegalArgumentException { + if (!isEnabled()) { + throw new IllegalStateException("Item editor is not enabled"); + } + T beanToEdit = getParent().getDataCommunicator(). + fetchItemsWithRange(rowNumber, 1). + stream().findFirst().orElseThrow(() -> new IllegalArgumentException( + "Row number " + rowNumber+ "did not yield any item from data provider")); + if (!beanToEdit.equals(edited)) { + if (isBuffered() && edited != null) { + throw new IllegalStateException("Editing item " + beanToEdit + + " failed. Item editor is already editing item " + + edited); + } else { + rpc.bind(rowNumber); + } + } + + } + private void doCancel(boolean afterBeingSaved) { T editedBean = edited; doClose(); -- cgit v1.2.3