1 package net.sourceforge.pmd.swingui;
2
3 import javax.swing.event.TreeExpansionEvent;
4 import javax.swing.event.TreeWillExpandListener;
5 import javax.swing.tree.DefaultTreeModel;
6 import javax.swing.tree.TreePath;
7 import java.io.File;
8 import java.io.FileFilter;
9 import java.util.Enumeration;
10
11 /***
12 *
13 * @author Donald A. Leckie
14 * @since August 17, 2002
15 * @version $Revision: 1.6 $, $Date: 2003/03/14 19:04:05 $
16 */
17 class DirectoryTreeModel extends DefaultTreeModel implements TreeWillExpandListener {
18
19 private DirectoryTree m_directoryTree;
20
21 /***
22 *****************************************************************************
23 */
24 protected DirectoryTreeModel(String rootName) {
25 super(DirectoryTreeNode.createRootNode(rootName));
26 }
27
28 /***
29 ********************************************************************************
30 *
31 */
32 protected void setupFiles(File[] rootFiles) {
33 DirectoryTreeNode rootNode;
34 FilesFilter filesFilter;
35
36 rootNode = (DirectoryTreeNode) getRoot();
37 filesFilter = new FilesFilter();
38
39 if (rootFiles != null) {
40 for (int n1 = 0; n1 < rootFiles.length; n1++) {
41 File rootFile;
42 DirectoryTreeNode fileNode;
43
44 rootFile = rootFiles[n1];
45 fileNode = new DirectoryTreeNode(rootFile);
46
47 rootNode.add(fileNode);
48
49 File[] files = rootFile.listFiles(filesFilter);
50
51 if (files != null) {
52 for (int n2 = 0; n2 < files.length; n2++) {
53 fileNode.add(new DirectoryTreeNode(files[n2]));
54 }
55 }
56 }
57 }
58 }
59
60 /***
61 ********************************************************************************
62 *
63 * @param directory
64 */
65 protected void setDirectoryTree(DirectoryTree directoryTree) {
66 m_directoryTree = directoryTree;
67
68 m_directoryTree.addTreeWillExpandListener(this);
69 }
70
71 /***
72 ******************************************************************************
73 *
74 * Called before a directory tree node in the tree will be expanded. The tree node
75 * to be expanded will contain a directory. The tree node will contain children
76 * consisting of subdirectories and/or files. The subdirectory tree nodes will have
77 * child tree nodes added so that they may be expanded.
78 *
79 * @param event
80 *
81 * @throws ExpandVetoException
82 */
83 public void treeWillExpand(TreeExpansionEvent event) {
84 TreePath treePath;
85 DirectoryTreeNode treeNode;
86 Enumeration children;
87
88 treePath = event.getPath();
89 treeNode = (DirectoryTreeNode) treePath.getLastPathComponent();
90 children = treeNode.children();
91
92 while (children.hasMoreElements()) {
93 DirectoryTreeNode childTreeNode = (DirectoryTreeNode) children.nextElement();
94 File directory = (File) childTreeNode.getUserObject();
95 File[] files = directory.listFiles(new FilesFilter());
96
97 childTreeNode.removeAllChildren();
98
99 if (files != null) {
100 for (int n = 0; n < files.length; n++) {
101 childTreeNode.add(new DirectoryTreeNode(files[n]));
102 }
103 }
104 }
105 }
106
107 /***
108 ******************************************************************************
109 *
110 * Called before a directory tree node in the tree will be collapsed. The tree node
111 * to be collapsed will contain a directory. The tree node will contain children
112 * consisting of subdirectories and/or files. The subdirectory tree nodes will have
113 * their child tree nodes removed since they will no longer be visible.
114 *
115 * @param event
116 *
117 * @throws ExpandVetoException
118 */
119 public void treeWillCollapse(TreeExpansionEvent event) {
120 TreePath treePath;
121 DirectoryTreeNode treeNode;
122 Enumeration children;
123
124 treePath = event.getPath();
125 treeNode = (DirectoryTreeNode) treePath.getLastPathComponent();
126 children = treeNode.children();
127
128 while (children.hasMoreElements()) {
129 DirectoryTreeNode childTreeNode = (DirectoryTreeNode) children.nextElement();
130
131 childTreeNode.removeAllChildren();
132 }
133 }
134
135 /***
136 *******************************************************************************
137 *******************************************************************************
138 *******************************************************************************
139 */
140 private class FilesFilter implements FileFilter {
141
142 /***
143 ****************************************************************************
144 *
145 * @param file
146 *
147 * @return
148 */
149 public boolean accept(File file) {
150 return file.isDirectory() && (file.isHidden() == false);
151 }
152 }
153 }
This page was automatically generated by Maven