diff options
author | Johannes Dahlström <johannesd@vaadin.com> | 2016-09-08 22:16:08 +0300 |
---|---|---|
committer | Ilia Motornyi <elmot@vaadin.com> | 2016-09-12 09:05:41 +0000 |
commit | f5104e34f3167fa2bf05e93272f5b71c15d00071 (patch) | |
tree | eacc80ae807333e764239dd19e468bdafe144ca6 /client | |
parent | 0b4ef8246ba2f026a1c1e1d7264ba2a2fcd9c0fa (diff) | |
download | vaadin-framework-f5104e34f3167fa2bf05e93272f5b71c15d00071.tar.gz vaadin-framework-f5104e34f3167fa2bf05e93272f5b71c15d00071.zip |
Update NativeSelect to use DataSource, extend AbstractListing
Selection and focus/blur support not yet implemented.
Change-Id: I76752084442216e60055d93367475c1c0a612787
Diffstat (limited to 'client')
-rw-r--r-- | client/src/main/java/com/vaadin/client/ui/VNativeSelect.java | 34 | ||||
-rw-r--r-- | client/src/main/java/com/vaadin/client/ui/nativeselect/NativeSelectConnector.java | 102 |
2 files changed, 136 insertions, 0 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 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<SelectionModel<?>> { + + private Registration dataChangeRegistration; + + @Override + public VNativeSelect getWidget() { + return (VNativeSelect) super.getWidget(); + } + + @Override + public void setDataSource(DataSource<JsonObject> 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); + } + } +} |