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.

SimpleTree.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package com.vaadin.tests.components.tree;
  2. import java.util.Date;
  3. import com.vaadin.data.Item;
  4. import com.vaadin.data.util.HierarchicalContainer;
  5. import com.vaadin.event.Action;
  6. import com.vaadin.server.ThemeResource;
  7. import com.vaadin.tests.components.TestBase;
  8. import com.vaadin.ui.AbstractSelect;
  9. import com.vaadin.ui.AbstractSelect.ItemDescriptionGenerator;
  10. import com.vaadin.ui.Component;
  11. import com.vaadin.ui.Tree;
  12. public class SimpleTree extends TestBase implements Action.Handler {
  13. private static final String[][] hardware = { //
  14. { "Desktops", "Dell OptiPlex GX240", "Dell OptiPlex GX260",
  15. "Dell OptiPlex GX280" },
  16. { "Monitors", "Benq T190HD", "Benq T220HD", "Benq T240HD" },
  17. { "Laptops", "IBM ThinkPad T40", "IBM ThinkPad T43",
  18. "IBM ThinkPad T60" } };
  19. ThemeResource notCachedFolderIconLargeOther = new ThemeResource(
  20. "../runo/icons/16/ok.png?" + new Date().getTime());
  21. ThemeResource notCachedFolderIconLarge = new ThemeResource(
  22. "../runo/icons/16/folder.png?" + new Date().getTime());
  23. // Actions for the context menu
  24. private static final Action ACTION_ADD = new Action("Add child item");
  25. private static final Action ACTION_DELETE = new Action("Delete");
  26. private static final Action[] ACTIONS = new Action[] { ACTION_ADD,
  27. ACTION_DELETE };
  28. private Tree tree;
  29. @Override
  30. public void setup() {
  31. // Create the Tree,a dd to layout
  32. tree = new Tree("Hardware Inventory");
  33. addComponent(tree);
  34. // Contents from a (prefilled example) hierarchical container:
  35. tree.setContainerDataSource(getHardwareContainer());
  36. // Add actions (context menu)
  37. tree.addActionHandler(this);
  38. // Cause valueChange immediately when the user selects
  39. tree.setImmediate(true);
  40. // Set tree to show the 'name' property as caption for items
  41. tree.setItemCaptionPropertyId("name");
  42. tree.setItemCaptionMode(AbstractSelect.ITEM_CAPTION_MODE_PROPERTY);
  43. tree.setItemIcon(9, notCachedFolderIconLargeOther, "First Choice");
  44. tree.setItemIcon(11, notCachedFolderIconLarge);
  45. tree.setItemDescriptionGenerator(new ItemDescriptionGenerator() {
  46. @Override
  47. public String generateDescription(Component source, Object itemId,
  48. Object propertyId) {
  49. if ((Integer) itemId == 3) {
  50. return "tree item tooltip";
  51. }
  52. return "";
  53. }
  54. });
  55. // Expand whole tree
  56. for (Object id : tree.rootItemIds()) {
  57. tree.expandItemsRecursively(id);
  58. }
  59. }
  60. public static HierarchicalContainer getHardwareContainer() {
  61. Item item = null;
  62. int itemId = 0; // Increasing numbering for itemId:s
  63. // Create new container
  64. HierarchicalContainer hwContainer = new HierarchicalContainer();
  65. // Create containerproperty for name
  66. hwContainer.addContainerProperty("name", String.class, null);
  67. // Create containerproperty for icon
  68. hwContainer.addContainerProperty("icon", ThemeResource.class,
  69. new ThemeResource("../runo/icons/16/document.png"));
  70. for (int i = 0; i < hardware.length; i++) {
  71. // Add new item
  72. item = hwContainer.addItem(itemId);
  73. // Add name property for item
  74. item.getItemProperty("name").setValue(hardware[i][0]);
  75. // Allow children
  76. hwContainer.setChildrenAllowed(itemId, true);
  77. itemId++;
  78. for (int j = 1; j < hardware[i].length; j++) {
  79. if (j == 1) {
  80. item.getItemProperty("icon").setValue(
  81. new ThemeResource("../runo/icons/16/folder.png"));
  82. }
  83. // Add child items
  84. item = hwContainer.addItem(itemId);
  85. item.getItemProperty("name").setValue(hardware[i][j]);
  86. hwContainer.setParent(itemId, itemId - j);
  87. hwContainer.setChildrenAllowed(itemId, false);
  88. if (j == 2) {
  89. hwContainer.setChildrenAllowed(itemId, true);
  90. }
  91. itemId++;
  92. }
  93. }
  94. return hwContainer;
  95. }
  96. @Override
  97. protected String getDescription() {
  98. return "Sample Tree for testing WAI-ARIA functionality";
  99. }
  100. @Override
  101. protected Integer getTicketNumber() {
  102. return 0;
  103. }
  104. @Override
  105. public Action[] getActions(Object target, Object sender) {
  106. return ACTIONS;
  107. }
  108. @Override
  109. public void handleAction(Action action, Object sender, Object target) {
  110. System.out.println("Action: " + action.getCaption());
  111. }
  112. }