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.

VTwinColSelect.java 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import java.util.ArrayList;
  6. import java.util.HashSet;
  7. import java.util.Iterator;
  8. import java.util.Set;
  9. import com.google.gwt.dom.client.Style.Overflow;
  10. import com.google.gwt.event.dom.client.ClickEvent;
  11. import com.google.gwt.event.dom.client.DoubleClickEvent;
  12. import com.google.gwt.event.dom.client.DoubleClickHandler;
  13. import com.google.gwt.event.dom.client.HasDoubleClickHandlers;
  14. import com.google.gwt.event.dom.client.KeyCodes;
  15. import com.google.gwt.event.dom.client.KeyDownEvent;
  16. import com.google.gwt.event.dom.client.KeyDownHandler;
  17. import com.google.gwt.event.dom.client.MouseDownEvent;
  18. import com.google.gwt.event.dom.client.MouseDownHandler;
  19. import com.google.gwt.event.shared.HandlerRegistration;
  20. import com.google.gwt.user.client.DOM;
  21. import com.google.gwt.user.client.Element;
  22. import com.google.gwt.user.client.ui.FlowPanel;
  23. import com.google.gwt.user.client.ui.HTML;
  24. import com.google.gwt.user.client.ui.ListBox;
  25. import com.google.gwt.user.client.ui.Panel;
  26. import com.google.gwt.user.client.ui.Widget;
  27. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  28. import com.vaadin.terminal.gwt.client.UIDL;
  29. import com.vaadin.terminal.gwt.client.Util;
  30. public class VTwinColSelect extends VOptionGroupBase implements KeyDownHandler,
  31. MouseDownHandler, DoubleClickHandler, SubPartAware {
  32. private static final String CLASSNAME = "v-select-twincol";
  33. public static final String ATTRIBUTE_LEFT_CAPTION = "lc";
  34. public static final String ATTRIBUTE_RIGHT_CAPTION = "rc";
  35. private static final int VISIBLE_COUNT = 10;
  36. private static final int DEFAULT_COLUMN_COUNT = 10;
  37. private final DoubleClickListBox options;
  38. private final DoubleClickListBox selections;
  39. private FlowPanel captionWrapper;
  40. private HTML optionsCaption = null;
  41. private HTML selectionsCaption = null;
  42. private final VButton add;
  43. private final VButton remove;
  44. private final FlowPanel buttons;
  45. private final Panel panel;
  46. private boolean widthSet = false;
  47. /**
  48. * A ListBox which catches double clicks
  49. *
  50. */
  51. public class DoubleClickListBox extends ListBox implements
  52. HasDoubleClickHandlers {
  53. public DoubleClickListBox(boolean isMultipleSelect) {
  54. super(isMultipleSelect);
  55. }
  56. public DoubleClickListBox() {
  57. super();
  58. }
  59. @Override
  60. public HandlerRegistration addDoubleClickHandler(
  61. DoubleClickHandler handler) {
  62. return addDomHandler(handler, DoubleClickEvent.getType());
  63. }
  64. }
  65. public VTwinColSelect() {
  66. super(CLASSNAME);
  67. captionWrapper = new FlowPanel();
  68. options = new DoubleClickListBox();
  69. options.addClickHandler(this);
  70. options.addDoubleClickHandler(this);
  71. options.setVisibleItemCount(VISIBLE_COUNT);
  72. options.setStyleName(CLASSNAME + "-options");
  73. selections = new DoubleClickListBox();
  74. selections.addClickHandler(this);
  75. selections.addDoubleClickHandler(this);
  76. selections.setVisibleItemCount(VISIBLE_COUNT);
  77. selections.setStyleName(CLASSNAME + "-selections");
  78. buttons = new FlowPanel();
  79. buttons.setStyleName(CLASSNAME + "-buttons");
  80. add = new VButton();
  81. add.setText(">>");
  82. add.addClickHandler(this);
  83. remove = new VButton();
  84. remove.setText("<<");
  85. remove.addClickHandler(this);
  86. panel = ((Panel) optionsContainer);
  87. panel.add(captionWrapper);
  88. captionWrapper.getElement().getStyle().setOverflow(Overflow.HIDDEN);
  89. // Hide until there actually is a caption to prevent IE from rendering
  90. // extra empty space
  91. captionWrapper.setVisible(false);
  92. panel.add(options);
  93. buttons.add(add);
  94. final HTML br = new HTML("<span/>");
  95. br.setStyleName(CLASSNAME + "-deco");
  96. buttons.add(br);
  97. buttons.add(remove);
  98. panel.add(buttons);
  99. panel.add(selections);
  100. options.addKeyDownHandler(this);
  101. options.addMouseDownHandler(this);
  102. selections.addMouseDownHandler(this);
  103. selections.addKeyDownHandler(this);
  104. }
  105. public HTML getOptionsCaption() {
  106. if (optionsCaption == null) {
  107. optionsCaption = new HTML();
  108. optionsCaption.setStyleName(CLASSNAME + "-caption-left");
  109. optionsCaption.getElement().getStyle()
  110. .setFloat(com.google.gwt.dom.client.Style.Float.LEFT);
  111. captionWrapper.add(optionsCaption);
  112. }
  113. return optionsCaption;
  114. }
  115. public HTML getSelectionsCaption() {
  116. if (selectionsCaption == null) {
  117. selectionsCaption = new HTML();
  118. selectionsCaption.setStyleName(CLASSNAME + "-caption-right");
  119. selectionsCaption.getElement().getStyle()
  120. .setFloat(com.google.gwt.dom.client.Style.Float.RIGHT);
  121. captionWrapper.add(selectionsCaption);
  122. }
  123. return selectionsCaption;
  124. }
  125. @Override
  126. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  127. // Captions are updated before super call to ensure the widths are set
  128. // correctly
  129. if (!uidl.getBooleanAttribute("cached")) {
  130. updateCaptions(uidl);
  131. }
  132. super.updateFromUIDL(uidl, client);
  133. }
  134. private void updateCaptions(UIDL uidl) {
  135. String leftCaption = (uidl.hasAttribute(ATTRIBUTE_LEFT_CAPTION) ? uidl
  136. .getStringAttribute(ATTRIBUTE_LEFT_CAPTION) : null);
  137. String rightCaption = (uidl.hasAttribute(ATTRIBUTE_RIGHT_CAPTION) ? uidl
  138. .getStringAttribute(ATTRIBUTE_RIGHT_CAPTION) : null);
  139. boolean hasCaptions = (leftCaption != null || rightCaption != null);
  140. if (leftCaption == null) {
  141. removeOptionsCaption();
  142. } else {
  143. getOptionsCaption().setText(leftCaption);
  144. }
  145. if (rightCaption == null) {
  146. removeSelectionsCaption();
  147. } else {
  148. getSelectionsCaption().setText(rightCaption);
  149. }
  150. captionWrapper.setVisible(hasCaptions);
  151. }
  152. private void removeOptionsCaption() {
  153. if (optionsCaption == null) {
  154. return;
  155. }
  156. if (optionsCaption.getParent() != null) {
  157. captionWrapper.remove(optionsCaption);
  158. }
  159. optionsCaption = null;
  160. }
  161. private void removeSelectionsCaption() {
  162. if (selectionsCaption == null) {
  163. return;
  164. }
  165. if (selectionsCaption.getParent() != null) {
  166. captionWrapper.remove(selectionsCaption);
  167. }
  168. selectionsCaption = null;
  169. }
  170. @Override
  171. protected void buildOptions(UIDL uidl) {
  172. final boolean enabled = !isDisabled() && !isReadonly();
  173. options.setMultipleSelect(isMultiselect());
  174. selections.setMultipleSelect(isMultiselect());
  175. options.setEnabled(enabled);
  176. selections.setEnabled(enabled);
  177. add.setEnabled(enabled);
  178. remove.setEnabled(enabled);
  179. options.clear();
  180. selections.clear();
  181. for (final Iterator<?> i = uidl.getChildIterator(); i.hasNext();) {
  182. final UIDL optionUidl = (UIDL) i.next();
  183. if (optionUidl.hasAttribute("selected")) {
  184. selections.addItem(optionUidl.getStringAttribute("caption"),
  185. optionUidl.getStringAttribute("key"));
  186. } else {
  187. options.addItem(optionUidl.getStringAttribute("caption"),
  188. optionUidl.getStringAttribute("key"));
  189. }
  190. }
  191. int cols = -1;
  192. if (getColumns() > 0) {
  193. cols = getColumns();
  194. } else if (!widthSet) {
  195. cols = DEFAULT_COLUMN_COUNT;
  196. }
  197. if (cols >= 0) {
  198. String colWidth = cols + "em";
  199. String containerWidth = (2 * cols + 4) + "em";
  200. // Caption wrapper width == optionsSelect + buttons +
  201. // selectionsSelect
  202. String captionWrapperWidth = (2 * cols + 4 - 0.5) + "em";
  203. options.setWidth(colWidth);
  204. if (optionsCaption != null) {
  205. optionsCaption.setWidth(colWidth);
  206. }
  207. selections.setWidth(colWidth);
  208. if (selectionsCaption != null) {
  209. selectionsCaption.setWidth(colWidth);
  210. }
  211. buttons.setWidth("3.5em");
  212. optionsContainer.setWidth(containerWidth);
  213. captionWrapper.setWidth(captionWrapperWidth);
  214. }
  215. if (getRows() > 0) {
  216. options.setVisibleItemCount(getRows());
  217. selections.setVisibleItemCount(getRows());
  218. }
  219. }
  220. @Override
  221. protected String[] getSelectedItems() {
  222. final ArrayList<String> selectedItemKeys = new ArrayList<String>();
  223. for (int i = 0; i < selections.getItemCount(); i++) {
  224. selectedItemKeys.add(selections.getValue(i));
  225. }
  226. return selectedItemKeys.toArray(new String[selectedItemKeys.size()]);
  227. }
  228. private boolean[] getSelectionBitmap(ListBox listBox) {
  229. final boolean[] selectedIndexes = new boolean[listBox.getItemCount()];
  230. for (int i = 0; i < listBox.getItemCount(); i++) {
  231. if (listBox.isItemSelected(i)) {
  232. selectedIndexes[i] = true;
  233. } else {
  234. selectedIndexes[i] = false;
  235. }
  236. }
  237. return selectedIndexes;
  238. }
  239. private void addItem() {
  240. Set<String> movedItems = moveSelectedItems(options, selections);
  241. selectedKeys.addAll(movedItems);
  242. client.updateVariable(id, "selected",
  243. selectedKeys.toArray(new String[selectedKeys.size()]),
  244. isImmediate());
  245. }
  246. private void removeItem() {
  247. Set<String> movedItems = moveSelectedItems(selections, options);
  248. selectedKeys.removeAll(movedItems);
  249. client.updateVariable(id, "selected",
  250. selectedKeys.toArray(new String[selectedKeys.size()]),
  251. isImmediate());
  252. }
  253. private Set<String> moveSelectedItems(ListBox source, ListBox target) {
  254. final boolean[] sel = getSelectionBitmap(source);
  255. final Set<String> movedItems = new HashSet<String>();
  256. int lastSelected = 0;
  257. for (int i = 0; i < sel.length; i++) {
  258. if (sel[i]) {
  259. final int optionIndex = i
  260. - (sel.length - source.getItemCount());
  261. movedItems.add(source.getValue(optionIndex));
  262. // Move selection to another column
  263. final String text = source.getItemText(optionIndex);
  264. final String value = source.getValue(optionIndex);
  265. target.addItem(text, value);
  266. target.setItemSelected(target.getItemCount() - 1, true);
  267. source.removeItem(optionIndex);
  268. if (source.getItemCount() > 0) {
  269. lastSelected = optionIndex > 0 ? optionIndex - 1 : 0;
  270. }
  271. }
  272. }
  273. if (source.getItemCount() > 0) {
  274. source.setSelectedIndex(lastSelected);
  275. }
  276. // If no items are left move the focus to the selections
  277. if (source.getItemCount() == 0) {
  278. target.setFocus(true);
  279. } else {
  280. source.setFocus(true);
  281. }
  282. return movedItems;
  283. }
  284. @Override
  285. public void onClick(ClickEvent event) {
  286. super.onClick(event);
  287. if (event.getSource() == add) {
  288. addItem();
  289. } else if (event.getSource() == remove) {
  290. removeItem();
  291. } else if (event.getSource() == options) {
  292. // unselect all in other list, to avoid mistakes (i.e wrong button)
  293. final int c = selections.getItemCount();
  294. for (int i = 0; i < c; i++) {
  295. selections.setItemSelected(i, false);
  296. }
  297. } else if (event.getSource() == selections) {
  298. // unselect all in other list, to avoid mistakes (i.e wrong button)
  299. final int c = options.getItemCount();
  300. for (int i = 0; i < c; i++) {
  301. options.setItemSelected(i, false);
  302. }
  303. }
  304. }
  305. @Override
  306. public void setHeight(String height) {
  307. super.setHeight(height);
  308. if ("".equals(height)) {
  309. options.setHeight("");
  310. selections.setHeight("");
  311. } else {
  312. setInternalHeights();
  313. }
  314. }
  315. private void setInternalHeights() {
  316. int captionHeight = 0;
  317. int totalHeight = getOffsetHeight();
  318. if (optionsCaption != null) {
  319. captionHeight = Util.getRequiredHeight(optionsCaption);
  320. } else if (selectionsCaption != null) {
  321. captionHeight = Util.getRequiredHeight(selectionsCaption);
  322. }
  323. String selectHeight = (totalHeight - captionHeight) + "px";
  324. selections.setHeight(selectHeight);
  325. options.setHeight(selectHeight);
  326. }
  327. @Override
  328. public void setWidth(String width) {
  329. super.setWidth(width);
  330. if (!"".equals(width) && width != null) {
  331. setInternalWidths();
  332. widthSet = true;
  333. } else {
  334. widthSet = false;
  335. }
  336. }
  337. private void setInternalWidths() {
  338. DOM.setStyleAttribute(getElement(), "position", "relative");
  339. int bordersAndPaddings = Util.measureHorizontalPaddingAndBorder(
  340. buttons.getElement(), 0);
  341. int buttonWidth = Util.getRequiredWidth(buttons);
  342. int totalWidth = getOffsetWidth();
  343. int spaceForSelect = (totalWidth - buttonWidth - bordersAndPaddings) / 2;
  344. options.setWidth(spaceForSelect + "px");
  345. if (optionsCaption != null) {
  346. optionsCaption.setWidth(spaceForSelect + "px");
  347. }
  348. selections.setWidth(spaceForSelect + "px");
  349. if (selectionsCaption != null) {
  350. selectionsCaption.setWidth(spaceForSelect + "px");
  351. }
  352. captionWrapper.setWidth("100%");
  353. }
  354. @Override
  355. protected void setTabIndex(int tabIndex) {
  356. options.setTabIndex(tabIndex);
  357. selections.setTabIndex(tabIndex);
  358. add.setTabIndex(tabIndex);
  359. remove.setTabIndex(tabIndex);
  360. }
  361. public void focus() {
  362. options.setFocus(true);
  363. }
  364. /**
  365. * Get the key that selects an item in the table. By default it is the Enter
  366. * key but by overriding this you can change the key to whatever you want.
  367. *
  368. * @return
  369. */
  370. protected int getNavigationSelectKey() {
  371. return KeyCodes.KEY_ENTER;
  372. }
  373. /*
  374. * (non-Javadoc)
  375. *
  376. * @see
  377. * com.google.gwt.event.dom.client.KeyDownHandler#onKeyDown(com.google.gwt
  378. * .event.dom.client.KeyDownEvent)
  379. */
  380. public void onKeyDown(KeyDownEvent event) {
  381. int keycode = event.getNativeKeyCode();
  382. // Catch tab and move between select:s
  383. if (keycode == KeyCodes.KEY_TAB && event.getSource() == options) {
  384. // Prevent default behavior
  385. event.preventDefault();
  386. // Remove current selections
  387. for (int i = 0; i < options.getItemCount(); i++) {
  388. options.setItemSelected(i, false);
  389. }
  390. // Focus selections
  391. selections.setFocus(true);
  392. }
  393. if (keycode == KeyCodes.KEY_TAB && event.isShiftKeyDown()
  394. && event.getSource() == selections) {
  395. // Prevent default behavior
  396. event.preventDefault();
  397. // Remove current selections
  398. for (int i = 0; i < selections.getItemCount(); i++) {
  399. selections.setItemSelected(i, false);
  400. }
  401. // Focus options
  402. options.setFocus(true);
  403. }
  404. if (keycode == getNavigationSelectKey()) {
  405. // Prevent default behavior
  406. event.preventDefault();
  407. // Decide which select the selection was made in
  408. if (event.getSource() == options) {
  409. // Prevents the selection to become a single selection when
  410. // using Enter key
  411. // as the selection key (default)
  412. options.setFocus(false);
  413. addItem();
  414. } else if (event.getSource() == selections) {
  415. // Prevents the selection to become a single selection when
  416. // using Enter key
  417. // as the selection key (default)
  418. selections.setFocus(false);
  419. removeItem();
  420. }
  421. }
  422. }
  423. /*
  424. * (non-Javadoc)
  425. *
  426. * @see
  427. * com.google.gwt.event.dom.client.MouseDownHandler#onMouseDown(com.google
  428. * .gwt.event.dom.client.MouseDownEvent)
  429. */
  430. public void onMouseDown(MouseDownEvent event) {
  431. // Ensure that items are deselected when selecting
  432. // from a different source. See #3699 for details.
  433. if (event.getSource() == options) {
  434. for (int i = 0; i < selections.getItemCount(); i++) {
  435. selections.setItemSelected(i, false);
  436. }
  437. } else if (event.getSource() == selections) {
  438. for (int i = 0; i < options.getItemCount(); i++) {
  439. options.setItemSelected(i, false);
  440. }
  441. }
  442. }
  443. /*
  444. * (non-Javadoc)
  445. *
  446. * @see
  447. * com.google.gwt.event.dom.client.DoubleClickHandler#onDoubleClick(com.
  448. * google.gwt.event.dom.client.DoubleClickEvent)
  449. */
  450. public void onDoubleClick(DoubleClickEvent event) {
  451. if (event.getSource() == options) {
  452. addItem();
  453. options.setSelectedIndex(-1);
  454. options.setFocus(false);
  455. } else if (event.getSource() == selections) {
  456. removeItem();
  457. selections.setSelectedIndex(-1);
  458. selections.setFocus(false);
  459. }
  460. }
  461. private static final String SUBPART_OPTION_SELECT = "leftSelect";
  462. private static final String SUBPART_OPTION_SELECT_ITEM = SUBPART_OPTION_SELECT
  463. + "-item";
  464. private static final String SUBPART_SELECTION_SELECT = "rightSelect";
  465. private static final String SUBPART_SELECTION_SELECT_ITEM = SUBPART_SELECTION_SELECT
  466. + "-item";
  467. private static final String SUBPART_LEFT_CAPTION = "leftCaption";
  468. private static final String SUBPART_RIGHT_CAPTION = "rightCaption";
  469. private static final String SUBPART_ADD_BUTTON = "add";
  470. private static final String SUBPART_REMOVE_BUTTON = "remove";
  471. public Element getSubPartElement(String subPart) {
  472. if (SUBPART_OPTION_SELECT.equals(subPart)) {
  473. return options.getElement();
  474. } else if (subPart.startsWith(SUBPART_OPTION_SELECT_ITEM)) {
  475. String idx = subPart.substring(SUBPART_OPTION_SELECT_ITEM.length());
  476. return (Element) options.getElement().getChild(
  477. Integer.parseInt(idx));
  478. } else if (SUBPART_SELECTION_SELECT.equals(subPart)) {
  479. return selections.getElement();
  480. } else if (subPart.startsWith(SUBPART_SELECTION_SELECT_ITEM)) {
  481. String idx = subPart.substring(SUBPART_SELECTION_SELECT_ITEM
  482. .length());
  483. return (Element) selections.getElement().getChild(
  484. Integer.parseInt(idx));
  485. } else if (optionsCaption != null
  486. && SUBPART_LEFT_CAPTION.equals(subPart)) {
  487. return optionsCaption.getElement();
  488. } else if (selectionsCaption != null
  489. && SUBPART_RIGHT_CAPTION.equals(subPart)) {
  490. return selectionsCaption.getElement();
  491. } else if (SUBPART_ADD_BUTTON.equals(subPart)) {
  492. return add.getElement();
  493. } else if (SUBPART_REMOVE_BUTTON.equals(subPart)) {
  494. return remove.getElement();
  495. }
  496. return null;
  497. }
  498. public String getSubPartName(Element subElement) {
  499. if (optionsCaption != null
  500. && optionsCaption.getElement().isOrHasChild(subElement)) {
  501. return SUBPART_LEFT_CAPTION;
  502. } else if (selectionsCaption != null
  503. && selectionsCaption.getElement().isOrHasChild(subElement)) {
  504. return SUBPART_RIGHT_CAPTION;
  505. } else if (options.getElement().isOrHasChild(subElement)) {
  506. if (options.getElement() == subElement) {
  507. return SUBPART_OPTION_SELECT;
  508. } else {
  509. int idx = Util.getChildElementIndex(subElement);
  510. return SUBPART_OPTION_SELECT_ITEM + idx;
  511. }
  512. } else if (selections.getElement().isOrHasChild(subElement)) {
  513. if (selections.getElement() == subElement) {
  514. return SUBPART_SELECTION_SELECT;
  515. } else {
  516. int idx = Util.getChildElementIndex(subElement);
  517. return SUBPART_SELECTION_SELECT_ITEM + idx;
  518. }
  519. } else if (add.getElement().isOrHasChild(subElement)) {
  520. return SUBPART_ADD_BUTTON;
  521. } else if (remove.getElement().isOrHasChild(subElement)) {
  522. return SUBPART_REMOVE_BUTTON;
  523. }
  524. return null;
  525. }
  526. public Widget getWidgetForPaintable() {
  527. return this;
  528. }
  529. }