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.

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