From 84c143dd76ed1d27d03c0d695e7218b477d008fe Mon Sep 17 00:00:00 2001 From: Henrik Paul Date: Mon, 9 Mar 2015 14:31:37 +0200 Subject: Server side Grid can open details on the client side (#16644) Change-Id: Ibff5a83b3a09c7c530926dadae9138ba3823f27a --- .../com/vaadin/data/RpcDataProviderExtension.java | 72 ++++++++++++++++- server/src/com/vaadin/ui/Grid.java | 91 ++++++++++++++++++++++ 2 files changed, 160 insertions(+), 3 deletions(-) (limited to 'server/src/com/vaadin') diff --git a/server/src/com/vaadin/data/RpcDataProviderExtension.java b/server/src/com/vaadin/data/RpcDataProviderExtension.java index 991cb0537d..cf2284a62e 100644 --- a/server/src/com/vaadin/data/RpcDataProviderExtension.java +++ b/server/src/com/vaadin/data/RpcDataProviderExtension.java @@ -95,7 +95,7 @@ public class RpcDataProviderExtension extends AbstractExtension { // private implementation } - void setActiveRange(Range newActiveRange) { + public void setActiveRange(Range newActiveRange) { final Range[] removed = activeRange.partitionWith(newActiveRange); final Range[] added = newActiveRange.partitionWith(activeRange); @@ -163,7 +163,7 @@ public class RpcDataProviderExtension extends AbstractExtension { return String.valueOf(rollingIndex++); } - String getKey(Object itemId) { + public String getKey(Object itemId) { String key = itemIdToKey.get(itemId); if (key == null) { key = nextKey(); @@ -240,6 +240,20 @@ public class RpcDataProviderExtension extends AbstractExtension { return itemIds; } + /** + * Gets the row index for a given item. + * + * @since + * @param itemId + * the item id of the item for which to get the item + * @return the index of the item, or -1 if no such item could be found + */ + @SuppressWarnings("boxing") + public int getIndex(Object itemId) { + Integer integer = indexToItemId.inverse().get(itemId); + return integer != null ? integer : -1; + } + /** * Pin an item id to be cached indefinitely. *

@@ -304,7 +318,7 @@ public class RpcDataProviderExtension extends AbstractExtension { return pinnedItemIds.contains(itemId); } - Object itemIdAtIndex(int index) { + private Object itemIdAtIndex(int index) { return indexToItemId.get(Integer.valueOf(index)); } } @@ -727,6 +741,12 @@ public class RpcDataProviderExtension extends AbstractExtension { /** Size possibly changed with a bare ItemSetChangeEvent */ private boolean bareItemSetTriggeredSizeChange = false; + /** + * This map represents all the details that are user-defined as visible. + * This does not reflect the status in the DOM. + */ + private Set visibleDetails = new HashSet(); + /** * Creates a new data provider using the given container. * @@ -859,6 +879,10 @@ public class RpcDataProviderExtension extends AbstractExtension { rowObject.put(GridState.JSONKEY_DATA, rowData); rowObject.put(GridState.JSONKEY_ROWKEY, keyMapper.getKey(itemId)); + if (visibleDetails.contains(itemId)) { + rowObject.put(GridState.JSONKEY_DETAILS_VISIBLE, true); + } + rowReference.set(itemId); CellStyleGenerator cellStyleGenerator = grid.getCellStyleGenerator(); @@ -1116,4 +1140,46 @@ public class RpcDataProviderExtension extends AbstractExtension { return Logger.getLogger(RpcDataProviderExtension.class.getName()); } + /** + * Marks a row's details to be visible or hidden. + *

+ * If that row is currently in the client side's cache, this information + * will be sent over to the client. + * + * @since + * @param itemId + * the id of the item of which to change the details visibility + * @param visible + * true to show the details, false to + * hide + */ + public void setDetailsVisible(Object itemId, boolean visible) { + final boolean modified; + if (visible) { + modified = visibleDetails.add(itemId); + } else { + modified = visibleDetails.remove(itemId); + } + + int rowIndex = keyMapper.getIndex(itemId); + boolean modifiedRowIsActive = activeRowHandler.activeRange + .contains(rowIndex); + if (modified && modifiedRowIsActive) { + updateRowData(itemId); + } + } + + /** + * Checks whether the details for a row is marked as visible. + * + * @since + * @param itemId + * the id of the item of which to check the visibility + * @return true iff the detials are visible for the item. This + * might return true even if the row is not currently + * visible in the DOM + */ + public boolean isDetailsVisible(Object itemId) { + return visibleDetails.contains(itemId); + } } diff --git a/server/src/com/vaadin/ui/Grid.java b/server/src/com/vaadin/ui/Grid.java index 22ef0333c2..b56bb0d036 100644 --- a/server/src/com/vaadin/ui/Grid.java +++ b/server/src/com/vaadin/ui/Grid.java @@ -164,6 +164,34 @@ import elemental.json.JsonValue; public class Grid extends AbstractComponent implements SelectionNotifier, SortNotifier, SelectiveRenderer, ItemClickNotifier { + /** + * A callback interface for generating details for a particular row in Grid. + * + * @since + * @author Vaadin Ltd + */ + public interface DetailsGenerator extends Serializable { + + /** A details generator that provides no details */ + public DetailsGenerator NULL = new DetailsGenerator() { + @Override + public Component getDetails(RowReference rowReference) { + return null; + } + }; + + /** + * This method is called for whenever a new details row needs to be + * generated. + * + * @param rowReference + * the reference for the row for which to generate details + * @return the details for the given row, or null to leave + * the details empty. + */ + Component getDetails(RowReference rowReference); + } + /** * Custom field group that allows finding property types before an item has * been bound. @@ -2888,6 +2916,8 @@ public class Grid extends AbstractComponent implements SelectionNotifier, private EditorErrorHandler editorErrorHandler = new DefaultEditorErrorHandler(); + private DetailsGenerator detailsGenerator = DetailsGenerator.NULL; + private static final Method SELECTION_CHANGE_METHOD = ReflectTools .findMethod(SelectionListener.class, "select", SelectionEvent.class); @@ -5093,4 +5123,65 @@ public class Grid extends AbstractComponent implements SelectionNotifier, public void recalculateColumnWidths() { getRpcProxy(GridClientRpc.class).recalculateColumnWidths(); } + + /** + * Sets a new details generator for row details. + *

+ * The currently opened row details will be re-rendered. + * + * @since + * @param detailsGenerator + * the details generator to set + * @throws IllegalArgumentException + * if detailsGenerator is null; + */ + public void setDetailsGenerator(DetailsGenerator detailsGenerator) + throws IllegalArgumentException { + if (detailsGenerator == null) { + throw new IllegalArgumentException( + "Details generator may not be null"); + } else if (detailsGenerator == this.detailsGenerator) { + return; + } + + this.detailsGenerator = detailsGenerator; + + getLogger().warning("[[details]] update details on generator swap"); + } + + /** + * Gets the current details generator for row details. + * + * @since + * @return the detailsGenerator the current details generator + */ + public DetailsGenerator getDetailsGenerator() { + return detailsGenerator; + } + + /** + * Shows or hides the details for a specific item. + * + * @since + * @param itemId + * the id of the item for which to set details visibility + * @param visible + * true to show the details, or false + * to hide them + */ + public void setDetailsVisible(Object itemId, boolean visible) { + datasourceExtension.setDetailsVisible(itemId, visible); + } + + /** + * Checks whether details are visible for the given item. + * + * @since + * @param itemId + * the id of the item for which to check details visibility + * @return true iff the details are visible + */ + public boolean isDetailsVisible(Object itemId) { + return datasourceExtension.isDetailsVisible(itemId); + } } -- cgit v1.2.3