diff options
author | Pekka Hyvönen <pekka@vaadin.com> | 2016-09-27 12:26:15 +0300 |
---|---|---|
committer | Denis Anisimov <denis@vaadin.com> | 2016-09-28 12:59:52 +0000 |
commit | c4b17ca879c0bb51c7da390fcbd25c96b73486fb (patch) | |
tree | 524833ebab1553534e3d4ff01cd0fa5cf58db3a7 /server | |
parent | 96119ab0224421d69393d9d41ed02900121a683e (diff) | |
download | vaadin-framework-c4b17ca879c0bb51c7da390fcbd25c96b73486fb.tar.gz vaadin-framework-c4b17ca879c0bb51c7da390fcbd25c96b73486fb.zip |
ListSelect with new data binding API8.0.0.alpha3
No single select mode for list select.
Based on AbstractMultiSelect.
Removes feature for adding new items.
Change-Id: I9a92aaf1f3d338a3b05c3aa4048f9db496dacd1d
Diffstat (limited to 'server')
3 files changed, 204 insertions, 1 deletions
diff --git a/server/src/main/java/com/vaadin/ui/ListSelect.java b/server/src/main/java/com/vaadin/ui/ListSelect.java new file mode 100644 index 0000000000..7bbcbb2e8b --- /dev/null +++ b/server/src/main/java/com/vaadin/ui/ListSelect.java @@ -0,0 +1,124 @@ +/* + * 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 com.vaadin.server.data.DataSource; +import com.vaadin.shared.ui.listselect.ListSelectState; + +/** + * This is a simple list select without, for instance, support for new items, + * lazyloading, and other advanced features. + * + * @author Vaadin Ltd + * + * @param <T> + * item type + */ +public class ListSelect<T> extends AbstractMultiSelect<T> { + + /** Default number of rows visible for select. */ + // protected to allow javadoc linking + protected static final int DEFAULT_ROWS = 10; + + /** + * Constructs a new ListSelect. + */ + public ListSelect() { + setRows(DEFAULT_ROWS); + } + + /** + * Constructs a new ListSelect with the given caption. + * + * @param caption + * the caption to set, can be {@code null} + */ + public ListSelect(String caption) { + this(); + setCaption(caption); + } + + /** + * Constructs a new ListSelect with caption and data source for options. + * + * @param caption + * the caption to set, can be {@code null} + * @param dataSource + * the data source, not {@code null} + */ + public ListSelect(String caption, DataSource<T> dataSource) { + this(caption); + setDataSource(dataSource); + } + + /** + * Constructs a new ListSelect with caption and the given options. + * + * @param caption + * the caption to set, can be {@code null} + * @param options + * the options, cannot be {@code null} + */ + public ListSelect(String caption, Collection<T> options) { + this(caption, DataSource.create(options)); + } + + /** + * Returns the number of rows in the select. + * <p> + * Default value is {@link #DEFAULT_ROWS} + * + * @return the number of rows visible + */ + public int getRows() { + return getState(false).rows; + } + + /** + * Sets the number of rows in the select. If the number of rows is set to 0, + * the actual number of displayed rows is determined implicitly by the + * select. + * <p> + * If a height if set (using {@link #setHeight(String)} or + * {@link #setHeight(float, int)}) it overrides the number of rows. Leave + * the height undefined to use this method. + * <p> + * Default value is {@link #DEFAULT_ROWS} + * + * @param rows + * the number of rows to set. + */ + public void setRows(int rows) { + if (rows < 0) { + rows = 0; + } + if (getState(false).rows != rows) { + getState().rows = rows; + } + } + + @Override + protected ListSelectState getState() { + return (ListSelectState) super.getState(); + } + + @Override + protected ListSelectState getState(boolean markAsDirty) { + return (ListSelectState) super.getState(markAsDirty); + } +} diff --git a/server/src/test/java/com/vaadin/tests/server/component/listselect/ListSelectDeclarativeTest.java b/server/src/test/java/com/vaadin/tests/server/component/listselect/ListSelectDeclarativeTest.java new file mode 100644 index 0000000000..a3079bcdc3 --- /dev/null +++ b/server/src/test/java/com/vaadin/tests/server/component/listselect/ListSelectDeclarativeTest.java @@ -0,0 +1,73 @@ +/* + * 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.tests.server.component.listselect; + +import java.util.Arrays; + +import org.junit.Test; + +import com.vaadin.tests.design.DeclarativeTestBase; +import com.vaadin.ui.ListSelect; + +public class ListSelectDeclarativeTest + extends DeclarativeTestBase<ListSelect<String>> { + + private ListSelect<String> getWithOptionsExpected() { + ListSelect<String> ls = new ListSelect<>(null, + Arrays.asList("Male", "Female")); + ls.setRows(9); // 10 is default + return ls; + } + + private String getWithOptionsDesign() { + return "<vaadin-list-select rows=9>\n" + + " <option>Male</option>\n" + + " <option>Female</option>\n" + + "</vaadin-list-select>\n" + ""; + } + + @Test + public void testReadWithOptions() { + testRead(getWithOptionsDesign(), getWithOptionsExpected()); + } + + @Test + public void testWriteWithOptions() { + testWrite(stripOptionTags(getWithOptionsDesign()), + getWithOptionsExpected()); + } + + private ListSelect<String> getBasicExpected() { + ListSelect<String> ls = new ListSelect<>(); + ls.setCaption("Hello"); + return ls; + } + + private String getBasicDesign() { + return "<vaadin-list-select caption='Hello' />"; + } + + @Test + public void testReadBasic() { + testRead(getBasicDesign(), getBasicExpected()); + } + + @Test + public void testWriteBasic() { + testWrite(getBasicDesign(), getBasicExpected()); + } + +} diff --git a/server/src/test/java/com/vaadin/ui/AbstractMultiSelectTest.java b/server/src/test/java/com/vaadin/ui/AbstractMultiSelectTest.java index 3505e4ec38..d10cee9157 100644 --- a/server/src/test/java/com/vaadin/ui/AbstractMultiSelectTest.java +++ b/server/src/test/java/com/vaadin/ui/AbstractMultiSelectTest.java @@ -41,7 +41,8 @@ public class AbstractMultiSelectTest { @Parameters(name = "{0}") public static Iterable<AbstractMultiSelect<String>> multiSelects() { - return Arrays.asList(new CheckBoxGroup<>(), new TwinColSelect<>()); + return Arrays.asList(new CheckBoxGroup<>(), new TwinColSelect<>(), + new ListSelect<>()); } @Parameter @@ -90,6 +91,11 @@ public class AbstractMultiSelectTest { selectionModel.deselectItems("2", "1", "4", "5"); assertSelectionOrder(selectionModel, "3", "7", "8"); + + selectionModel.updateSelection( + new LinkedHashSet<>(Arrays.asList("5", "2")), + new LinkedHashSet<>(Arrays.asList("3", "8"))); + assertSelectionOrder(selectionModel, "7", "5", "2"); } @Test |