*/
package com.vaadin.client.ui;
+import java.util.Objects;
+
import com.google.gwt.user.client.ui.ListBox;
import com.vaadin.shared.ui.nativeselect.NativeSelectState;
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;
+ }
+ }
+ }
+ }
}
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;
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;
*/
@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();
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
package com.vaadin.ui;
import java.util.Collection;
-import java.util.Collections;
-import java.util.Set;
import com.vaadin.server.data.DataSource;
import com.vaadin.shared.data.DataCommunicatorConstants;
-import com.vaadin.shared.data.selection.SelectionModel;
import com.vaadin.shared.ui.nativeselect.NativeSelectState;
/**
*
* @see com.vaadin.ui.ComboBox
*/
-public class NativeSelect<T> extends AbstractListing<T, SelectionModel<T>> {
+public class NativeSelect<T> extends AbstractSingleSelect<T> {
/**
* Creates a new {@code NativeSelect} with an empty caption and no items.
addDataGenerator((item, json) -> json.put(
DataCommunicatorConstants.DATA, String.valueOf(item)));
- setSelectionModel(new SelectionModel<T>() {
-
- @Override
- public Set<T> getSelectedItems() {
- return Collections.emptySet();
- }
-
- @Override
- public void select(T item) {
- }
-
- @Override
- public void deselect(T item) {
- }
- });
+ setSelectionModel(new SimpleSingleSelection());
}
/**
import java.util.Locale;
import java.util.Map;
import java.util.Set;
+import java.util.function.Function;
import com.vaadin.annotations.Theme;
import com.vaadin.event.FieldEvents.BlurEvent;
import com.vaadin.server.Resource;
import com.vaadin.server.ThemeResource;
import com.vaadin.server.VaadinRequest;
+import com.vaadin.shared.Registration;
import com.vaadin.tests.util.Log;
import com.vaadin.tests.util.LoremIpsum;
import com.vaadin.ui.AbstractComponent;
}
}
+ protected void createListenerAction(String caption, String category,
+ Function<T, Registration> addListener) {
+
+ createBooleanAction(caption, category, false,
+ new Command<T, Boolean>() {
+ Registration registration;
+
+ @Override
+ public void execute(T c, Boolean enabled,
+ Object data) {
+ if (enabled) {
+ registration = addListener.apply(c);
+ } else if (registration != null) {
+ registration.remove();
+ registration = null;
+ }
+ }
+ });
+ }
+
protected LinkedHashMap<String, Integer> createIntegerOptions(int max) {
LinkedHashMap<String, Integer> options = new LinkedHashMap<>();
for (int i = 0; i <= 9 && i <= max; i++) {
protected Class<NativeSelect<Object>> getTestClass() {
return (Class) NativeSelect.class;
}
+
+ @Override
+ protected void createActions() {
+ super.createActions();
+ createListenerMenu();
+ }
+
+ protected void createListenerMenu() {
+ createListenerAction("Selection listener", "Listeners",
+ c -> c.addSelectionListener(
+ e -> log("Selected: " + e.getValue())));
+ }
}
import com.vaadin.testbench.TestBenchElement;
import com.vaadin.testbench.customelements.NativeSelectElement;
+import com.vaadin.testbench.elements.AbstractComponentElement.ReadOnlyException;
import com.vaadin.tests.tb3.MultiBrowserTest;
public class NativeSelectTest extends MultiBrowserTest {
assertItems(100);
}
+ @Test
+ public void selectItemProgrammatically_correctItemSelected() {
+ selectMenuPath("Component", "Selection", "Select", "Item 2");
+
+ assertEquals("Selected item", "Item 2", getSelect().getValue());
+ }
+
+ @Test
+ public void selectionListenerAdded_selectItem_listenerInvoked() {
+ selectMenuPath("Component", "Listeners", "Selection listener");
+
+ getSelect().selectByText("Item 5");
+
+ assertEquals("1. Selected: Item 5", getLogRow(0));
+ }
+
+ @Test
+ public void selectionListenerRemoved_selectItem_listenerNotInvoked() {
+ selectMenuPath("Component", "Listeners", "Selection listener");
+ selectMenuPath("Component", "Listeners", "Selection listener");
+
+ getSelect().selectByText("Item 5");
+
+ assertEquals("1. Command: /Selection listener(false)", getLogRow(0));
+ }
+
+ @Test(expected = ReadOnlyException.class)
+ public void setReadOnly_trySelectItem_throws() {
+ selectMenuPath("Component", "State", "Readonly");
+ getSelect().selectByText("Item 5");
+ }
+
@Override
protected Class<?> getUIClass() {
return NativeSelects.class;