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.

TreeExample.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.automatedtests.featurebrowser;
  5. import com.vaadin.data.Item;
  6. import com.vaadin.data.Property;
  7. import com.vaadin.data.Property.ValueChangeEvent;
  8. import com.vaadin.event.Action;
  9. import com.vaadin.ui.AbstractSelect;
  10. import com.vaadin.ui.CustomComponent;
  11. import com.vaadin.ui.HorizontalLayout;
  12. import com.vaadin.ui.Label;
  13. import com.vaadin.ui.Panel;
  14. import com.vaadin.ui.TextField;
  15. import com.vaadin.ui.Tree;
  16. /**
  17. * Demonstrates basic Tree -functionality. Actions are used for add/remove item
  18. * functionality, and a ValueChangeListener reacts to both the Tree and the
  19. * TextField.
  20. */
  21. public class TreeExample extends CustomComponent implements Action.Handler,
  22. Tree.ValueChangeListener {
  23. private static final Action ADD = new Action("Add item");
  24. private static final Action DELETE = new Action("Delete item");
  25. private static final Action[] actions = new Action[] { ADD, DELETE };
  26. // Id for the caption property
  27. private static final Object CAPTION_PROPERTY = "caption";
  28. private static final String desc = "Try both right- and left-click!";
  29. Tree tree;
  30. TextField editor;
  31. public TreeExample() {
  32. final HorizontalLayout main = new HorizontalLayout();
  33. main.setWidth("100%");
  34. main.setDebugId("mainLayout");
  35. main.setMargin(true);
  36. setCompositionRoot(main);
  37. // Panel w/ Tree
  38. Panel p = new Panel("Select item");
  39. p.setStyleName(Panel.STYLE_LIGHT);
  40. p.setWidth("250px");
  41. // Description
  42. p.addComponent(new Label(desc));
  43. // Tree with a few items
  44. tree = new Tree();
  45. tree.setDebugId("tree");
  46. tree.setImmediate(true);
  47. // we'll use a property for caption instead of the item id ("value"),
  48. // so that multiple items can have the same caption
  49. tree.addContainerProperty(CAPTION_PROPERTY, String.class, "");
  50. tree.setItemCaptionMode(AbstractSelect.ITEM_CAPTION_MODE_PROPERTY);
  51. tree.setItemCaptionPropertyId(CAPTION_PROPERTY);
  52. for (int i = 1; i <= 3; i++) {
  53. final Object id = addCaptionedItem("Section " + i, null);
  54. tree.expandItem(id);
  55. addCaptionedItem("Team A", id);
  56. addCaptionedItem("Team B", id);
  57. }
  58. // listen for selections
  59. tree.addListener(this);
  60. // "context menu"
  61. tree.addActionHandler(this);
  62. p.addComponent(tree);
  63. main.addComponent(p);
  64. // Panel w/ TextField ("editor")
  65. p = new Panel("Edit item caption");
  66. p.setStyleName(Panel.STYLE_LIGHT);
  67. editor = new TextField();
  68. // make immediate, instead of adding an "apply" button
  69. editor.setImmediate(true);
  70. editor.setEnabled(false);
  71. editor.setColumns(15);
  72. p.addComponent(editor);
  73. main.addComponent(p);
  74. main.setExpandRatio(p, 1);
  75. }
  76. public Action[] getActions(Object target, Object sender) {
  77. // We can provide different actions for each target (item), but we'll
  78. // use the same actions all the time.
  79. return actions;
  80. }
  81. public void handleAction(Action action, Object sender, Object target) {
  82. if (action == DELETE) {
  83. tree.removeItem(target);
  84. } else {
  85. // Add
  86. final Object id = addCaptionedItem("New Item", target);
  87. tree.expandItem(target);
  88. tree.setValue(id);
  89. editor.focus();
  90. }
  91. }
  92. public void valueChange(ValueChangeEvent event) {
  93. final Object id = tree.getValue(); // selected item id
  94. if (event.getProperty() == tree) {
  95. // a Tree item was (un) selected
  96. if (id == null) {
  97. // no selecteion, disable TextField
  98. editor.removeListener(this);
  99. editor.setValue("");
  100. editor.setEnabled(false);
  101. } else {
  102. // item selected
  103. // first remove previous listener
  104. editor.removeListener(this);
  105. // enable TextField and update value
  106. editor.setEnabled(true);
  107. final Item item = tree.getItem(id);
  108. editor.setValue(item.getItemProperty(CAPTION_PROPERTY)
  109. .getValue());
  110. // listen for TextField changes
  111. editor.addListener(this);
  112. editor.focus();
  113. }
  114. } else {
  115. // TextField
  116. if (id != null) {
  117. final Item item = tree.getItem(id);
  118. final Property p = item.getItemProperty(CAPTION_PROPERTY);
  119. p.setValue(editor.getValue());
  120. tree.requestRepaint();
  121. }
  122. }
  123. }
  124. /**
  125. * Helper to add an item with specified caption and (optional) parent.
  126. *
  127. * @param caption
  128. * The item caption
  129. * @param parent
  130. * The (optional) parent item id
  131. * @return the created item's id
  132. */
  133. private Object addCaptionedItem(String caption, Object parent) {
  134. // add item, let tree decide id
  135. final Object id = tree.addItem();
  136. // get the created item
  137. final Item item = tree.getItem(id);
  138. // set our "caption" property
  139. final Property p = item.getItemProperty(CAPTION_PROPERTY);
  140. p.setValue(caption);
  141. if (parent != null) {
  142. tree.setChildrenAllowed(parent, true);
  143. tree.setParent(id, parent);
  144. tree.setChildrenAllowed(id, false);
  145. }
  146. return id;
  147. }
  148. }