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.

UsingCustomNewItemHandlerInSelect.java 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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.tests;
  17. import java.util.Random;
  18. import com.vaadin.data.Item;
  19. import com.vaadin.ui.AbstractSelect;
  20. import com.vaadin.ui.CustomComponent;
  21. import com.vaadin.ui.Panel;
  22. import com.vaadin.ui.Select;
  23. import com.vaadin.ui.VerticalLayout;
  24. public class UsingCustomNewItemHandlerInSelect extends CustomComponent {
  25. private final Select select = new Select();
  26. public static Random random = new Random(1);
  27. private static int sequence = 0;
  28. public UsingCustomNewItemHandlerInSelect() {
  29. VerticalLayout pl = new VerticalLayout();
  30. pl.setMargin(true);
  31. final Panel panel = new Panel("Select demo", pl);
  32. pl.addComponent(select);
  33. select.setCaption("Select component");
  34. select.setImmediate(true);
  35. select.addContainerProperty("CAPTION", String.class, "");
  36. select.setItemCaptionMode(Select.ITEM_CAPTION_MODE_PROPERTY);
  37. select.setItemCaptionPropertyId("CAPTION");
  38. select.setNewItemsAllowed(true);
  39. select.setNewItemHandler(new MyNewItemHandler());
  40. populateSelect();
  41. setCompositionRoot(panel);
  42. }
  43. public void populateSelect() {
  44. final String[] names = new String[] { "John", "Mary", "Joe", "Sarah",
  45. "Jeff", "Jane", "Peter", "Marc", "Josie", "Linus" };
  46. for (int j = 0; j < 4; j++) {
  47. Integer id = new Integer(sequence++);
  48. Item item = select.addItem(id);
  49. item.getItemProperty("CAPTION").setValue(
  50. id.toString() + ": "
  51. + names[random.nextInt() % names.length]);
  52. }
  53. }
  54. public class MyNewItemHandler implements AbstractSelect.NewItemHandler {
  55. @Override
  56. public void addNewItem(String newItemCaption) {
  57. // here could be db insert or other backend operation
  58. Integer id = new Integer(sequence++);
  59. Item item = select.addItem(id);
  60. item.getItemProperty("CAPTION").setValue(
  61. id.toString() + ": " + newItemCaption);
  62. select.setValue(id);
  63. }
  64. }
  65. }