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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import java.util.ArrayList;
  6. import java.util.Iterator;
  7. import com.google.gwt.event.dom.client.ChangeEvent;
  8. import com.vaadin.terminal.gwt.client.BrowserInfo;
  9. import com.vaadin.terminal.gwt.client.UIDL;
  10. import com.vaadin.terminal.gwt.client.Util;
  11. public class VNativeSelect extends VOptionGroupBase implements Field {
  12. public static final String CLASSNAME = "v-select";
  13. protected TooltipListBox select;
  14. public VNativeSelect() {
  15. super(new TooltipListBox(false), CLASSNAME);
  16. select = (TooltipListBox) optionsContainer;
  17. select.setSelect(this);
  18. select.setVisibleItemCount(1);
  19. select.addChangeHandler(this);
  20. select.setStyleName(CLASSNAME + "-select");
  21. }
  22. @Override
  23. protected void buildOptions(UIDL uidl) {
  24. select.setClient(client);
  25. select.setEnabled(!isDisabled() && !isReadonly());
  26. select.clear();
  27. if (isNullSelectionAllowed() && !isNullSelectionItemAvailable()) {
  28. // can't unselect last item in singleselect mode
  29. select.addItem("", null);
  30. }
  31. boolean selected = false;
  32. for (final Iterator i = uidl.getChildIterator(); i.hasNext();) {
  33. final UIDL optionUidl = (UIDL) i.next();
  34. select.addItem(optionUidl.getStringAttribute("caption"),
  35. optionUidl.getStringAttribute("key"));
  36. if (optionUidl.hasAttribute("selected")) {
  37. select.setItemSelected(select.getItemCount() - 1, true);
  38. selected = true;
  39. }
  40. }
  41. if (!selected && !isNullSelectionAllowed()) {
  42. // null-select not allowed, but value not selected yet; add null and
  43. // remove when something is selected
  44. select.insertItem("", null, 0);
  45. select.setItemSelected(0, true);
  46. }
  47. if (BrowserInfo.get().isIE6()) {
  48. // lazy size change - IE6 uses naive dropdown that does not have a
  49. // proper size yet
  50. Util.notifyParentOfSizeChange(this, true);
  51. }
  52. }
  53. @Override
  54. protected String[] getSelectedItems() {
  55. final ArrayList<String> selectedItemKeys = new ArrayList<String>();
  56. for (int i = 0; i < select.getItemCount(); i++) {
  57. if (select.isItemSelected(i)) {
  58. selectedItemKeys.add(select.getValue(i));
  59. }
  60. }
  61. return selectedItemKeys.toArray(new String[selectedItemKeys.size()]);
  62. }
  63. @Override
  64. public void onChange(ChangeEvent event) {
  65. if (select.isMultipleSelect()) {
  66. client.updateVariable(id, "selected", getSelectedItems(),
  67. isImmediate());
  68. } else {
  69. client.updateVariable(id, "selected", new String[] { ""
  70. + getSelectedItem() }, isImmediate());
  71. }
  72. if (!isNullSelectionAllowed() && "null".equals(select.getValue(0))) {
  73. // remove temporary empty item
  74. select.removeItem(0);
  75. }
  76. }
  77. @Override
  78. public void setHeight(String height) {
  79. select.setHeight(height);
  80. super.setHeight(height);
  81. }
  82. @Override
  83. public void setWidth(String width) {
  84. select.setWidth(width);
  85. super.setWidth(width);
  86. }
  87. @Override
  88. protected void setTabIndex(int tabIndex) {
  89. ((TooltipListBox) optionsContainer).setTabIndex(tabIndex);
  90. }
  91. public void focus() {
  92. select.setFocus(true);
  93. }
  94. }