summaryrefslogtreecommitdiffstats
path: root/client
diff options
context:
space:
mode:
authorDenis Anisimov <denis@vaadin.com>2016-11-29 15:27:20 +0300
committerVaadin Code Review <review@vaadin.com>2016-12-01 07:42:43 +0000
commit1bb04399f3b825203a2c6d62154750768d75003e (patch)
treef90007d4358de336e05fcadd5d2f99c0999d6e19 /client
parente4fbf7a5f2ab83d613100ec83aac99a3f88bf8b9 (diff)
downloadvaadin-framework-1bb04399f3b825203a2c6d62154750768d75003e.tar.gz
vaadin-framework-1bb04399f3b825203a2c6d62154750768d75003e.zip
Allow to set caption for the empty selection in a ComboBox.
Fixes vaadin/framework8-issues#163 Change-Id: Ib68ad5421934c8375a91d7948d860381a5adb9bb
Diffstat (limited to 'client')
-rw-r--r--client/src/main/java/com/vaadin/client/ui/VComboBox.java4
-rw-r--r--client/src/main/java/com/vaadin/client/ui/combobox/ComboBoxConnector.java173
2 files changed, 101 insertions, 76 deletions
diff --git a/client/src/main/java/com/vaadin/client/ui/VComboBox.java b/client/src/main/java/com/vaadin/client/ui/VComboBox.java
index b19f9944ba..6ecea2d116 100644
--- a/client/src/main/java/com/vaadin/client/ui/VComboBox.java
+++ b/client/src/main/java/com/vaadin/client/ui/VComboBox.java
@@ -1592,7 +1592,7 @@ public class VComboBox extends Composite implements Field, KeyDownHandler,
* <p>
* For internal use only. May be removed or replaced in the future.
*/
- public final List<ComboBoxSuggestion> currentSuggestions = new ArrayList<ComboBoxSuggestion>();
+ public final List<ComboBoxSuggestion> currentSuggestions = new ArrayList<>();
/** For internal use only. May be removed or replaced in the future. */
public String serverSelectedKey;
@@ -2067,7 +2067,7 @@ public class VComboBox extends Composite implements Field, KeyDownHandler,
Unit.PX);
}
- private static Set<Integer> navigationKeyCodes = new HashSet<Integer>();
+ private static Set<Integer> navigationKeyCodes = new HashSet<>();
static {
navigationKeyCodes.add(KeyCodes.KEY_DOWN);
navigationKeyCodes.add(KeyCodes.KEY_UP);
diff --git a/client/src/main/java/com/vaadin/client/ui/combobox/ComboBoxConnector.java b/client/src/main/java/com/vaadin/client/ui/combobox/ComboBoxConnector.java
index 8b56483e8f..a4a9c17f4d 100644
--- a/client/src/main/java/com/vaadin/client/ui/combobox/ComboBoxConnector.java
+++ b/client/src/main/java/com/vaadin/client/ui/combobox/ComboBoxConnector.java
@@ -15,6 +15,8 @@
*/
package com.vaadin.client.ui.combobox;
+import java.util.List;
+
import com.vaadin.client.Profiler;
import com.vaadin.client.annotations.OnStateChange;
import com.vaadin.client.communication.StateChangeEvent;
@@ -25,6 +27,7 @@ import com.vaadin.client.ui.HasErrorIndicator;
import com.vaadin.client.ui.HasRequiredIndicator;
import com.vaadin.client.ui.SimpleManagedLayout;
import com.vaadin.client.ui.VComboBox;
+import com.vaadin.client.ui.VComboBox.ComboBoxSuggestion;
import com.vaadin.client.ui.VComboBox.DataReceivedHandler;
import com.vaadin.shared.EventId;
import com.vaadin.shared.Registration;
@@ -87,6 +90,15 @@ public class ComboBoxConnector extends AbstractListingConnector
Profiler.leave("ComboBoxConnector.onStateChanged update content");
}
+ @OnStateChange("emptySelectionCaption")
+ private void onEmptySelectionCaptionChange() {
+ List<ComboBoxSuggestion> suggestions = getWidget().currentSuggestions;
+ if (!suggestions.isEmpty() && isFirstPage()) {
+ suggestions.remove(0);
+ addEmptySelectionItem();
+ }
+ }
+
@OnStateChange({ "selectedItemKey", "selectedItemCaption" })
private void onSelectionChange() {
getDataReceivedHandler().updateSelectionFromServer(
@@ -249,80 +261,7 @@ public class ComboBoxConnector extends AbstractListingConnector
public void setDataSource(DataSource<JsonObject> dataSource) {
super.setDataSource(dataSource);
dataChangeHandlerRegistration = dataSource
- .addDataChangeHandler(range -> {
- // try to find selected item if requested
- if (getState().scrollToSelectedItem
- && getState().pageLength > 0
- && getWidget().currentPage < 0
- && getWidget().selectedOptionKey != null) {
- // search for the item with the selected key
- getWidget().currentPage = 0;
- for (int i = 0; i < getDataSource().size(); ++i) {
- JsonObject row = getDataSource().getRow(i);
- if (row != null) {
- String key = getRowKey(row);
- if (getWidget().selectedOptionKey.equals(key)) {
- if (getWidget().nullSelectionAllowed) {
- getWidget().currentPage = (i + 1)
- / getState().pageLength;
- } else {
- getWidget().currentPage = i
- / getState().pageLength;
- }
- break;
- }
- }
- }
- } else if (getWidget().currentPage < 0) {
- getWidget().currentPage = 0;
- }
-
- getWidget().currentSuggestions.clear();
-
- int start = getWidget().currentPage
- * getWidget().pageLength;
- int end = getWidget().pageLength > 0
- ? start + getWidget().pageLength
- : getDataSource().size();
-
- if (getWidget().nullSelectionAllowed
- && "".equals(getWidget().lastFilter)) {
- // add special null selection item...
- if (getWidget().currentPage == 0) {
- getWidget().currentSuggestions
- .add(getWidget().new ComboBoxSuggestion("",
- "", null, null));
- } else {
- // ...or leave space for it
- start = start - 1;
- }
- // in either case, the last item to show is
- // shifted by one
- end = end - 1;
- }
-
- for (int i = start; i < end; ++i) {
- JsonObject row = getDataSource().getRow(i);
-
- if (row != null) {
- String key = getRowKey(row);
- String caption = row
- .getString(DataCommunicatorConstants.NAME);
- String style = row
- .getString(ComboBoxConstants.STYLE);
- String untranslatedIconUri = row
- .getString(ComboBoxConstants.ICON);
- getWidget().currentSuggestions
- .add(getWidget().new ComboBoxSuggestion(key,
- caption, style,
- untranslatedIconUri));
- }
- }
- getWidget().totalMatches = getDataSource().size()
- + (getState().emptySelectionAllowed ? 1 : 0);
-
- getDataReceivedHandler().dataReceived();
- });
+ .addDataChangeHandler(range -> refreshData());
}
@Override
@@ -335,4 +274,90 @@ public class ComboBoxConnector extends AbstractListingConnector
public boolean isRequiredIndicatorVisible() {
return getState().required && !isReadOnly();
}
+
+ private void refreshData() {
+ updateCurrentPage();
+
+ getWidget().currentSuggestions.clear();
+
+ int start = getWidget().currentPage * getWidget().pageLength;
+ int end = getWidget().pageLength > 0 ? start + getWidget().pageLength
+ : getDataSource().size();
+
+ if (getWidget().nullSelectionAllowed
+ && "".equals(getWidget().lastFilter)) {
+ // add special null selection item...
+ if (isFirstPage()) {
+ addEmptySelectionItem();
+ } else {
+ // ...or leave space for it
+ start = start - 1;
+ }
+ // in either case, the last item to show is
+ // shifted by one
+ end = end - 1;
+ }
+
+ updateSuggestions(start, end);
+ getWidget().totalMatches = getDataSource().size()
+ + (getState().emptySelectionAllowed ? 1 : 0);
+
+ getDataReceivedHandler().dataReceived();
+ }
+
+ private void updateSuggestions(int start, int end) {
+ for (int i = start; i < end; ++i) {
+ JsonObject row = getDataSource().getRow(i);
+
+ if (row != null) {
+ String key = getRowKey(row);
+ String caption = row.getString(DataCommunicatorConstants.NAME);
+ String style = row.getString(ComboBoxConstants.STYLE);
+ String untranslatedIconUri = row
+ .getString(ComboBoxConstants.ICON);
+ getWidget().currentSuggestions
+ .add(getWidget().new ComboBoxSuggestion(key, caption,
+ style, untranslatedIconUri));
+ }
+ }
+ }
+
+ private boolean isFirstPage() {
+ return getWidget().currentPage == 0;
+ }
+
+ private void addEmptySelectionItem() {
+ if (isFirstPage()) {
+ getWidget().currentSuggestions.add(0,
+ getWidget().new ComboBoxSuggestion("",
+ getState().emptySelectionCaption, null, null));
+ }
+ }
+
+ private void updateCurrentPage() {
+ // try to find selected item if requested
+ if (getState().scrollToSelectedItem && getState().pageLength > 0
+ && getWidget().currentPage < 0
+ && getWidget().selectedOptionKey != null) {
+ // search for the item with the selected key
+ getWidget().currentPage = 0;
+ for (int i = 0; i < getDataSource().size(); ++i) {
+ JsonObject row = getDataSource().getRow(i);
+ if (row != null) {
+ String key = getRowKey(row);
+ if (getWidget().selectedOptionKey.equals(key)) {
+ if (getWidget().nullSelectionAllowed) {
+ getWidget().currentPage = (i + 1)
+ / getState().pageLength;
+ } else {
+ getWidget().currentPage = i / getState().pageLength;
+ }
+ break;
+ }
+ }
+ }
+ } else if (getWidget().currentPage < 0) {
+ getWidget().currentPage = 0;
+ }
+ }
}