KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > gui > filechooser > FolderInfoFileWrapper


1 package net.suberic.pooka.gui.filechooser;
2 import javax.swing.*;
3 import java.io.*;
4 import java.util.Vector JavaDoc;
5 import net.suberic.pooka.*;
6
7 /**
8  * This wraps a Folder or Store in a File object.
9  */

10 public class FolderInfoFileWrapper extends File {
11   private FolderInfo folder;
12   private StoreInfo store;
13   private FolderInfoFileWrapper parent;
14   private FolderInfoFileWrapper[] children = null;
15   private String JavaDoc path;
16   
17   /**
18    * Creates a new FolderInfoFileWrapper from a FolderInfo. This should only
19    * be used for direct children of this FolderInfo.
20    */

21   public FolderInfoFileWrapper(FolderInfo f, FolderInfoFileWrapper p) {
22     super(f.getFolderName());
23     folder = f;
24     parent = p;
25     if (parent == null)
26       parent = this;
27
28     path = f.getFolderName();
29     
30       if (p != null)
31     getLogger().info("creating FolderInfoFileWrapper from parent '" + p.getAbsolutePath() + "' from folder '" + f.getFolderName() + "'");
32       else
33     getLogger().info("creating FolderInfoFileWrapper from parent 'null' from folder '" + f.getFolderName() + "'");
34   }
35   
36   /**
37    * Creates a new FolderInfoFileWrapper from a StoreInfo. This probably
38    * really shouldn't be used.
39    */

40   public FolderInfoFileWrapper(StoreInfo s, FolderInfoFileWrapper p) {
41     super(s.getStoreID());
42     store = s;
43     parent = p;
44     path = s.getStoreID();
45     
46       if (p != null)
47     getLogger().info("creating FolderInfoFileWrapper from parent '" + p.getAbsolutePath() + "' from store '" + s.getStoreID() + "'");
48       else
49     getLogger().info("creating FolderInfoFileWrapper from parent 'null' from store '" + s.getStoreID() + "'");
50   }
51   
52   /**
53    * Creates a new FolderInfoFileWrapper from a FolderInfo with the given path
54    * and parent. This is used for making relative paths to files, i.e.
55    * a child of '/foo' called 'bar/baz'.
56    */

57   public FolderInfoFileWrapper(FolderInfo f, FolderInfoFileWrapper p, String JavaDoc filePath) {
58     super(f.getFolderName());
59     folder = f;
60     parent = p;
61     path = filePath;
62     
63       if (p != null)
64     getLogger().info("creating FolderInfoFileWrapper from parent '" + p.getAbsolutePath() + "' called '" + filePath + "'");
65       else
66     getLogger().info("creating FolderInfoFileWrapper from parent 'null' called '" + filePath + "'");
67   }
68   
69   /**
70    * Creates a new FolderInfoFileWrapper from a StoreInfo with the given path
71    * and parent. This is used for making relative paths to files, i.e.
72    * a child of '/foo' called 'bar/baz'. For StoreInfos, the parent should
73    * really always be null.
74    */

75   public FolderInfoFileWrapper(StoreInfo s, FolderInfoFileWrapper p, String JavaDoc filePath) {
76     super(s.getStoreID());
77     store = s;
78     parent = p;
79     path = filePath;
80     
81       if (p != null)
82     getLogger().info("creating FolderInfoFileWrapper from parent '" + p.getAbsolutePath() + "' called '" + filePath + "'");
83       else
84     getLogger().info("creating FolderInfoFileWrapper from parent 'null' called '" + filePath + "'");
85   }
86   
87   /**
88    * returns true.
89    */

90   public boolean canRead() {
91     return true;
92   }
93   
94   /**
95    * returns true.
96    */

97   public boolean canWrite() {
98     return true;
99   }
100   
101   /**
102    * Throws an IOException; use the folder subscription mechanism to
103    * subscribe to new folders.
104    */

105   public boolean createNewFile() throws IOException {
106     throw new IOException (Pooka.getProperty("error.folderinfofilewrapper.cantcreate", "Cannot create new Folders here. Use Subscribe instead."));
107   }
108   
109   
110   /**
111    * Attempts to delete the Folder. And fails--returns false always.
112    */

113   public boolean delete() {
114     return false;
115   }
116   
117   /**
118    * A no-op; we're not deleting any Mail folders on exit.
119    */

120   public void deleteOnExit() {
121   }
122   
123   /**
124    * Equals if the underlying Folder objects are equal.
125    */

126   public boolean equals(Object JavaDoc obj) {
127     if (obj instanceof FolderInfoFileWrapper) {
128       FolderInfoFileWrapper wrapper = ((FolderInfoFileWrapper) obj);
129       if (folder != null && wrapper.folder != null)
130     return (folder == wrapper.folder);
131       else if (store != null && wrapper.store != null)
132     return (store == wrapper.store);
133     }
134
135     return false;
136   }
137   
138   /**
139    * Always true for our purposes.
140    */

141   public boolean exists() {
142     return true;
143   }
144   
145   /**
146    * Returns this object.
147    */

148   public File getAbsoluteFile() {
149     
150       getLogger().info("calling getAbsoluteFile() on " + getAbsolutePath());
151     if (this.isAbsolute())
152       return this;
153     else {
154       if (store != null)
155     return new FolderInfoFileWrapper(store, getRoot(), getAbsolutePath());
156       else
157     return new FolderInfoFileWrapper(folder, getRoot(), getAbsolutePath());
158     }
159   }
160   
161   /**
162    * returns the root of this tree.
163    */

164   private FolderInfoFileWrapper getRoot() {
165     FolderInfoFileWrapper tmpParent = this;
166     while (tmpParent != null && tmpParent.getParent() != null && tmpParent.getParentFile() != tmpParent) {
167       tmpParent = (FolderInfoFileWrapper)tmpParent.getParentFile();
168     }
169     return tmpParent;
170   }
171   
172   /**
173    * Returns the Folder's full name. It does this recursively, by calling
174    * the this on the parent and then appending this name.
175    */

176   public String JavaDoc getAbsolutePath() {
177     
178       getLogger().info("calling getAbsolutePath() on " + path);
179     
180     if (isAbsolute()) {
181       
182     getLogger().info("returning " + getPath());
183       return getPath();
184     }
185     else {
186       if (parent != null) {
187     String JavaDoc parentPath = parent.getAbsolutePath();
188     if (parentPath != "/") {
189       
190         getLogger().info("returning parentPath + slash + getPath() (" + parentPath + "/" + getPath() + ")");
191       
192       return parentPath + "/" + getPath();
193     } else {
194       
195         getLogger().info("returning just slash + getPath() (/" + getPath() + ")");
196       return "/" + getPath();
197     }
198       } else {
199     
200       getLogger().info("returning just /.");
201     return "/";
202       }
203     }
204   }
205   
206   /**
207    * returns this.
208    */

209   public File getCanonicalFile() {
210     return this;
211   }
212   
213   /**
214    * returns getAbsolutePath();
215    */

216   public String JavaDoc getCanonicalPath() {
217     return getAbsolutePath();
218   }
219   
220   /**
221    * Returns the Folder's name.
222    */

223   public String JavaDoc getName() {
224     if (folder != null)
225       return folder.getFolderName();
226     else if (store != null)
227       return store.getStoreID();
228
229     return "null";
230   }
231   
232   /**
233    * Returns the parent's name.
234    */

235   public String JavaDoc getParent() {
236     if (parent != null)
237       return parent.getAbsolutePath();
238     else
239       return this.getAbsolutePath();
240   }
241   
242   /**
243    * This returns the parent Folder as a FolderInfoFileWrapper.
244    */

245   public File getParentFile() {
246     return parent;
247   }
248   
249   /**
250    * Returns the filePath variable.
251    */

252   public String JavaDoc getPath() {
253     return path;
254   }
255   
256   /**
257    * Returns true if this is an absolute reference, false otherwise.
258    */

259   public boolean isAbsolute() {
260     return (parent == null || parent == this);
261   }
262   
263   /**
264    * Tests to see if this can act as a directory.
265    */

266   public boolean isDirectory() {
267     if (store != null)
268       return true;
269     
270     if (folder != null) {
271       Vector JavaDoc v = folder.getChildren();
272       if (v != null)
273     return (v.size() > 0);
274     
275     }
276
277     return false;
278   }
279   
280   /**
281    * Tests to see if we should call this a File.
282    */

283   public boolean isFile() {
284     if (store != null)
285       return false;
286
287     return (folder.getType() & javax.mail.Folder.HOLDS_MESSAGES) != 0;
288   }
289   
290   /**
291    * Returns false.
292    */

293   public boolean isHidden() {
294     return false;
295   }
296   
297   /**
298    * Returns 0.
299    */

300   public long lastModified() {
301     return 0;
302   }
303   
304   /**
305    * returns the children of the File.
306    */

307   public String JavaDoc[] list() {
308     if (isDirectory()) {
309       if (children == null)
310     loadChildren();
311       if (children != null) {
312     String JavaDoc[] returnValue = new String JavaDoc[children.length];
313     for (int i = 0; i < children.length; i++) {
314       returnValue[i] = children[i].getName();
315     }
316     return returnValue;
317       }
318     }
319     
320     return null;
321     
322   }
323   
324   /**
325    * Returns the children of the File, filterd by the FilenameFilter.
326    */

327   public String JavaDoc[] list(FilenameFilter filter) {
328     String JavaDoc[] children = list();
329     String JavaDoc[] matching = new String JavaDoc[children.length];
330     int retValueCounter = 0;
331     for (int i = 0; i < children.length; i++) {
332       if (filter.accept(this, children[i])) {
333     matching[retValueCounter++] = children[i];
334       }
335     }
336     
337     String JavaDoc[] returnValue = new String JavaDoc[retValueCounter];
338     
339     for (int i = 0; i < retValueCounter; i++)
340       returnValue[i] = matching[i];
341     
342     return returnValue;
343   }
344   
345   /**
346    * This returns the children of the File as Files.
347    */

348   public File[] listFiles() {
349      {
350       getLogger().info("Running listFiles() on '" + getAbsolutePath() + "'");
351     }
352     
353     if (isDirectory()) {
354       if (children == null) {
355      {
356       getLogger().info("about to load children.");
357     }
358     loadChildren();
359       }
360       
361       if (children != null)
362     return children;
363     }
364     
365     return new FolderInfoFileWrapper[0];
366   }
367   
368   public File[] listFiles(FileFilter filter) {
369     
370       getLogger().info("Running listFiles(FileFilter) on '" + getAbsolutePath() + "'");
371     
372     File[] children = listFiles();
373     File[] matching = new File[children.length];
374     int retValueCounter = 0;
375     for (int i = 0; i < children.length; i++) {
376       if (filter.accept(children[i])) {
377     matching[retValueCounter++] = children[i];
378       }
379     }
380     
381     File[] returnValue = new File[retValueCounter];
382     
383     for (int i = 0; i < retValueCounter; i++)
384       returnValue[i] = matching[i];
385     
386     return returnValue;
387   }
388   
389   public File[] listFiles(FilenameFilter filter) {
390     
391       getLogger().info("Running listFiles(FilenameFilter) on '" + getAbsolutePath() + "'");
392     
393     File[] children = listFiles();
394     File[] matching = new File[children.length];
395     int retValueCounter = 0;
396     for (int i = 0; i < children.length; i++) {
397       if (filter.accept(this, children[i].getName())) {
398     matching[retValueCounter++] = children[i];
399       }
400     }
401     
402     File[] returnValue = new File[retValueCounter];
403     
404     for (int i = 0; i < retValueCounter; i++)
405       returnValue[i] = matching[i];
406     
407     return returnValue;
408   }
409   
410   /**
411    * This fails to create a new directory. (return false)
412    */

413   public boolean mkdir() {
414     return false;
415   }
416   
417   /**
418    * This fails to created a new directory. (return false)
419    */

420   public boolean mkdirs() {
421     return false;
422   }
423   
424   
425   /**
426    * Returns false.
427    */

428   public boolean renameTo(File dest) {
429     return false;
430   }
431   
432   /**
433    * This returns the wrapped FolderInfo.
434    */

435   public FolderInfo getFolderInfo() {
436     return folder;
437   }
438   
439   /**
440    * This returns the wrapped StoreInfo.
441    */

442   public StoreInfo getStoreInfo() {
443     return store;
444   }
445   
446   /**
447    * This loads the children of the Folder.
448    */

449   private synchronized void loadChildren() {
450      {
451       getLogger().info(Thread.currentThread().getName() + ": calling loadChildren() on " + getAbsolutePath());
452     }
453     
454     if (children == null) {
455        {
456     getLogger().info(Thread.currentThread().getName() + ": children is null on " + getAbsolutePath() + ". loading children.");
457       }
458
459       if (isDirectory()) {
460     
461       getLogger().info(Thread.currentThread().getName() + ": calling folder.list()");
462
463     Vector JavaDoc origChildren = null;
464     if (store != null)
465       origChildren = new Vector JavaDoc (store.getChildren());
466     else if (folder != null)
467       origChildren = new Vector JavaDoc (folder.getChildren());
468
469     if (origChildren == null)
470       origChildren = new Vector JavaDoc();
471
472     FolderInfoFileWrapper[] tmpChildren = new FolderInfoFileWrapper[origChildren.size()];
473     for (int i = 0; i < origChildren.size(); i++) {
474     
475       FolderInfo fi = (FolderInfo) origChildren.get(i);
476       tmpChildren[i] = new FolderInfoFileWrapper(fi, this);
477     }
478       
479     children = tmpChildren;
480       }
481     }
482     
483   }
484
485   /**
486    * This refreshes the children of the Folder.
487    */

488   public synchronized void refreshChildren() {
489      {
490       getLogger().info(Thread.currentThread().getName() + ": calling refreshChildren() on " + getAbsolutePath());
491       //Thread.dumpStack();
492
}
493     
494     if (children == null) {
495        {
496     getLogger().info(Thread.currentThread().getName() + ": children is null on " + getAbsolutePath() + ". calling loadChildren().");
497       }
498       loadChildren();
499     } else {
500       if (isDirectory()) {
501
502     Vector JavaDoc origChildren = null;
503     if (store != null)
504       origChildren = new Vector JavaDoc (store.getChildren());
505     else if (folder != null)
506       origChildren = new Vector JavaDoc (folder.getChildren());
507
508     if (origChildren == null)
509       origChildren = new Vector JavaDoc();
510
511     FolderInfoFileWrapper[] tmpChildren = new FolderInfoFileWrapper[origChildren.size()];
512     for (int i = 0; i < origChildren.size(); i++) {
513       
514       // yeah, this is n! or something like that. shouldn't matter--
515
// if we have somebody running with that many folders, or with
516
// that slow of a machine, we're in trouble anyway.
517
FolderInfo fi = (FolderInfo) origChildren.get(i);
518       
519       //try to get a match.
520
boolean found = false;
521       for (int j = 0; ! found && j < children.length; j++) {
522         if (children[j].getName().equals(fi.getFolderName())) {
523           tmpChildren[i] = children[j];
524           found = true;
525         }
526       }
527       
528       if (! found) {
529         tmpChildren[i] = new FolderInfoFileWrapper(fi, this);
530       }
531     }
532     
533     children = tmpChildren;
534       }
535     }
536     
537   }
538   
539   /* Only accepts relative filenames. */
540   public FolderInfoFileWrapper getFileByName(String JavaDoc filename) {
541     
542     
543       getLogger().info("calling getFileByName(" + filename + ") on folder " + getName() + " (" + getPath() + ") (abs " + getAbsolutePath() + ")");
544     
545     String JavaDoc origFilename = new String JavaDoc(filename);
546     if (filename == null || filename.length() < 1 || (filename.equals("/") && getParentFile() == null) || (filename.equals("/") && getParentFile() == this)) {
547       
548     getLogger().info("returning this for getFileByName()");
549       return this;
550     }
551     
552     if (this.isAbsolute(filename))
553       {
554     return null; // FIXME error
555
}
556     
557     // strip out the /'s
558

559     String JavaDoc subdirFile = null;
560     
561     int dirMarker = filename.indexOf('/');
562     while (dirMarker == 0) {
563       filename = filename.substring(1);
564       dirMarker = filename.indexOf('/');
565     }
566     
567     // divide into first component and rest of components
568
if (dirMarker > 0) {
569       subdirFile = filename.substring(dirMarker + 1);
570       filename=filename.substring(0, dirMarker);
571     }
572     
573     FolderInfoFileWrapper currentFile = getChildFile(filename);
574     if (currentFile != null && subdirFile != null) {
575       // recurse with rest of components
576
FolderInfoFileWrapper tmp = currentFile.getFileByName(subdirFile);
577       // tmp.path = origFilename;
578

579       
580     getLogger().info("created file " + tmp.getName() + " (" + tmp.getPath() + ") (abs " + tmp.getAbsolutePath() + ") from string " + origFilename + " on folder " + getName() + " (" + getPath() + ") (abs " + getAbsolutePath() + ")");
581       
582       return tmp;
583       
584     } else {
585       return currentFile;
586     }
587     
588   }
589   
590   FolderInfoFileWrapper getChildFile(String JavaDoc filename) {
591     
592       getLogger().info("calling getChildFile on " + getName() + " with filename " + filename);
593     
594     if (children == null)
595       loadChildren();
596     
597     if (children != null) {
598       for (int i = 0; i < children.length; i++) {
599     if (children[i].getName().equals(filename))
600       return children[i];
601       }
602
603     }
604
605     return this;
606     
607   }
608   
609   private boolean isAbsolute(String JavaDoc filename) {
610     return filename.startsWith("/");
611   }
612   
613   public String JavaDoc filenameAsRelativeToRoot(String JavaDoc filename) {
614     String JavaDoc relative = filename;
615     while (relative.length() > 0 & isAbsolute (relative)) {
616       relative = relative.substring(1);
617     }
618     
619     return relative;
620   }
621
622
623   /**
624    * Returns the Logger object for this class.
625    */

626   public java.util.logging.Logger JavaDoc getLogger() {
627     return java.util.logging.Logger.getLogger("Pooka.debug.gui.filechooser");
628   }
629   
630 }
631
Popular Tags