KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > improve > struts > taglib > layout > treeview > FileTreeMenuComponent


1 package fr.improve.struts.taglib.layout.treeview;
2
3 import java.io.File JavaDoc;
4
5 import fr.improve.struts.taglib.layout.menu.MenuComponent;
6
7 /**
8  * Example of a lazy initialized MenuComponent showing a file system content.
9  *
10  * @author jnribette
11  */

12 public class FileTreeMenuComponent extends MenuComponent {
13     private File JavaDoc file;
14     private boolean inited = false;
15     
16     public FileTreeMenuComponent() {
17         this(new File JavaDoc("/"));
18     }
19     
20     public FileTreeMenuComponent(File JavaDoc in_file) {
21         file = in_file;
22     }
23
24     
25     public MenuComponent[] getMenuComponents() {
26         synchronized(this) {
27             if (!inited) {
28                 inited = true;
29                 File JavaDoc[] files = file.listFiles();
30                 if (files!=null && files.length!=0) {
31                     System.out.println("Loading childs of " + file.getAbsolutePath());
32                     for (int i = 0; i < files.length; i++) {
33                         File JavaDoc subFile = files[i];
34                         addMenuComponent(new FileTreeMenuComponent(subFile));
35                     }
36                 }
37             }
38             return super.getMenuComponents();
39         }
40     }
41     
42     /**
43      * The title of the item if the file name.
44      */

45     public String JavaDoc getTitle() {
46         return file.getName();
47     }
48     
49     public boolean hasMenuComponents() {
50         return file.isDirectory();
51     }
52 }
53
Popular Tags