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.

TreeFilesystem.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.itmill.toolkit.demo;
  5. import java.io.File;
  6. import com.itmill.toolkit.data.Item;
  7. import com.itmill.toolkit.demo.util.SampleDirectory;
  8. import com.itmill.toolkit.ui.Label;
  9. import com.itmill.toolkit.ui.Panel;
  10. import com.itmill.toolkit.ui.Tree;
  11. import com.itmill.toolkit.ui.Window;
  12. import com.itmill.toolkit.ui.Tree.ExpandEvent;
  13. /**
  14. * Browsable file explorer using Toolkit Tree component. Demonstrates: how to
  15. * add items hierarchially into
  16. * <code>com.itmill.toolkit.ui.Component.Tree</code>, how to receive
  17. * ExpandEvent and implement
  18. * <code>com.itmill.toolkit.ui.Tree.ExpandListener</code>.
  19. *
  20. * @since 4.0.0
  21. *
  22. */
  23. public class TreeFilesystem extends com.itmill.toolkit.Application implements
  24. Tree.ExpandListener {
  25. // Filesystem explorer panel and it's components
  26. private final Panel explorerPanel = new Panel("Filesystem explorer");
  27. private final Tree tree = new Tree();
  28. public void init() {
  29. final Window main = new Window("Tree filesystem demo");
  30. setMainWindow(main);
  31. // Main window contains heading and panel
  32. main.addComponent(new Label("<h2>Tree demo</h2>", Label.CONTENT_XHTML));
  33. // configure file structure panel
  34. main.addComponent(explorerPanel);
  35. explorerPanel.addComponent(tree);
  36. explorerPanel.setHeight(400);
  37. // "this" handles tree's expand event
  38. tree.addListener(this);
  39. // Get sample directory
  40. final File sampleDir = SampleDirectory.getDirectory(this);
  41. // populate tree's root node with example directory
  42. if (sampleDir != null) {
  43. populateNode(sampleDir.getAbsolutePath(), null);
  44. }
  45. }
  46. /**
  47. * Handle tree expand event, populate expanded node's childs with new files
  48. * and directories.
  49. */
  50. public void nodeExpand(ExpandEvent event) {
  51. final Item i = tree.getItem(event.getItemId());
  52. if (!tree.hasChildren(i)) {
  53. // populate tree's node which was expanded
  54. populateNode(event.getItemId().toString(), event.getItemId());
  55. }
  56. }
  57. /**
  58. * Populates files to tree as items. In this example items are of String
  59. * type that consist of file path. New items are added to tree and item's
  60. * parent and children properties are updated.
  61. *
  62. * @param file
  63. * path which contents are added to tree
  64. * @param parent
  65. * for added nodes, if null then new nodes are added to root
  66. * node
  67. */
  68. private void populateNode(String file, Object parent) {
  69. final File subdir = new File(file);
  70. final File[] files = subdir.listFiles();
  71. for (int x = 0; x < files.length; x++) {
  72. try {
  73. // add new item (String) to tree
  74. final String path = files[x].getCanonicalPath().toString();
  75. tree.addItem(path);
  76. // set parent if this item has one
  77. if (parent != null) {
  78. tree.setParent(path, parent);
  79. }
  80. // check if item is a directory and read access exists
  81. if (files[x].isDirectory() && files[x].canRead()) {
  82. // yes, childrens therefore exists
  83. tree.setChildrenAllowed(path, true);
  84. } else {
  85. // no, childrens therefore do not exists
  86. tree.setChildrenAllowed(path, false);
  87. }
  88. } catch (final Exception e) {
  89. throw new RuntimeException(e);
  90. }
  91. }
  92. }
  93. }