/Users/lyon/j4p/src/futils/DirList.java

1    package futils; 
2     
3    import javax.swing.JDialog; 
4    import javax.swing.JLabel; 
5    import java.awt.Container; 
6    import java.awt.FlowLayout; 
7    import java.io.File; 
8    import java.io.FilenameFilter; 
9    import java.util.Collections; 
10   import java.util.Vector; 
11    
12   /** 
13    * DirList, w/o recursion. 
14    */ 
15   public class DirList { 
16       private File startDir; 
17       private Vector history = new Vector(); 
18       private int totalBytes = 0; 
19       private String suffix = null; 
20       private boolean verbose = false; 
21       private ProgressPanel pp = new ProgressPanel(); 
22       private FilenameFilter fnf = null; 
23    
24       public class ProgressPanel extends JDialog { 
25           JLabel jl = new JLabel("file #"); 
26           JLabel fl = new JLabel("0"); 
27           int i = 0; 
28    
29           ProgressPanel() { 
30               Container c = getContentPane(); 
31               c.setLayout(new FlowLayout()); 
32               c.add(jl); 
33               c.add(fl); 
34               setSize(200, 200); 
35           } 
36    
37           public void incrementValue() { 
38               fl.setText("" + i++); 
39           } 
40       } 
41    
42       public static void main(String args[]) { 
43           DirList dl = new DirList(""); 
44           dl.sort(); 
45           File f[] = dl.getFiles(); 
46           dl.printFiles(); 
47           dl.printStats(); 
48       } 
49    
50       public void sort() { 
51           Collections.sort(history); 
52       } 
53    
54       public DirList(FilenameFilter fnf) { 
55           this.fnf = fnf; 
56           suffix = null; 
57           startDir = Futil.getReadDirFile("select a directory"); 
58           pp.show(); 
59           startAtThisDir(startDir); 
60           pp.hide(); 
61       } 
62    
63       public DirList(String _suffix) { 
64           suffix = _suffix; 
65           startDir = Futil.getReadDirFile("select a *." + suffix + " file"); 
66           pp.show(); 
67           startAtThisDir(startDir); 
68           pp.hide(); 
69       } 
70    
71       public DirList(File _startDir, String _suffix) { 
72           startDir = _startDir; 
73           startAtThisDir(startDir); 
74       } 
75    
76       public void printStats() { 
77           System.out.println("Saw " + 
78                   getTotalFiles() + 
79                   " *" + 
80                   suffix + 
81                   " files with a total size of " + 
82                   totalBytes + 
83                   " bytes"); 
84       } 
85    
86       public void printFiles() { 
87           System.out.println("PrintFiles-------"); 
88           File f[] = getFilesNotDirectories(); 
89           for (int i = 0; i < f.length; i++) 
90               System.out.println(f[i]); 
91       } 
92    
93       public final void printVerbose(Object o) { 
94           if (verbose) 
95               System.out.println(o); 
96       } 
97    
98       //------------------------------------------------------- 
99       // Recursive function that given an anchor directory 
100      // will walk directory gui.tree 
101      // 
102      //------------------------------------------------------- 
103      public void startAtThisDir(File f1) { 
104          printVerbose("Selected -> " + f1); 
105          printVerbose("Files in this Directory: "); 
106          addFilesInThisDirectory(f1); 
107          processAllDirectoriesInThisDirectory(f1); 
108      } 
109   
110      private Vector vecDir = new Vector(); 
111   
112      private File nextDir() { 
113          return (File) vecDir.remove(vecDir.size() - 1); 
114      } 
115   
116      private void addDirs(File dirList[]) { 
117          if (dirList == null) return; 
118          for (int i = 0; i < dirList.length; i++) 
119              vecDir.addElement(dirList[i]); 
120      } 
121   
122      private void processAllDirectoriesInThisDirectory(File f1) { 
123          addDirs(f1.listFiles(new DirFilter())); 
124          while (vecDir.size() > 0) { 
125              File f = nextDir(); 
126              printVerbose("Selected -> " + f); 
127              printVerbose("Files in this Directory: "); 
128              addFilesInThisDirectory(f); 
129              addDirs(f.listFiles(new DirFilter())); 
130          } 
131      } 
132   
133      //------------------------------------------------------- 
134      // Loop through all the filenames in the current directory 
135      // and store them in history: 
136      //------------------------------------------------------- 
137      private void addFilesInThisDirectory(File f1) { 
138          File ls[] = null; 
139          pp.incrementValue(); 
140          if (fnf == null)  { 
141               ls = f1.listFiles(new WildFilter(suffix)); 
142          } else  ls = f1.listFiles(fnf); 
143          if (ls == null) return; 
144          for (int i = 0; i < ls.length; i++) 
145              addFile(ls[i]); 
146      } 
147   
148      private void addFile(File f2) { 
149          int bytes = Futil.available(f2); 
150          totalBytes += bytes; 
151          printVerbose(f2 + 
152                  " has " + 
153                  bytes + " bytes"); 
154          history.addElement(f2); 
155      } 
156   
157      public File getStartDir() { 
158          return startDir; 
159      } 
160   
161      public File[] getFilesNotDirectories() { 
162          Vector v = new Vector(); 
163          for (int i = 0; i < history.size(); i++) { 
164              File f = (File) history.elementAt(i); 
165              if (f.isFile()) 
166                  v.addElement(f); 
167          } 
168          File f[] = new File[v.size()]; 
169          v.copyInto(f); 
170          return f; 
171      } 
172   
173      public File[] getFiles() { 
174          File f[] = new File[history.size()]; 
175          history.copyInto(f); 
176          return f; 
177      } 
178   
179      public int getTotalBytes() { 
180          return totalBytes; 
181      } 
182   
183      public int getTotalFiles() { 
184          return history.size(); 
185      } 
186   
187      public String getSuffix() { 
188          return suffix; 
189      } 
190  } 
191   
192   
193   
194