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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.v7.client.ui;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import com.google.gwt.event.dom.client.ChangeEvent;
  20. import com.google.gwt.user.client.ui.ListBox;
  21. import com.vaadin.client.BrowserInfo;
  22. import com.vaadin.client.UIDL;
  23. public class VNativeSelect extends VOptionGroupBase {
  24. public static final String CLASSNAME = "v-select";
  25. protected ListBox select;
  26. private boolean firstValueIsTemporaryNullItem = false;
  27. public VNativeSelect() {
  28. super(new ListBox(false), CLASSNAME);
  29. select = getOptionsContainer();
  30. select.setVisibleItemCount(1);
  31. select.addChangeHandler(this);
  32. select.setStyleName(CLASSNAME + "-select");
  33. updateEnabledState();
  34. }
  35. protected ListBox getOptionsContainer() {
  36. return (ListBox) optionsContainer;
  37. }
  38. @Override
  39. public void buildOptions(UIDL uidl) {
  40. select.clear();
  41. firstValueIsTemporaryNullItem = false;
  42. if (isNullSelectionAllowed() && !isNullSelectionItemAvailable()) {
  43. // can't unselect last item in singleselect mode
  44. select.addItem("", (String) null);
  45. }
  46. boolean selected = false;
  47. for (final Object child : uidl) {
  48. final UIDL optionUidl = (UIDL) child;
  49. select.addItem(optionUidl.getStringAttribute("caption"),
  50. optionUidl.getStringAttribute("key"));
  51. if (optionUidl.hasAttribute("selected")) {
  52. select.setItemSelected(select.getItemCount() - 1, true);
  53. selected = true;
  54. }
  55. }
  56. if (!selected && !isNullSelectionAllowed()) {
  57. // null-select not allowed, but value not selected yet; add null and
  58. // remove when something is selected
  59. select.insertItem("", (String) null, 0);
  60. select.setItemSelected(0, true);
  61. firstValueIsTemporaryNullItem = true;
  62. }
  63. }
  64. @Override
  65. protected String[] getSelectedItems() {
  66. final List<String> selectedItemKeys = new ArrayList<String>();
  67. for (int i = 0; i < select.getItemCount(); i++) {
  68. if (select.isItemSelected(i)) {
  69. selectedItemKeys.add(select.getValue(i));
  70. }
  71. }
  72. return selectedItemKeys.toArray(new String[selectedItemKeys.size()]);
  73. }
  74. @Override
  75. public void onChange(ChangeEvent event) {
  76. if (select.isMultipleSelect()) {
  77. client.updateVariable(paintableId, "selected", getSelectedItems(),
  78. isImmediate());
  79. } else {
  80. client.updateVariable(paintableId, "selected",
  81. new String[] { "" + getSelectedItem() }, isImmediate());
  82. }
  83. if (firstValueIsTemporaryNullItem) {
  84. // remove temporary empty item
  85. select.removeItem(0);
  86. firstValueIsTemporaryNullItem = false;
  87. /*
  88. * Workaround to achrome bug that may cause value change event not
  89. * to fire when selection is done with keyboard.
  90. *
  91. * http://dev.vaadin.com/ticket/10109
  92. *
  93. * Problem is confirmed to exist only on Chrome-Win, but just
  94. * execute in for all webkits. Probably exists also in other
  95. * webkits/blinks on windows.
  96. */
  97. if (BrowserInfo.get().isWebkit()) {
  98. select.getElement().blur();
  99. select.getElement().focus();
  100. }
  101. }
  102. }
  103. @Override
  104. public void setHeight(String height) {
  105. select.setHeight(height);
  106. super.setHeight(height);
  107. }
  108. @Override
  109. public void setWidth(String width) {
  110. select.setWidth(width);
  111. super.setWidth(width);
  112. }
  113. @Override
  114. public void setTabIndex(int tabIndex) {
  115. getOptionsContainer().setTabIndex(tabIndex);
  116. }
  117. @Override
  118. protected void updateEnabledState() {
  119. select.setEnabled(isEnabled() && !isReadonly());
  120. }
  121. @Override
  122. public void focus() {
  123. select.setFocus(true);
  124. }
  125. /**
  126. * @return the root select widget
  127. */
  128. public ListBox getSelect() {
  129. return getOptionsContainer();
  130. }
  131. }