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

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