Change-Id: I11dcd769dd81a90b41cc74707ef8d6ef4ddc9be7tags/8.0.0.alpha4^0
@@ -1,12 +1,12 @@ | |||
/* | |||
* 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 | |||
@@ -17,41 +17,55 @@ package com.vaadin.client.ui; | |||
import java.util.Objects; | |||
import com.google.gwt.user.client.ui.Composite; | |||
import com.google.gwt.user.client.ui.FlowPanel; | |||
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 { | |||
public class VNativeSelect extends Composite { | |||
private final ListBox listBox = new ListBox(); | |||
/** | |||
* Creates a new {@code VNativeSelect} instance. | |||
*/ | |||
public VNativeSelect() { | |||
setStyleName(NativeSelectState.STYLE_NAME); | |||
FlowPanel panel = new FlowPanel(); | |||
panel.add(listBox); | |||
initWidget(panel); | |||
} | |||
/** | |||
* 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); | |||
getListBox().setSelectedIndex(-1); | |||
} else { | |||
for (int i = 0; i < getItemCount(); i++) { | |||
if (Objects.equals(value, getValue(i))) { | |||
setSelectedIndex(i); | |||
for (int i = 0; i < getListBox().getItemCount(); i++) { | |||
if (Objects.equals(value, getListBox().getValue(i))) { | |||
getListBox().setSelectedIndex(i); | |||
break; | |||
} | |||
} | |||
} | |||
} | |||
/** | |||
* Gets the underlying ListBox widget that this widget wraps. | |||
* | |||
* @return the ListBox this widget wraps | |||
*/ | |||
public ListBox getListBox() { | |||
return listBox; | |||
} | |||
} |
@@ -32,12 +32,12 @@ 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) | |||
@@ -53,9 +53,11 @@ public class NativeSelectConnector | |||
@Override | |||
protected void init() { | |||
super.init(); | |||
selectionChangeRegistration = getWidget().addChangeHandler( | |||
e -> selectionRpc.select(getWidget().getSelectedValue())); | |||
getWidget().getListBox() | |||
.setStyleName(NativeSelectState.STYLE_NAME + "-select"); | |||
selectionChangeRegistration = getWidget().getListBox() | |||
.addChangeHandler(e -> selectionRpc | |||
.select(getWidget().getListBox().getSelectedValue())); | |||
} | |||
@Override | |||
@@ -83,7 +85,7 @@ public class NativeSelectConnector | |||
@OnStateChange("readOnly") | |||
@SuppressWarnings("deprecation") | |||
void updateWidgetReadOnly() { | |||
getWidget().setEnabled(isEnabled() && !isReadOnly()); | |||
getWidget().getListBox().setEnabled(isEnabled() && !isReadOnly()); | |||
} | |||
@OnStateChange("selectedItemKey") | |||
@@ -100,7 +102,7 @@ public class NativeSelectConnector | |||
* 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 | |||
*/ | |||
@@ -110,7 +112,7 @@ public class NativeSelectConnector | |||
+ range; | |||
final VNativeSelect select = getWidget(); | |||
final int itemCount = select.getItemCount(); | |||
final int itemCount = select.getListBox().getItemCount(); | |||
for (int i = range.getStart(); i < range.getEnd(); i++) { | |||
@@ -118,17 +120,19 @@ public class NativeSelectConnector | |||
if (i < itemCount) { | |||
// Reuse and update an existing item | |||
select.setItemText(i, getRowData(row).asString()); | |||
select.setValue(i, getRowKey(row)); | |||
select.getListBox().setItemText(i, getRowData(row).asString()); | |||
select.getListBox().setValue(i, getRowKey(row)); | |||
} else { | |||
// Add new items if the new dataset is larger than the old | |||
select.addItem(getRowData(row).asString(), getRowKey(row)); | |||
select.getListBox().addItem(getRowData(row).asString(), | |||
getRowKey(row)); | |||
} | |||
} | |||
for (int i = select.getItemCount() - 1; i >= range.getEnd(); i--) { | |||
for (int i = select.getListBox().getItemCount() - 1; i >= range | |||
.getEnd(); i--) { | |||
// Remove extra items if the new dataset is smaller than the old | |||
select.removeItem(i); | |||
select.getListBox().removeItem(i); | |||
} | |||
updateSelectedItem(); | |||
} |
@@ -1,12 +1,12 @@ | |||
/* | |||
* 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 | |||
@@ -29,7 +29,7 @@ public class NativeSelectState extends AbstractSingleSelectState { | |||
/** | |||
* The default primary style name for {@code NativeSelect}. | |||
*/ | |||
public static final String STYLE_NAME = "v-nativeselect"; | |||
public static final String STYLE_NAME = "v-select"; | |||
{ | |||
primaryStyleName = STYLE_NAME; |
@@ -31,12 +31,7 @@ public class NativeSelectElement extends AbstractSelectElement { | |||
@Override | |||
protected void init() { | |||
super.init(); | |||
// FIXME: Newer NativeSelect has different DOM structure. | |||
if (getTagName().equals("select")) { | |||
selectElement = new Select(this); | |||
} else { | |||
selectElement = new Select(findElement(By.tagName("select"))); | |||
} | |||
selectElement = new Select(findElement(By.tagName("select"))); | |||
} | |||
public List<TestBenchElement> getOptions() { |