Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

NativeSelect.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 java.util.Objects;
  19. import com.vaadin.data.HasDataProvider;
  20. import com.vaadin.data.provider.DataProvider;
  21. import com.vaadin.event.FieldEvents.BlurEvent;
  22. import com.vaadin.event.FieldEvents.BlurListener;
  23. import com.vaadin.event.FieldEvents.BlurNotifier;
  24. import com.vaadin.event.FieldEvents.FocusAndBlurServerRpcDecorator;
  25. import com.vaadin.event.FieldEvents.FocusEvent;
  26. import com.vaadin.event.FieldEvents.FocusListener;
  27. import com.vaadin.event.FieldEvents.FocusNotifier;
  28. import com.vaadin.shared.Registration;
  29. import com.vaadin.shared.data.DataCommunicatorConstants;
  30. import com.vaadin.shared.ui.nativeselect.NativeSelectState;
  31. /**
  32. * A simple drop-down select component. Represented on the client side by a
  33. * "native" HTML {@code <select>} element. Lacks advanced features such as lazy
  34. * loading, filtering, and adding new items.
  35. *
  36. * @author Vaadin Ltd.
  37. *
  38. * @param <T>
  39. * the data item type
  40. *
  41. * @see com.vaadin.ui.ComboBox
  42. */
  43. public class NativeSelect<T> extends AbstractSingleSelect<T>
  44. implements FocusNotifier, BlurNotifier, HasDataProvider<T> {
  45. /**
  46. * Creates a new {@code NativeSelect} with an empty caption and no items.
  47. */
  48. public NativeSelect() {
  49. registerRpc(new FocusAndBlurServerRpcDecorator(this, this::fireEvent));
  50. addDataGenerator((item, json) -> {
  51. String caption = getItemCaptionGenerator().apply(item);
  52. if (caption == null) {
  53. caption = "";
  54. }
  55. json.put(DataCommunicatorConstants.DATA, caption);
  56. });
  57. setItemCaptionGenerator(String::valueOf);
  58. }
  59. /**
  60. * Creates a new {@code NativeSelect} with the given caption and no items.
  61. *
  62. * @param caption
  63. * the component caption to set, null for no caption
  64. */
  65. public NativeSelect(String caption) {
  66. this();
  67. setCaption(caption);
  68. }
  69. /**
  70. * Creates a new {@code NativeSelect} with the given caption, containing the
  71. * data items in the given collection.
  72. *
  73. * @param caption
  74. * the component caption to set, null for no caption
  75. * @param items
  76. * the data items to use, not null
  77. */
  78. public NativeSelect(String caption, Collection<T> items) {
  79. this(caption);
  80. setItems(items);
  81. }
  82. /**
  83. * Creates a new {@code NativeSelect} with the given caption, using the
  84. * given {@code DataProvider} as the source of data items.
  85. *
  86. * @param caption
  87. * the component caption to set, null for no caption
  88. * @param dataProvider
  89. * the source of data items to use, not null
  90. */
  91. public NativeSelect(String caption, DataProvider<T, ?> dataProvider) {
  92. this(caption);
  93. setDataProvider(dataProvider);
  94. }
  95. @Override
  96. public Registration addFocusListener(FocusListener listener) {
  97. return addListener(FocusEvent.EVENT_ID, FocusEvent.class, listener,
  98. FocusListener.focusMethod);
  99. }
  100. @Override
  101. public Registration addBlurListener(BlurListener listener) {
  102. return addListener(BlurEvent.EVENT_ID, BlurEvent.class, listener,
  103. BlurListener.blurMethod);
  104. }
  105. @Override
  106. protected NativeSelectState getState() {
  107. return getState(true);
  108. }
  109. @Override
  110. protected NativeSelectState getState(boolean markAsDirty) {
  111. return (NativeSelectState) super.getState(markAsDirty);
  112. }
  113. @Override
  114. public DataProvider<T, ?> getDataProvider() {
  115. return internalGetDataProvider();
  116. }
  117. @Override
  118. public void setDataProvider(DataProvider<T, ?> dataProvider) {
  119. internalSetDataProvider(dataProvider);
  120. }
  121. @Override
  122. public void setItemCaptionGenerator(
  123. ItemCaptionGenerator<T> itemCaptionGenerator) {
  124. super.setItemCaptionGenerator(itemCaptionGenerator);
  125. }
  126. @Override
  127. public ItemCaptionGenerator<T> getItemCaptionGenerator() {
  128. return super.getItemCaptionGenerator();
  129. }
  130. /**
  131. * Returns whether the user is allowed to select nothing in the combo box.
  132. *
  133. * @return true if empty selection is allowed, false otherwise
  134. * @since 8.0
  135. */
  136. public boolean isEmptySelectionAllowed() {
  137. return getState(false).emptySelectionAllowed;
  138. }
  139. /**
  140. * Sets whether the user is allowed to select nothing in the combo box. When
  141. * true, a special empty item is shown to the user.
  142. *
  143. * @param emptySelectionAllowed
  144. * true to allow not selecting anything, false to require
  145. * selection
  146. * @since 8.0
  147. */
  148. public void setEmptySelectionAllowed(boolean emptySelectionAllowed) {
  149. getState().emptySelectionAllowed = emptySelectionAllowed;
  150. }
  151. /**
  152. * Returns the empty selection caption.
  153. * <p>
  154. * The empty string {@code ""} is the default empty selection caption.
  155. *
  156. * @see #setEmptySelectionAllowed(boolean)
  157. * @see #isEmptySelectionAllowed()
  158. * @see #setEmptySelectionCaption(String)
  159. * @see #isSelected(Object)
  160. *
  161. * @return the empty selection caption, not {@code null}
  162. * @since 8.0
  163. */
  164. public String getEmptySelectionCaption() {
  165. return getState(false).emptySelectionCaption;
  166. }
  167. /**
  168. * Sets the empty selection caption.
  169. * <p>
  170. * The empty string {@code ""} is the default empty selection caption.
  171. * <p>
  172. * If empty selection is allowed via the
  173. * {@link #setEmptySelectionAllowed(boolean)} method (it is by default) then
  174. * the empty item will be shown with the given caption.
  175. *
  176. * @param caption
  177. * the caption to set, not {@code null}
  178. * @see #isSelected(Object)
  179. * @since 8.0
  180. */
  181. public void setEmptySelectionCaption(String caption) {
  182. Objects.nonNull(caption);
  183. getState().emptySelectionCaption = caption;
  184. }
  185. /**
  186. * Sets the number of items that are visible. If only one item is visible,
  187. * then the box will be displayed as a drop-down list (the default).
  188. *
  189. * @since 8.1
  190. * @param visibleItemCount
  191. * the visible item count
  192. * @throws IllegalArgumentException
  193. * if the value is smaller than one
  194. */
  195. public void setVisibleItemCount(int visibleItemCount) {
  196. if (visibleItemCount < 1) {
  197. throw new IllegalArgumentException(
  198. "There must be at least one item visible");
  199. }
  200. getState().visibleItemCount = visibleItemCount;
  201. }
  202. /**
  203. * Gets the number of items that are visible. If only one item is visible,
  204. * then the box will be displayed as a drop-down list.
  205. *
  206. * @since 8.1
  207. * @return the visible item count
  208. */
  209. public int getVisibleItemCount() {
  210. return getState(false).visibleItemCount;
  211. }
  212. }