From 30a1416da4fdbc13cde6662498690792808a87b7 Mon Sep 17 00:00:00 2001 From: Pekka Hyvönen Date: Thu, 16 Feb 2017 10:14:38 +0200 Subject: Reduce ComboBox initial requests (#8571) * Reduce ComboBox initial requests Use initial fetched data on client side, do not request data from server side for each time popup is opened. Fixed initial filter being null for ComboBox on DataProvider, causing unnecessary size & fetch for not-changed filter. Fixed ComboBox sending default filter unnecessarily to server. Fixed wrong page indexing in VComboBox -> ComboBoxConnector. Fixes #8496 Fixes vaadin/framework8-issues#488 * Fix last item missing When pageLength was 0 and nullSelectionAllowed, the last item was not shown. Tried to sensify the API for total suggestions versus total suggestions + null selection item. * Fix ComboBox selected item updates Handles changing of ItemCaptionGenerator or ItemIconGenerator, need to update the selected item caption and icon separately. Previously it worked because all data was sent all the time to client. Doesn't fix the issue, when selected item is updated with refreshItem(), and it is not on the active range that will be sent to client. For that, ComboBox would need a separate notification about item update. * Updated screenshots --- server/src/main/java/com/vaadin/ui/ComboBox.java | 35 +++++++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) (limited to 'server') diff --git a/server/src/main/java/com/vaadin/ui/ComboBox.java b/server/src/main/java/com/vaadin/ui/ComboBox.java index a2db94a8c6..ed1e9d8beb 100644 --- a/server/src/main/java/com/vaadin/ui/ComboBox.java +++ b/server/src/main/java/com/vaadin/ui/ComboBox.java @@ -575,6 +575,9 @@ public class ComboBox extends AbstractSingleSelect public void setItemCaptionGenerator( ItemCaptionGenerator itemCaptionGenerator) { super.setItemCaptionGenerator(itemCaptionGenerator); + if (getSelectedItem().isPresent()) { + updateSelectedItemCaption(); + } } /** @@ -614,6 +617,10 @@ public class ComboBox extends AbstractSingleSelect @Override public void setItemIconGenerator(IconGenerator itemIconGenerator) { super.setItemIconGenerator(itemIconGenerator); + + if (getSelectedItem().isPresent()) { + updateSelectedItemIcon(); + } } @Override @@ -631,7 +638,7 @@ public class ComboBox extends AbstractSingleSelect */ public void setNewItemHandler(NewItemHandler newItemHandler) { this.newItemHandler = newItemHandler; - getState().allowNewItems = (newItemHandler != null); + getState().allowNewItems = newItemHandler != null; markAsDirty(); } @@ -670,14 +677,32 @@ public class ComboBox extends AbstractSingleSelect protected void doSetSelectedKey(String key) { super.doSetSelectedKey(key); + updateSelectedItemCaption(); + updateSelectedItemIcon(); + } + + private void updateSelectedItemCaption() { String selectedCaption = null; - T value = getDataCommunicator().getKeyMapper().get(key); + T value = getDataCommunicator().getKeyMapper().get(getSelectedKey()); if (value != null) { selectedCaption = getItemCaptionGenerator().apply(value); } getState().selectedItemCaption = selectedCaption; } + private void updateSelectedItemIcon() { + String selectedItemIcon = null; + T value = getDataCommunicator().getKeyMapper().get(getSelectedKey()); + if (value != null) { + Resource icon = getItemIconGenerator().apply(value); + if (icon != null) { + selectedItemIcon = ResourceReference + .create(icon, ComboBox.this, null).getURL(); + } + } + getState().selectedItemIcon = selectedItemIcon; + } + @Override protected Element writeItem(Element design, T item, DesignContext context) { Element element = design.appendElement("option"); @@ -748,7 +773,7 @@ public class ComboBox extends AbstractSingleSelect "filterConverter cannot be null"); SerializableFunction convertOrNull = filterText -> { - if (filterText == null) { + if (filterText == null || filterText.isEmpty()) { return null; } @@ -814,10 +839,12 @@ public class ComboBox extends AbstractSingleSelect extends SerializableBiPredicate { /** - * Check item caption against entered text + * Check item caption against entered text. * * @param itemCaption + * the caption of the item to filter, not {@code null} * @param filterText + * user entered filter, not {@code null} * @return {@code true} if item passes the filter and should be listed, * {@code false} otherwise */ -- cgit v1.2.3