Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

VTwinColSelect.java 20KB

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