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

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