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 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tests;
  17. import java.io.File;
  18. import com.vaadin.server.VaadinSession;
  19. import com.vaadin.shared.ui.ContentMode;
  20. import com.vaadin.tests.util.SampleDirectory;
  21. import com.vaadin.ui.Label;
  22. import com.vaadin.ui.LegacyWindow;
  23. import com.vaadin.ui.Panel;
  24. import com.vaadin.ui.VerticalLayout;
  25. import com.vaadin.v7.data.Item;
  26. import com.vaadin.v7.ui.Tree;
  27. import com.vaadin.v7.ui.Tree.ExpandEvent;
  28. /**
  29. * Browsable file explorer using Vaadin Tree component. Demonstrates: how to add
  30. * items hierarchically into <code>com.vaadin.ui.Component.Tree</code>, how to
  31. * receive ExpandEvent and implement
  32. * <code>com.vaadin.ui.Tree.ExpandListener</code>.
  33. *
  34. * @since 4.0.0
  35. *
  36. */
  37. public class TreeFilesystem extends com.vaadin.server.LegacyApplication
  38. implements Tree.ExpandListener {
  39. // Filesystem explorer panel and it's components
  40. private final Panel explorerPanel = new Panel("Filesystem explorer");
  41. private final Tree tree = new Tree();
  42. @Override
  43. public void init() {
  44. final LegacyWindow main = new LegacyWindow("Tree filesystem demo");
  45. setMainWindow(main);
  46. // Main window contains heading and panel
  47. main.addComponent(new Label("<h2>Tree demo</h2>", ContentMode.HTML));
  48. // configure file structure panel
  49. VerticalLayout explorerLayout = new VerticalLayout();
  50. explorerLayout.setMargin(true);
  51. explorerPanel.setContent(explorerLayout);
  52. main.addComponent(explorerPanel);
  53. explorerLayout.addComponent(tree);
  54. explorerPanel.setHeight("400px");
  55. // "this" handles tree's expand event
  56. tree.addListener(this);
  57. // Get sample directory
  58. final File sampleDir = SampleDirectory
  59. .getDirectory(VaadinSession.getCurrent(), main);
  60. // populate tree's root node with example directory
  61. if (sampleDir != null) {
  62. populateNode(sampleDir.getAbsolutePath(), null);
  63. }
  64. }
  65. /**
  66. * Handle tree expand event, populate expanded node's childs with new files
  67. * and directories.
  68. */
  69. @Override
  70. public void nodeExpand(ExpandEvent event) {
  71. final Item i = tree.getItem(event.getItemId());
  72. if (!tree.hasChildren(i)) {
  73. // populate tree's node which was expanded
  74. populateNode(event.getItemId().toString(), event.getItemId());
  75. }
  76. }
  77. /**
  78. * Populates files to tree as items. In this example items are of String
  79. * type that consist of file path. New items are added to tree and item's
  80. * parent and children properties are updated.
  81. *
  82. * @param file
  83. * path which contents are added to tree
  84. * @param parent
  85. * for added nodes, if null then new nodes are added to root node
  86. */
  87. private void populateNode(String file, Object parent) {
  88. final File subdir = new File(file);
  89. final File[] files = subdir.listFiles();
  90. for (int x = 0; x < files.length; x++) {
  91. try {
  92. // add new item (String) to tree
  93. final String path = files[x].getCanonicalPath().toString();
  94. tree.addItem(path);
  95. // set parent if this item has one
  96. if (parent != null) {
  97. tree.setParent(path, parent);
  98. }
  99. // check if item is a directory and read access exists
  100. if (files[x].isDirectory() && files[x].canRead()) {
  101. // yes, childrens therefore exists
  102. tree.setChildrenAllowed(path, true);
  103. } else {
  104. // no, childrens therefore do not exists
  105. tree.setChildrenAllowed(path, false);
  106. }
  107. } catch (final Exception e) {
  108. throw new RuntimeException(e);
  109. }
  110. }
  111. }
  112. }