You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VNativeSelect.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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.client.ui;
  17. import java.util.Objects;
  18. import com.google.gwt.user.client.ui.ListBox;
  19. import com.vaadin.client.widgets.FocusableFlowPanelComposite;
  20. import com.vaadin.shared.ui.nativeselect.NativeSelectState;
  21. /**
  22. * The client-side widget for the {@code NativeSelect} component.
  23. *
  24. * @author Vaadin Ltd.
  25. */
  26. public class VNativeSelect extends FocusableFlowPanelComposite {
  27. private final ListBox listBox = new ListBox();
  28. private boolean emptySelectionAllowed = true;
  29. /**
  30. * Creates a new {@code VNativeSelect} instance.
  31. */
  32. public VNativeSelect() {
  33. setStyleName(NativeSelectState.STYLE_NAME);
  34. getListBox().setStyleName(NativeSelectState.STYLE_NAME + "-select");
  35. getWidget().add(listBox);
  36. }
  37. @Override
  38. public void setStylePrimaryName(String style) {
  39. super.setStylePrimaryName(style);
  40. getListBox().setStyleName(style + "-select");
  41. }
  42. /**
  43. * Sets the selected item by its value. If given {@code null}, removes
  44. * selection.
  45. *
  46. * @param value
  47. * the value of the item to select or {@code null} to select
  48. * nothing
  49. */
  50. public void setSelectedItem(String value) {
  51. if (value == null) {
  52. if (emptySelectionAllowed) {
  53. getListBox().setSelectedIndex(0);
  54. } else {
  55. getListBox().setSelectedIndex(-1);
  56. }
  57. } else {
  58. for (int i = 0; i < getListBox().getItemCount(); i++) {
  59. if (Objects.equals(value, getListBox().getValue(i))) {
  60. getListBox().setSelectedIndex(i);
  61. break;
  62. }
  63. }
  64. }
  65. }
  66. /**
  67. * Sets the tab index.
  68. *
  69. * @param tabIndex
  70. * the tab index to set
  71. */
  72. public void setTabIndex(int tabIndex) {
  73. getListBox().setTabIndex(tabIndex);
  74. }
  75. /**
  76. * Gets the underlying ListBox widget that this widget wraps.
  77. *
  78. * @return the ListBox this widget wraps
  79. */
  80. public ListBox getListBox() {
  81. return listBox;
  82. }
  83. @Override
  84. public void setWidth(String width) {
  85. if ("".equals(width)) {
  86. // undefined width
  87. getListBox().setWidth("");
  88. } else {
  89. // fill the composite
  90. getListBox().setWidth("100%");
  91. }
  92. super.setWidth(width);
  93. }
  94. @Override
  95. public void setHeight(String height) {
  96. if ("".equals(height)) {
  97. // undefined height
  98. getListBox().setHeight("");
  99. } else {
  100. // fill the composite
  101. getListBox().setHeight("100%");
  102. }
  103. super.setHeight(height);
  104. }
  105. /**
  106. * Sets the number of items that are visible. If only one item is visible,
  107. * then the box will be displayed as a drop-down list (the default).
  108. *
  109. * @since 8.1
  110. * @param visibleItemCount
  111. * the visible item count
  112. */
  113. public void setVisibleItemCount(int visibleItemCount) {
  114. getListBox().setVisibleItemCount(visibleItemCount);
  115. }
  116. /**
  117. * Gets the number of items that are visible. If only one item is visible,
  118. * then the box will be displayed as a drop-down list.
  119. *
  120. * @since 8.1
  121. * @return the visible item count
  122. */
  123. public int getVisibleItemCount() {
  124. return getListBox().getVisibleItemCount();
  125. }
  126. /**
  127. * Returns true if empty selection is allowed.
  128. *
  129. * @since 8.7
  130. * @return empty selection is allowed
  131. */
  132. public boolean isEmptySelectionAllowed() {
  133. return emptySelectionAllowed;
  134. }
  135. /**
  136. * Sets true if empty selection is allowed.
  137. *
  138. * @since 8.7
  139. * @param emptySelectionAllowed
  140. */
  141. public void setEmptySelectionAllowed(boolean emptySelectionAllowed) {
  142. this.emptySelectionAllowed = emptySelectionAllowed;
  143. }
  144. }