From f5104e34f3167fa2bf05e93272f5b71c15d00071 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Johannes=20Dahlstr=C3=B6m?= Date: Thu, 8 Sep 2016 22:16:08 +0300 Subject: [PATCH] Update NativeSelect to use DataSource, extend AbstractListing Selection and focus/blur support not yet implemented. Change-Id: I76752084442216e60055d93367475c1c0a612787 --- .../com/vaadin/client/ui/VNativeSelect.java | 34 ++++++ .../nativeselect/NativeSelectConnector.java | 102 ++++++++++++++++ .../main/java/com/vaadin/ui/NativeSelect.java | 114 ++++++++++++++++++ .../ui/nativeselect/NativeSelectState.java | 37 ++++++ .../customelements/NativeSelectElement.java | 75 ++++++++++++ .../AbstractListingTestUI.java | 14 ++- .../nativeselect/NativeSelects.java | 14 +++ .../nativeselect/NativeSelectTest.java | 68 +++++++++++ 8 files changed, 452 insertions(+), 6 deletions(-) create mode 100644 client/src/main/java/com/vaadin/client/ui/VNativeSelect.java create mode 100644 client/src/main/java/com/vaadin/client/ui/nativeselect/NativeSelectConnector.java create mode 100644 server/src/main/java/com/vaadin/ui/NativeSelect.java create mode 100644 shared/src/main/java/com/vaadin/shared/ui/nativeselect/NativeSelectState.java create mode 100644 uitest-common/src/main/java/com/vaadin/testbench/customelements/NativeSelectElement.java create mode 100644 uitest/src/main/java/com/vaadin/tests/components/nativeselect/NativeSelects.java create mode 100644 uitest/src/test/java/com/vaadin/tests/components/nativeselect/NativeSelectTest.java diff --git a/client/src/main/java/com/vaadin/client/ui/VNativeSelect.java b/client/src/main/java/com/vaadin/client/ui/VNativeSelect.java new file mode 100644 index 0000000000..4a3d95c0a5 --- /dev/null +++ b/client/src/main/java/com/vaadin/client/ui/VNativeSelect.java @@ -0,0 +1,34 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.client.ui; + +import com.google.gwt.user.client.ui.ListBox; +import com.vaadin.shared.ui.nativeselect.NativeSelectState; + +/** + * The client-side widget for the {@code NativeSelect} component. + * + * @author Vaadin Ltd. + */ +public class VNativeSelect extends ListBox { + + /** + * Creates a new {@code VNativeSelect} instance. + */ + public VNativeSelect() { + setStyleName(NativeSelectState.STYLE_NAME); + } +} 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 new file mode 100644 index 0000000000..afa202e174 --- /dev/null +++ b/client/src/main/java/com/vaadin/client/ui/nativeselect/NativeSelectConnector.java @@ -0,0 +1,102 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.vaadin.client.ui.nativeselect; + +import com.vaadin.client.annotations.OnStateChange; +import com.vaadin.client.connectors.AbstractListingConnector; +import com.vaadin.client.data.DataSource; +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.ui.Connect; + +import elemental.json.JsonObject; + +/** + * The client-side connector for the {@code NativeSelect} component. + * + * @author Vaadin Ltd. + * + * @see com.vaadin.ui.NativeSelect + * @see com.vaadin.client.ui.VNativeSelect + * + * @since 8.0 + */ +@Connect(com.vaadin.ui.NativeSelect.class) +public class NativeSelectConnector extends + AbstractListingConnector> { + + private Registration dataChangeRegistration; + + @Override + public VNativeSelect getWidget() { + return (VNativeSelect) super.getWidget(); + } + + @Override + public void setDataSource(DataSource dataSource) { + if (dataChangeRegistration != null) { + dataChangeRegistration.remove(); + } + dataChangeRegistration = dataSource.addDataChangeHandler( + this::onDataChange); + super.setDataSource(dataSource); + } + + @OnStateChange("readOnly") + @SuppressWarnings("deprecation") + void updateWidgetReadOnly() { + getWidget().setEnabled(isEnabled() && !isReadOnly()); + } + + /** + * A data change handler registered to the data source. Updates the data + * items and selection status when the data source notifies of new changes + * from the server side. + * + * @param range + * the new range of data items + */ + private void onDataChange(Range range) { + assert range.getStart() == 0 && range.getEnd() == getDataSource() + .size() : "NativeSelect only supports full updates, but got range " + + range; + + final VNativeSelect select = getWidget(); + final int itemCount = select.getItemCount(); + + for (int i = range.getStart(); i < range.getEnd(); i++) { + + final JsonObject row = getDataSource().getRow(i); + + if (i < itemCount) { + // Reuse and update an existing item + select.setItemText(i, getRowData(row).asString()); + select.setValue(i, getRowKey(row)); + } else { + // Add new items if the new dataset is larger than the old + select.addItem(getRowData(row).asString(), getRowKey(row)); + } + } + + for (int i = select.getItemCount() - 1; i >= range.getEnd(); i--) { + // Remove extra items if the new dataset is smaller than the old + select.removeItem(i); + } + } +} diff --git a/server/src/main/java/com/vaadin/ui/NativeSelect.java b/server/src/main/java/com/vaadin/ui/NativeSelect.java new file mode 100644 index 0000000000..9a400a1a01 --- /dev/null +++ b/server/src/main/java/com/vaadin/ui/NativeSelect.java @@ -0,0 +1,114 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +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; + +/** + * A simple drop-down select component. Represented on the client side by a + * "native" HTML {@code