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.

VListSelect.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright 2000-2014 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.ArrayList;
  18. import java.util.Iterator;
  19. import com.google.gwt.event.dom.client.ChangeEvent;
  20. import com.google.gwt.user.client.ui.ListBox;
  21. import com.vaadin.client.UIDL;
  22. public class VListSelect extends VOptionGroupBase {
  23. public static final String CLASSNAME = "v-select";
  24. private static final int VISIBLE_COUNT = 10;
  25. protected ListBox select;
  26. private int lastSelectedIndex = -1;
  27. public VListSelect() {
  28. super(new ListBox(true), CLASSNAME);
  29. select = getOptionsContainer();
  30. select.addChangeHandler(this);
  31. select.addClickHandler(this);
  32. select.setVisibleItemCount(VISIBLE_COUNT);
  33. setStyleName(CLASSNAME);
  34. updateEnabledState();
  35. }
  36. @Override
  37. public void setStyleName(String style) {
  38. super.setStyleName(style);
  39. updateStyleNames();
  40. }
  41. @Override
  42. public void setStylePrimaryName(String style) {
  43. super.setStylePrimaryName(style);
  44. updateStyleNames();
  45. }
  46. protected void updateStyleNames() {
  47. container.setStyleName(getStylePrimaryName());
  48. select.setStyleName(getStylePrimaryName() + "-select");
  49. }
  50. protected ListBox getOptionsContainer() {
  51. return (ListBox) optionsContainer;
  52. }
  53. @Override
  54. public void buildOptions(UIDL uidl) {
  55. int scrollTop = select.getElement().getScrollTop();
  56. int rowCount = getRows();
  57. select.setMultipleSelect(isMultiselect());
  58. select.clear();
  59. if (!isMultiselect() && isNullSelectionAllowed()
  60. && !isNullSelectionItemAvailable()) {
  61. // can't unselect last item in singleselect mode
  62. select.addItem("", (String) null);
  63. }
  64. for (final Iterator<?> i = uidl.getChildIterator(); i.hasNext();) {
  65. final UIDL optionUidl = (UIDL) i.next();
  66. select.addItem(optionUidl.getStringAttribute("caption"),
  67. optionUidl.getStringAttribute("key"));
  68. if (optionUidl.hasAttribute("selected")) {
  69. int itemIndex = select.getItemCount() - 1;
  70. select.setItemSelected(itemIndex, true);
  71. lastSelectedIndex = itemIndex;
  72. }
  73. }
  74. if (getRows() > 0) {
  75. select.setVisibleItemCount(getRows());
  76. }
  77. // FIXME: temporary hack for preserving the scroll state when the
  78. // contents haven't been changed obviously. This should be dealt with in
  79. // the rewrite.
  80. if (rowCount == getRows()) {
  81. select.getElement().setScrollTop(scrollTop);
  82. }
  83. }
  84. @Override
  85. protected String[] getSelectedItems() {
  86. final ArrayList<String> selectedItemKeys = new ArrayList<String>();
  87. for (int i = 0; i < select.getItemCount(); i++) {
  88. if (select.isItemSelected(i)) {
  89. selectedItemKeys.add(select.getValue(i));
  90. }
  91. }
  92. return selectedItemKeys.toArray(new String[selectedItemKeys.size()]);
  93. }
  94. @Override
  95. public void onChange(ChangeEvent event) {
  96. final int si = select.getSelectedIndex();
  97. if (si == -1 && !isNullSelectionAllowed()) {
  98. select.setSelectedIndex(lastSelectedIndex);
  99. } else {
  100. lastSelectedIndex = si;
  101. if (isMultiselect()) {
  102. client.updateVariable(paintableId, "selected",
  103. getSelectedItems(), isImmediate());
  104. } else {
  105. client.updateVariable(paintableId, "selected",
  106. new String[] { "" + getSelectedItem() }, isImmediate());
  107. }
  108. }
  109. }
  110. @Override
  111. public void setHeight(String height) {
  112. select.setHeight(height);
  113. super.setHeight(height);
  114. }
  115. @Override
  116. public void setWidth(String width) {
  117. select.setWidth(width);
  118. super.setWidth(width);
  119. }
  120. @Override
  121. public void setTabIndex(int tabIndex) {
  122. getOptionsContainer().setTabIndex(tabIndex);
  123. }
  124. @Override
  125. protected void updateEnabledState() {
  126. select.setEnabled(isEnabled() && !isReadonly());
  127. }
  128. @Override
  129. public void focus() {
  130. select.setFocus(true);
  131. }
  132. }