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.4KB

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