001: package org.acm.seguin.pmd.swingui;
002:
003: import javax.swing.event.TreeExpansionEvent;
004: import javax.swing.event.TreeWillExpandListener;
005: import javax.swing.tree.DefaultTreeModel;
006: import javax.swing.tree.TreePath;
007: import java.io.File;
008: import java.io.FileFilter;
009: import java.util.Enumeration;
010:
011: /**
012: *
013: * @author Donald A. Leckie
014: * @since August 17, 2002
015: * @version $Revision: 1.2 $, $Date: 2003/09/21 20:51:41 $
016: */
017: class DirectoryTreeModel extends DefaultTreeModel implements
018: TreeWillExpandListener {
019:
020: private DirectoryTree m_directoryTree;
021:
022: /**
023: *****************************************************************************
024: */
025: protected DirectoryTreeModel(String rootName) {
026: super (DirectoryTreeNode.createRootNode(rootName));
027: }
028:
029: /**
030: ********************************************************************************
031: *
032: */
033: protected void setupFiles(File[] rootFiles) {
034: DirectoryTreeNode rootNode;
035: FilesFilter filesFilter;
036:
037: rootNode = (DirectoryTreeNode) getRoot();
038: filesFilter = new FilesFilter();
039:
040: if (rootFiles != null) {
041: for (int n1 = 0; n1 < rootFiles.length; n1++) {
042: File rootFile;
043: DirectoryTreeNode fileNode;
044:
045: rootFile = rootFiles[n1];
046: fileNode = new DirectoryTreeNode(rootFile);
047:
048: rootNode.add(fileNode);
049:
050: if (rootFile.exists()) {
051: File[] files = rootFile.listFiles(filesFilter);
052:
053: if (files != null) {
054: for (int n2 = 0; n2 < files.length; n2++) {
055: fileNode.add(new DirectoryTreeNode(
056: files[n2]));
057: }
058: }
059: }
060: }
061: }
062: }
063:
064: /**
065: ********************************************************************************
066: *
067: * @param directory
068: */
069: protected void setDirectoryTree(DirectoryTree directoryTree) {
070: m_directoryTree = directoryTree;
071:
072: m_directoryTree.addTreeWillExpandListener(this );
073: }
074:
075: /**
076: ******************************************************************************
077: *
078: * Called before a directory tree node in the tree will be expanded. The tree node
079: * to be expanded will contain a directory. The tree node will contain children
080: * consisting of subdirectories and/or files. The subdirectory tree nodes will have
081: * child tree nodes added so that they may be expanded.
082: *
083: * @param event
084: *
085: * @throws ExpandVetoException
086: */
087: public void treeWillExpand(TreeExpansionEvent event) {
088: TreePath treePath;
089: DirectoryTreeNode treeNode;
090: Enumeration children;
091:
092: treePath = event.getPath();
093: treeNode = (DirectoryTreeNode) treePath.getLastPathComponent();
094: children = treeNode.children();
095:
096: while (children.hasMoreElements()) {
097: DirectoryTreeNode childTreeNode = (DirectoryTreeNode) children
098: .nextElement();
099: File directory = (File) childTreeNode.getUserObject();
100: if (directory.exists()) {
101: File[] files = directory.listFiles(new FilesFilter());
102:
103: childTreeNode.removeAllChildren();
104:
105: if (files != null) {
106: for (int n = 0; n < files.length; n++) {
107: childTreeNode.add(new DirectoryTreeNode(
108: files[n]));
109: }
110: }
111: }
112: }
113: }
114:
115: /**
116: ******************************************************************************
117: *
118: * Called before a directory tree node in the tree will be collapsed. The tree node
119: * to be collapsed will contain a directory. The tree node will contain children
120: * consisting of subdirectories and/or files. The subdirectory tree nodes will have
121: * their child tree nodes removed since they will no longer be visible.
122: *
123: * @param event
124: *
125: * @throws ExpandVetoException
126: */
127: public void treeWillCollapse(TreeExpansionEvent event) {
128: TreePath treePath;
129: DirectoryTreeNode treeNode;
130: Enumeration children;
131:
132: treePath = event.getPath();
133: treeNode = (DirectoryTreeNode) treePath.getLastPathComponent();
134: children = treeNode.children();
135:
136: while (children.hasMoreElements()) {
137: DirectoryTreeNode childTreeNode = (DirectoryTreeNode) children
138: .nextElement();
139:
140: childTreeNode.removeAllChildren();
141: }
142: }
143:
144: /**
145: *******************************************************************************
146: *******************************************************************************
147: *******************************************************************************
148: */
149: private class FilesFilter implements FileFilter {
150:
151: /**
152: ****************************************************************************
153: *
154: * @param file
155: *
156: * @return
157: */
158: public boolean accept(File file) {
159: return file.isDirectory() && (file.isHidden() == false);
160: }
161: }
162: }
|