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 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright 2000-2016 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.event.dom.client.HasAllFocusHandlers;
  19. import com.google.gwt.user.client.ui.ListBox;
  20. import com.vaadin.client.widgets.FocusableFlowPanelComposite;
  21. import com.vaadin.shared.ui.nativeselect.NativeSelectState;
  22. /**
  23. * The client-side widget for the {@code NativeSelect} component.
  24. *
  25. * @author Vaadin Ltd.
  26. */
  27. public class VNativeSelect extends FocusableFlowPanelComposite
  28. implements HasAllFocusHandlers {
  29. private final ListBox listBox = new ListBox();
  30. /**
  31. * Creates a new {@code VNativeSelect} instance.
  32. */
  33. public VNativeSelect() {
  34. setStyleName(NativeSelectState.STYLE_NAME);
  35. getListBox().setStyleName(NativeSelectState.STYLE_NAME + "-select");
  36. getWidget().add(listBox);
  37. }
  38. @Override
  39. public void setStylePrimaryName(String style) {
  40. super.setStylePrimaryName(style);
  41. getListBox().setStyleName(style + "-select");
  42. }
  43. /**
  44. * Sets the selected item by its value. If given {@code null}, removes
  45. * selection.
  46. *
  47. * @param value
  48. * the value of the item to select or {@code null} to select
  49. * nothing
  50. */
  51. public void setSelectedItem(String value) {
  52. if (value == null) {
  53. getListBox().setSelectedIndex(-1);
  54. } else {
  55. for (int i = 0; i < getListBox().getItemCount(); i++) {
  56. if (Objects.equals(value, getListBox().getValue(i))) {
  57. getListBox().setSelectedIndex(i);
  58. break;
  59. }
  60. }
  61. }
  62. }
  63. /**
  64. * Sets the tab index.
  65. *
  66. * @param tabIndex
  67. * the tab index to set
  68. */
  69. public void setTabIndex(int tabIndex) {
  70. getListBox().setTabIndex(tabIndex);
  71. }
  72. /**
  73. * Gets the underlying ListBox widget that this widget wraps.
  74. *
  75. * @return the ListBox this widget wraps
  76. */
  77. public ListBox getListBox() {
  78. return listBox;
  79. }
  80. @Override
  81. public void setWidth(String width) {
  82. if ("".equals(width)) {
  83. // undefined width
  84. getListBox().setWidth("");
  85. } else {
  86. // fill the composite
  87. getListBox().setWidth("100%");
  88. }
  89. super.setWidth(width);
  90. }
  91. @Override
  92. public void setHeight(String height) {
  93. if ("".equals(height)) {
  94. // undefined height
  95. getListBox().setHeight("");
  96. } else {
  97. // fill the composite
  98. getListBox().setHeight("100%");
  99. }
  100. super.setHeight(height);
  101. }
  102. }