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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.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.Label;
  25. import com.vaadin.ui.LegacyWindow;
  26. import com.vaadin.ui.Panel;
  27. import com.vaadin.ui.Tree;
  28. import com.vaadin.ui.VerticalLayout;
  29. import com.vaadin.v7.ui.LegacyField;
  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 com.vaadin.server.LegacyApplication
  41. 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(
  51. "Tree FilesystemContainer demo");
  52. setMainWindow(w);
  53. final VerticalLayout main = new VerticalLayout();
  54. w.setContent(main);
  55. main.setMargin(true);
  56. main.setSpacing(true);
  57. propertyPanel.setHeight("120px");
  58. main.addComponent(propertyPanel);
  59. explorerPanel.setHeight("100%");
  60. main.addComponent(explorerPanel);
  61. main.setExpandRatio(explorerPanel, 1);
  62. // Explorer panel contains tree
  63. VerticalLayout explorerLayout = new VerticalLayout();
  64. explorerLayout.setMargin(true);
  65. explorerPanel.setContent(explorerLayout);
  66. explorerLayout.addComponent(filesystem);
  67. // Property panel contains label
  68. VerticalLayout propertyLayout = new VerticalLayout();
  69. propertyLayout.setMargin(true);
  70. propertyPanel.setContent(propertyLayout);
  71. propertyLayout.addComponent(fileProperties);
  72. fileProperties.setCaption("No file selected.");
  73. propertyPanel.setEnabled(false);
  74. // Get sample directory
  75. final File sampleDir = SampleDirectory
  76. .getDirectory(VaadinSession.getCurrent(), w);
  77. // Populate tree with FilesystemContainer
  78. final FilesystemContainer fsc = new FilesystemContainer(sampleDir,
  79. true);
  80. filesystem.setContainerDataSource(fsc);
  81. // "this" handles all filesystem events
  82. // e.g. node clicked, expanded etc.
  83. filesystem.addListener(this);
  84. // Value changes are immediate
  85. filesystem.setImmediate(true);
  86. }
  87. /**
  88. * Listener for any component events. This class has been registered as an
  89. * listener for component fsTree.
  90. */
  91. @Override
  92. public void componentEvent(Event event) {
  93. // Check if event occured at fsTree component
  94. if (event.getSource() == filesystem) {
  95. // Check if event is about changing value
  96. if (event.getClass() == LegacyField.ValueChangeEvent.class) {
  97. // Update property panel contents
  98. final FileItem fileItem = (FileItem) filesystem
  99. .getItem(filesystem.getValue());
  100. fileProperties.setIcon(fileItem.getIcon());
  101. fileProperties.setCaption(fileItem.getName() + ", size "
  102. + fileItem.getSize() + " bytes.");
  103. propertyPanel.setEnabled(true);
  104. }
  105. // here we could check for other type of events for filesystem
  106. // component
  107. }
  108. // here we could check for other component's events
  109. }
  110. }