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.

IListSelect.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.itmill.toolkit.terminal.gwt.client.ui;
  5. import java.util.Iterator;
  6. import java.util.Vector;
  7. import com.google.gwt.user.client.Event;
  8. import com.google.gwt.user.client.ui.ListBox;
  9. import com.google.gwt.user.client.ui.Widget;
  10. import com.itmill.toolkit.terminal.gwt.client.ApplicationConnection;
  11. import com.itmill.toolkit.terminal.gwt.client.Paintable;
  12. import com.itmill.toolkit.terminal.gwt.client.ITooltip;
  13. import com.itmill.toolkit.terminal.gwt.client.UIDL;
  14. public class IListSelect extends IOptionGroupBase {
  15. public static final String CLASSNAME = "i-select";
  16. private static final int VISIBLE_COUNT = 10;
  17. protected TooltipListBox select;
  18. private int lastSelectedIndex = -1;
  19. public IListSelect() {
  20. super(new TooltipListBox(true), CLASSNAME);
  21. select = (TooltipListBox) optionsContainer;
  22. select.setSelect(this);
  23. select.addChangeListener(this);
  24. select.addClickListener(this);
  25. select.setStyleName(CLASSNAME + "-select");
  26. select.setVisibleItemCount(VISIBLE_COUNT);
  27. }
  28. protected void buildOptions(UIDL uidl) {
  29. select.setClient(client);
  30. select.setMultipleSelect(isMultiselect());
  31. select.setEnabled(!isDisabled() && !isReadonly());
  32. select.clear();
  33. if (!isMultiselect() && isNullSelectionAllowed()
  34. && !isNullSelectionItemAvailable()) {
  35. // can't unselect last item in singleselect mode
  36. select.addItem("", null);
  37. }
  38. for (final Iterator i = uidl.getChildIterator(); i.hasNext();) {
  39. final UIDL optionUidl = (UIDL) i.next();
  40. select.addItem(optionUidl.getStringAttribute("caption"), optionUidl
  41. .getStringAttribute("key"));
  42. if (optionUidl.hasAttribute("selected")) {
  43. select.setItemSelected(select.getItemCount() - 1, true);
  44. }
  45. }
  46. if (getRows() > 0) {
  47. select.setVisibleItemCount(getRows());
  48. }
  49. }
  50. protected Object[] getSelectedItems() {
  51. final Vector selectedItemKeys = new Vector();
  52. for (int i = 0; i < select.getItemCount(); i++) {
  53. if (select.isItemSelected(i)) {
  54. selectedItemKeys.add(select.getValue(i));
  55. }
  56. }
  57. return selectedItemKeys.toArray();
  58. }
  59. public void onChange(Widget sender) {
  60. final int si = select.getSelectedIndex();
  61. if (si == -1 && !isNullSelectionAllowed()) {
  62. select.setSelectedIndex(lastSelectedIndex);
  63. } else {
  64. lastSelectedIndex = si;
  65. if (isMultiselect()) {
  66. client.updateVariable(id, "selected", getSelectedItems(),
  67. isImmediate());
  68. } else {
  69. client.updateVariable(id, "selected", new String[] { ""
  70. + getSelectedItem() }, isImmediate());
  71. }
  72. }
  73. }
  74. public void setHeight(String height) {
  75. select.setHeight(height);
  76. super.setHeight(height);
  77. }
  78. public void setWidth(String width) {
  79. select.setWidth(width);
  80. super.setWidth(width);
  81. }
  82. protected void setTabIndex(int tabIndex) {
  83. ((TooltipListBox) optionsContainer).setTabIndex(tabIndex);
  84. }
  85. }
  86. /**
  87. * Extended ListBox to listen tooltip events and forward them to generic
  88. * handler.
  89. */
  90. class TooltipListBox extends ListBox {
  91. private ApplicationConnection client;
  92. private Paintable pntbl;
  93. TooltipListBox(boolean isMultiselect) {
  94. super(isMultiselect);
  95. sinkEvents(ITooltip.TOOLTIP_EVENTS);
  96. }
  97. public void setClient(ApplicationConnection client) {
  98. this.client = client;
  99. }
  100. public void setSelect(Paintable s) {
  101. pntbl = s;
  102. }
  103. public void onBrowserEvent(Event event) {
  104. super.onBrowserEvent(event);
  105. if (client != null) {
  106. client.handleTooltipEvent(event, pntbl);
  107. }
  108. }
  109. }