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.

ITwinColSelect.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package com.itmill.toolkit.terminal.gwt.client.ui;
  2. import java.util.Iterator;
  3. import java.util.Vector;
  4. import com.google.gwt.user.client.ui.FlowPanel;
  5. import com.google.gwt.user.client.ui.HTML;
  6. import com.google.gwt.user.client.ui.ListBox;
  7. import com.google.gwt.user.client.ui.Panel;
  8. import com.google.gwt.user.client.ui.Widget;
  9. import com.itmill.toolkit.terminal.gwt.client.UIDL;
  10. public class ITwinColSelect extends IOptionGroupBase {
  11. private static final String CLASSNAME = "i-select-twincol";
  12. private static final int VISIBLE_COUNT = 10;
  13. private ListBox options;
  14. private ListBox selections;
  15. private IButton add;
  16. private IButton remove;
  17. public ITwinColSelect() {
  18. super(CLASSNAME);
  19. options = new ListBox();
  20. selections = new ListBox();
  21. options.setVisibleItemCount(VISIBLE_COUNT);
  22. selections.setVisibleItemCount(VISIBLE_COUNT);
  23. options.setStyleName(CLASSNAME + "-options");
  24. selections.setStyleName(CLASSNAME + "-selections");
  25. Panel buttons = new FlowPanel();
  26. buttons.setStyleName(CLASSNAME + "-buttons");
  27. add = new IButton();
  28. remove = new IButton();
  29. add.setText(">>");
  30. remove.setText("<<");
  31. add.addClickListener(this);
  32. remove.addClickListener(this);
  33. Panel p = ((Panel) optionsContainer);
  34. p.add(options);
  35. buttons.add(add);
  36. HTML br = new HTML("&nbsp;");
  37. br.setStyleName(CLASSNAME + "-deco");
  38. buttons.add(br);
  39. buttons.add(remove);
  40. p.add(buttons);
  41. p.add(selections);
  42. }
  43. protected void buildOptions(UIDL uidl) {
  44. boolean enabled = !disabled && !readonly;
  45. options.setMultipleSelect(multiselect);
  46. selections.setMultipleSelect(multiselect);
  47. options.setEnabled(enabled);
  48. selections.setEnabled(enabled);
  49. add.setEnabled(enabled);
  50. remove.setEnabled(enabled);
  51. options.clear();
  52. selections.clear();
  53. for (Iterator i = uidl.getChildIterator(); i.hasNext();) {
  54. UIDL optionUidl = (UIDL) i.next();
  55. if (optionUidl.hasAttribute("selected")) {
  56. selections.addItem(optionUidl.getStringAttribute("caption"),
  57. optionUidl.getStringAttribute("key"));
  58. } else
  59. options.addItem(optionUidl.getStringAttribute("caption"),
  60. optionUidl.getStringAttribute("key"));
  61. }
  62. }
  63. protected Object[] getSelectedItems() {
  64. Vector selectedItemKeys = new Vector();
  65. for (int i = 0; i < selections.getItemCount(); i++) {
  66. selectedItemKeys.add(selections.getValue(i));
  67. }
  68. return selectedItemKeys.toArray();
  69. }
  70. private boolean[] getItemsToAdd() {
  71. boolean[] selectedIndexes = new boolean[options.getItemCount()];
  72. for (int i = 0; i < options.getItemCount(); i++) {
  73. if (options.isItemSelected(i))
  74. selectedIndexes[i] = true;
  75. else
  76. selectedIndexes[i] = false;
  77. }
  78. return selectedIndexes;
  79. }
  80. private boolean[] getItemsToRemove() {
  81. boolean[] selectedIndexes = new boolean[selections.getItemCount()];
  82. for (int i = 0; i < selections.getItemCount(); i++) {
  83. if (selections.isItemSelected(i))
  84. selectedIndexes[i] = true;
  85. else
  86. selectedIndexes[i] = false;
  87. }
  88. return selectedIndexes;
  89. }
  90. public void onClick(Widget sender) {
  91. super.onClick(sender);
  92. if (sender == add) {
  93. boolean[] sel = getItemsToAdd();
  94. for (int i = 0; i < sel.length; i++) {
  95. if (sel[i]) {
  96. int optionIndex = i - (sel.length - options.getItemCount());
  97. selectedKeys.add(options.getValue(optionIndex));
  98. // Move selection to another column
  99. String text = options.getItemText(optionIndex);
  100. String value = options.getValue(optionIndex);
  101. selections.addItem(text, value);
  102. options.removeItem(optionIndex);
  103. }
  104. }
  105. client.updateVariable(id, "selected", selectedKeys.toArray(),
  106. immediate);
  107. } else if (sender == remove) {
  108. boolean[] sel = getItemsToRemove();
  109. for (int i = 0; i < sel.length; i++) {
  110. if (sel[i]) {
  111. int selectionIndex = i
  112. - (sel.length - selections.getItemCount());
  113. selectedKeys.remove(selections.getValue(selectionIndex));
  114. // Move selection to another column
  115. String text = selections.getItemText(selectionIndex);
  116. String value = selections.getValue(selectionIndex);
  117. options.addItem(text, value);
  118. selections.removeItem(selectionIndex);
  119. }
  120. }
  121. client.updateVariable(id, "selected", selectedKeys.toArray(),
  122. immediate);
  123. }
  124. }
  125. }