aboutsummaryrefslogtreecommitdiffstats
path: root/client/src
diff options
context:
space:
mode:
authorJohannes Dahlström <johannesd@vaadin.com>2016-09-08 22:49:54 +0300
committerArtur Signell <artur@vaadin.com>2016-09-12 09:36:08 +0000
commit8588b4a7759dff9a3862079d516f5201be55982c (patch)
tree0dbfb02ac867e7c565358b40f1e06f8ecdb38f96 /client/src
parentf5104e34f3167fa2bf05e93272f5b71c15d00071 (diff)
downloadvaadin-framework-8588b4a7759dff9a3862079d516f5201be55982c.tar.gz
vaadin-framework-8588b4a7759dff9a3862079d516f5201be55982c.zip
Add selection support to NativeSelect
Change-Id: Iabe563852150a7d690f6e2edaa40253d03127881
Diffstat (limited to 'client/src')
-rw-r--r--client/src/main/java/com/vaadin/client/ui/VNativeSelect.java23
-rw-r--r--client/src/main/java/com/vaadin/client/ui/nativeselect/NativeSelectConnector.java34
2 files changed, 56 insertions, 1 deletions
diff --git a/client/src/main/java/com/vaadin/client/ui/VNativeSelect.java b/client/src/main/java/com/vaadin/client/ui/VNativeSelect.java
index 4a3d95c0a5..716450c82a 100644
--- a/client/src/main/java/com/vaadin/client/ui/VNativeSelect.java
+++ b/client/src/main/java/com/vaadin/client/ui/VNativeSelect.java
@@ -15,6 +15,8 @@
*/
package com.vaadin.client.ui;
+import java.util.Objects;
+
import com.google.gwt.user.client.ui.ListBox;
import com.vaadin.shared.ui.nativeselect.NativeSelectState;
@@ -31,4 +33,25 @@ public class VNativeSelect extends ListBox {
public VNativeSelect() {
setStyleName(NativeSelectState.STYLE_NAME);
}
+
+ /**
+ * Sets the selected item by its value. If given {@code null}, removes
+ * selection.
+ *
+ * @param value
+ * the value of the item to select or {@code null} to select
+ * nothing
+ */
+ public void setSelectedItem(String value) {
+ if (value == null) {
+ setSelectedIndex(-1);
+ } else {
+ for (int i = 0; i < getItemCount(); i++) {
+ if (Objects.equals(value, getValue(i))) {
+ setSelectedIndex(i);
+ break;
+ }
+ }
+ }
+ }
}
diff --git a/client/src/main/java/com/vaadin/client/ui/nativeselect/NativeSelectConnector.java b/client/src/main/java/com/vaadin/client/ui/nativeselect/NativeSelectConnector.java
index afa202e174..c61b7efc3c 100644
--- a/client/src/main/java/com/vaadin/client/ui/nativeselect/NativeSelectConnector.java
+++ b/client/src/main/java/com/vaadin/client/ui/nativeselect/NativeSelectConnector.java
@@ -16,6 +16,7 @@
package com.vaadin.client.ui.nativeselect;
+import com.google.gwt.event.shared.HandlerRegistration;
import com.vaadin.client.annotations.OnStateChange;
import com.vaadin.client.connectors.AbstractListingConnector;
import com.vaadin.client.data.DataSource;
@@ -23,7 +24,9 @@ import com.vaadin.client.ui.VNativeSelect;
import com.vaadin.shared.Range;
import com.vaadin.shared.Registration;
import com.vaadin.shared.data.selection.SelectionModel;
+import com.vaadin.shared.data.selection.SelectionServerRpc;
import com.vaadin.shared.ui.Connect;
+import com.vaadin.shared.ui.nativeselect.NativeSelectState;
import elemental.json.JsonObject;
@@ -39,10 +42,29 @@ import elemental.json.JsonObject;
*/
@Connect(com.vaadin.ui.NativeSelect.class)
public class NativeSelectConnector extends
- AbstractListingConnector<SelectionModel<?>> {
+ AbstractListingConnector<SelectionModel.Single<?>> {
+ private HandlerRegistration selectionChangeRegistration;
private Registration dataChangeRegistration;
+ private final SelectionServerRpc selectionRpc = getRpcProxy(
+ SelectionServerRpc.class);
+
+ @Override
+ protected void init() {
+ super.init();
+
+ selectionChangeRegistration = getWidget().addChangeHandler(
+ e -> selectionRpc.select(getWidget().getSelectedValue()));
+ }
+
+ @Override
+ public void onUnregister() {
+ super.onUnregister();
+ selectionChangeRegistration.removeHandler();
+ selectionChangeRegistration = null;
+ }
+
@Override
public VNativeSelect getWidget() {
return (VNativeSelect) super.getWidget();
@@ -64,6 +86,16 @@ public class NativeSelectConnector extends
getWidget().setEnabled(isEnabled() && !isReadOnly());
}
+ @OnStateChange("selectedItemKey")
+ void updateSelectedItem() {
+ getWidget().setSelectedItem(getState().selectedItemKey);
+ }
+
+ @Override
+ public NativeSelectState getState() {
+ return (NativeSelectState) super.getState();
+ }
+
/**
* A data change handler registered to the data source. Updates the data
* items and selection status when the data source notifies of new changes