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.

TreeFilesystemContainer.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package com.vaadin.tests;
  2. import java.io.File;
  3. import com.vaadin.server.VaadinSession;
  4. import com.vaadin.tests.util.SampleDirectory;
  5. import com.vaadin.ui.Component.Event;
  6. import com.vaadin.ui.Component.Listener;
  7. import com.vaadin.ui.Label;
  8. import com.vaadin.ui.LegacyWindow;
  9. import com.vaadin.ui.Panel;
  10. import com.vaadin.ui.VerticalLayout;
  11. import com.vaadin.v7.data.util.FilesystemContainer;
  12. import com.vaadin.v7.data.util.FilesystemContainer.FileItem;
  13. import com.vaadin.v7.ui.Field;
  14. import com.vaadin.v7.ui.Tree;
  15. /**
  16. * Browsable file explorer using Vaadin Tree component. Demonstrates: how to use
  17. * <code>com.vaadin.ui.Component.Tree</code> datasource container, how to create
  18. * <code>com.vaadin.data.util.FilesystemContainer</code>, how to read
  19. * <code>com.vaadin.ui.Component.Event</code> objects, how to receive and handle
  20. * any event by implementing <code>com.vaadin.ui.Component.Listener</code>.
  21. *
  22. * @since 4.0.0
  23. *
  24. */
  25. public class TreeFilesystemContainer extends com.vaadin.server.LegacyApplication
  26. implements Listener {
  27. // Filesystem explorer panel and it's components
  28. private final Panel explorerPanel = new Panel("Filesystem explorer");
  29. private final Tree filesystem = new Tree();
  30. // File properties panel and it's components
  31. private final Panel propertyPanel = new Panel("File properties");
  32. private final Label fileProperties = new Label();
  33. @Override
  34. public void init() {
  35. final LegacyWindow w = new LegacyWindow(
  36. "Tree FilesystemContainer demo");
  37. setMainWindow(w);
  38. final VerticalLayout main = new VerticalLayout();
  39. w.setContent(main);
  40. main.setMargin(true);
  41. main.setSpacing(true);
  42. propertyPanel.setHeight("120px");
  43. main.addComponent(propertyPanel);
  44. explorerPanel.setHeight("100%");
  45. main.addComponent(explorerPanel);
  46. main.setExpandRatio(explorerPanel, 1);
  47. // Explorer panel contains tree
  48. VerticalLayout explorerLayout = new VerticalLayout();
  49. explorerLayout.setMargin(true);
  50. explorerPanel.setContent(explorerLayout);
  51. explorerLayout.addComponent(filesystem);
  52. // Property panel contains label
  53. VerticalLayout propertyLayout = new VerticalLayout();
  54. propertyLayout.setMargin(true);
  55. propertyPanel.setContent(propertyLayout);
  56. propertyLayout.addComponent(fileProperties);
  57. fileProperties.setCaption("No file selected.");
  58. propertyPanel.setEnabled(false);
  59. // Get sample directory
  60. final File sampleDir = SampleDirectory
  61. .getDirectory(VaadinSession.getCurrent(), w);
  62. // Populate tree with FilesystemContainer
  63. final FilesystemContainer fsc = new FilesystemContainer(sampleDir,
  64. true);
  65. filesystem.setContainerDataSource(fsc);
  66. // "this" handles all filesystem events
  67. // e.g. node clicked, expanded etc.
  68. filesystem.addListener(this);
  69. // Value changes are immediate
  70. filesystem.setImmediate(true);
  71. }
  72. /**
  73. * Listener for any component events. This class has been registered as an
  74. * listener for component fsTree.
  75. */
  76. @Override
  77. public void componentEvent(Event event) {
  78. // Check if event occurred at fsTree component
  79. if (event.getSource() == filesystem) {
  80. // Check if event is about changing value
  81. if (event.getClass() == Field.ValueChangeEvent.class) {
  82. // Update property panel contents
  83. final FileItem fileItem = (FileItem) filesystem
  84. .getItem(filesystem.getValue());
  85. fileProperties.setIcon(fileItem.getIcon());
  86. fileProperties.setCaption(fileItem.getName() + ", size "
  87. + fileItem.getSize() + " bytes.");
  88. propertyPanel.setEnabled(true);
  89. }
  90. // here we could check for other type of events for filesystem
  91. // component
  92. }
  93. // here we could check for other component's events
  94. }
  95. }