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

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