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

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