Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ListSelect.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.ui;
  17. import java.util.Collection;
  18. import com.vaadin.data.HasDataProvider;
  19. import com.vaadin.data.provider.DataProvider;
  20. import com.vaadin.shared.ui.listselect.ListSelectState;
  21. /**
  22. * This is a simple list select without, for instance, support for new items,
  23. * lazyloading, and other advanced features.
  24. *
  25. * @author Vaadin Ltd
  26. *
  27. * @param <T>
  28. * item type
  29. */
  30. public class ListSelect<T> extends AbstractMultiSelect<T>
  31. implements HasDataProvider<T> {
  32. /** Default number of rows visible for select. */
  33. // protected to allow javadoc linking
  34. protected static final int DEFAULT_ROWS = 10;
  35. /**
  36. * Constructs a new ListSelect.
  37. */
  38. public ListSelect() {
  39. setRows(DEFAULT_ROWS);
  40. }
  41. /**
  42. * Constructs a new ListSelect with the given caption.
  43. *
  44. * @param caption
  45. * the caption to set, can be {@code null}
  46. */
  47. public ListSelect(String caption) {
  48. this();
  49. setCaption(caption);
  50. }
  51. /**
  52. * Constructs a new ListSelect with caption and data provider for options.
  53. *
  54. * @param caption
  55. * the caption to set, can be {@code null}
  56. * @param dataProvider
  57. * the data provider, not {@code null}
  58. * @since 8.0
  59. */
  60. public ListSelect(String caption, DataProvider<T, ?> dataProvider) {
  61. this(caption);
  62. setDataProvider(dataProvider);
  63. }
  64. /**
  65. * Constructs a new ListSelect with caption and the given options.
  66. *
  67. * @param caption
  68. * the caption to set, can be {@code null}
  69. * @param options
  70. * the options, cannot be {@code null}
  71. */
  72. public ListSelect(String caption, Collection<T> options) {
  73. this(caption, DataProvider.ofCollection(options));
  74. }
  75. /**
  76. * Returns the number of rows in the select.
  77. * <p>
  78. * Default value is {@link #DEFAULT_ROWS}
  79. *
  80. * @return the number of rows visible
  81. */
  82. public int getRows() {
  83. return getState(false).rows;
  84. }
  85. /**
  86. * Sets the number of rows in the select. If the number of rows is set to 0,
  87. * the actual number of displayed rows is determined implicitly by the
  88. * select.
  89. * <p>
  90. * If a height if set (using {@link #setHeight(String)} or
  91. * {@link #setHeight(float, Unit)}) it overrides the number of rows. Leave
  92. * the height undefined to use this method.
  93. * <p>
  94. * Default value is {@link #DEFAULT_ROWS}
  95. *
  96. * @param rows
  97. * the number of rows to set.
  98. */
  99. public void setRows(int rows) {
  100. if (rows < 0) {
  101. rows = 0;
  102. }
  103. if (getState(false).rows != rows) {
  104. getState().rows = rows;
  105. }
  106. }
  107. @Override
  108. protected ListSelectState getState() {
  109. return (ListSelectState) super.getState();
  110. }
  111. @Override
  112. protected ListSelectState getState(boolean markAsDirty) {
  113. return (ListSelectState) super.getState(markAsDirty);
  114. }
  115. @Override
  116. public DataProvider<T, ?> getDataProvider() {
  117. return internalGetDataProvider();
  118. }
  119. @Override
  120. public void setDataProvider(DataProvider<T, ?> dataProvider) {
  121. internalSetDataProvider(dataProvider);
  122. }
  123. }