KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.suberic.pooka.gui.filechooser;
2 import javax.swing.*;
3 import javax.mail.*;
4 import java.io.*;
5 import net.suberic.pooka.Pooka;
6 import net.suberic.pooka.StoreInfo;
7 import java.util.Vector JavaDoc;
8
9 /**
10  * This class implements a FileSystemView based on the available Folders in
11  * a Store.
12  */

13
14 public class MailFileSystemView
15   extends javax.swing.filechooser.FileSystemView JavaDoc {
16   
17   StoreInfo[] storeList;
18   FolderFileWrapper[] roots = null;
19   
20   /**
21    * This creates a new MailFileSystemView at the top of the Store list;
22    * all available Stores will be listed, with their folders under them.
23    */

24   public MailFileSystemView() {
25     Vector JavaDoc v = Pooka.getStoreManager().getStoreList();
26     for (int i = v.size() - 1; i >= 0; i--) {
27       StoreInfo current = (StoreInfo) v.elementAt(i);
28       if (! current.isConnected())
29     v.removeElementAt(i);
30     }
31     
32     storeList = new StoreInfo[v.size()];
33     for (int i = 0; i < v.size(); i++) {
34       storeList[i] = (StoreInfo) v.elementAt(i);
35     }
36     
37     getRoots();
38   }
39   
40   /**
41    * This creates a MailFileSystemView out of a Store object.
42    */

43   public MailFileSystemView(StoreInfo newStore) {
44     storeList = new StoreInfo[] { newStore };
45     
46     getLogger().info("creating new MailFileSystemView for store " + newStore.getStoreID());
47     
48     try {
49       if (!newStore.isConnected()) {
50     
51     getLogger().info("store is not connected. connecting.");
52     newStore.connectStore();
53       }
54       getRoots();
55     } catch (MessagingException me) {
56       getLogger().info("caught messagingException : "
57              + me.getMessage());
58       me.printStackTrace();
59     }
60   }
61   
62   /**
63    * This creates a new Folder and FolderFileWrapper in the Folder
64    * corresponding to the directory dir with the name filename.
65    *
66    * @dir a FolderFileWrapper representing an IMAP folder.
67    * @filename a string representing an IMAP folder name.
68    */

69   public File createFileObject(File dir, String JavaDoc filename) {
70     if (dir != null)
71       getLogger().info("calling createFileObject on directory " + dir.getName() + " (" + dir.getPath() + "), filename " + filename);
72     else
73       getLogger().info("calling createFileObject on directory null, filename " + filename);
74     
75     
76     if (dir != null && dir instanceof FolderFileWrapper)
77       return ((FolderFileWrapper)dir).getFileByName(filename);
78     else
79       return null;
80   }
81   
82   /**
83    * @filename is an IMAP folder name.
84    */

85   public File createFileObject(String JavaDoc filename) {
86     // todo jph: strip off any leading directoy separators. we
87
// want to call getFileByName with a relative path (in this case
88
// to the root directory) always.
89

90     
91     getLogger().info("running createFileObject2 on filename '" + filename + "'");
92     
93     if (roots == null || roots.length == 0) {
94       
95       getLogger().info("root == null");
96       return null;
97     }
98     
99     
100     getLogger().info("root != null");
101     
102     if (filename.equals("/") || filename.equals("")) {
103       return roots[0];
104     }
105     
106     int firstSlash = filename.indexOf('/');
107     String JavaDoc storeName = null;
108     String JavaDoc filePart = "";
109     if (firstSlash > -1) {
110       storeName = filename.substring(0, firstSlash);
111       
112     getLogger().info("store name is " + storeName);
113       if (firstSlash < filename.length()) {
114     filePart = filename.substring(firstSlash + 1);
115     
116       getLogger().info("file name is " + filePart);
117       }
118     } else {
119       
120     getLogger().info("store name is " + filename);
121       
122       storeName = filename;
123     }
124     
125     FolderFileWrapper currentRoot = findRoot(storeName);
126     if (currentRoot == null) {
127       
128     getLogger().info("found no matching store root for " + storeName + ".");
129       return new File(filename);
130     }
131     
132     File returnValue = currentRoot.getFileByName(filePart);
133     return returnValue;
134     
135   }
136   
137   /**
138    * Creates a new Folder under the containingDir.
139    */

140   public File createNewFolder(File containingDir) {
141     
142       getLogger().info("running createNewFolder.");
143     
144     try {
145       Folder parentFolder = null;
146       if (containingDir instanceof FolderFileWrapper) {
147     parentFolder = ((FolderFileWrapper)containingDir).getFolder();
148     
149     Folder newFolder = parentFolder.getFolder("New_folder");
150     for (int i = 1; newFolder.exists(); i++) {
151       newFolder=parentFolder.getFolder("New_folder_" + i);
152     }
153     
154     newFolder.create(Folder.HOLDS_FOLDERS);
155
156     ((FolderFileWrapper) containingDir).refreshChildren();
157
158     //return new FolderFileWrapper(newFolder, (FolderFileWrapper)containingDir);
159
return ((FolderFileWrapper)containingDir).getFileByName(newFolder.getName());
160       } else {
161     return null;
162
163     //parentFolder = store.getFolder(containingDir.getAbsolutePath());
164
}
165     } catch (MessagingException me) {
166       Pooka.getUIFactory().showError(Pooka.getProperty("error.creatingFolder", "Error creating folder: "), me);
167     }
168     
169     return null;
170   }
171
172   /**
173    * Gets the child for the file.
174    */

175   public File getChild(File parent, String JavaDoc filename) {
176     if (parent instanceof FolderFileWrapper) {
177       return ((FolderFileWrapper) parent).getChildFile(filename);
178     } else {
179       return new File(parent, filename);
180     }
181   }
182
183   /**
184    * Gets the default starting directory for the file chooser.
185    */

186   public File getDefaultDirectory() {
187     return getDefaultRoot();
188   }
189
190   /**
191    * Returns all of the files under a particular directory.
192    */

193   public File[] getFiles(File dir, boolean useFileHiding) {
194     
195       getLogger().info("running getFiles " + dir + ", " + useFileHiding + ".");
196     
197     if (dir instanceof FolderFileWrapper) {
198       
199     getLogger().info("getFiles: returning dir.listFiles()");
200       return ((FolderFileWrapper)dir).listFiles();
201     } else {
202       
203     getLogger().info("getFiles: dir isn't a FFW.");
204       if (dir == null) {
205     
206       getLogger().info("getFiles: dir is null; returning null.");
207     return null; // FIXME: or set dir to root?
208
}
209       
210       // FIXME: ugly?
211

212       
213     getLogger().info("getFiles: just returning the root.");
214       
215       File f = ((FolderFileWrapper)getDefaultRoot()).getFileByName(dir.getAbsolutePath());
216       
217       if (f == null) {
218     
219       getLogger().info("getFiles: tried returning the root, but got null. returning the root itself instead.");
220     return new FolderFileWrapper[0];
221       }
222       
223       
224     getLogger().info("getFiles: returning " + f + ".listFiles() for getFiles()");
225       return f.listFiles();
226     }
227   }
228   
229     /**
230      * Returns the user's home directory. Kind of a strange thing
231      * on a mail system...
232      */

233     public File getHomeDirectory() {
234     
235         getLogger().info("running getHomeDirectory().");
236
237     return getDefaultRoot();
238     }
239
240     /**
241      * Returns the parent directory of the current File.
242      */

243     public File getParentDirectory(File dir) {
244     
245         getLogger().info("running getParentDirectory on " + dir);
246
247     if (dir == null)
248         return null; // at root
249

250     if (! (dir instanceof FolderFileWrapper)) {
251         if (roots != null && roots.length > 0) {
252         dir = createFileObject(dir.getPath());
253         } else
254         return null; // FIXME error?
255

256     }
257     if (dir == null)
258         return null; // at root
259

260     return dir.getParentFile();
261     }
262
263     /**
264      * Gets all the roots for this MailFileSystemView.
265      */

266     public File[] getRoots() {
267     
268         getLogger().info("calling getRoots() on MailFileSystemView.");
269
270     if (roots != null) {
271         
272         getLogger().info("root has already been set.");
273         return roots;
274     }
275     try {
276         
277         getLogger().info("setting folder f to store.getDefaultFolder().");
278         roots = new FolderFileWrapper[storeList.length];
279         for (int i = 0; i < storeList.length; i++) {
280           synchronized(storeList[i].getStoreThread().getRunLock()) {
281         Folder f = storeList[i].getStore().getDefaultFolder();
282         roots[i] = new FolderFileWrapper(f, storeList[i].getStoreID(), storeList[i].getStoreThread().getRunLock());
283           }
284         }
285         return roots;
286     } catch (MessagingException me) {
287         return null; // FIXME: throw this on
288
}
289     }
290
291   /**
292    * always returns false for now.
293    */

294   public boolean isHiddenFile(File f) {
295     return false;
296   }
297   
298   /**
299    * returns true for all files in the roots array.
300    */

301   public boolean isRoot(File f) {
302     if (f.getParentFile() == null)
303       return true;
304     else
305       return false;
306   }
307   
308   /**
309    * Returns true if the directory is traversable.
310    */

311   public Boolean JavaDoc isTraversable(File f) {
312     if (f != null && f instanceof FolderFileWrapper) {
313       if (((FolderFileWrapper) f).isDirectory())
314     return new Boolean JavaDoc(true);
315       else
316     return new Boolean JavaDoc(false);
317     } else
318       return new Boolean JavaDoc(false);
319   }
320
321   /*
322    * Checks if <code>f</code> represents a real directory or file as opposed to a
323    * special folder such as <code>"Desktop"</code>. Used by UI classes to decide if
324    * a folder is selectable when doing directory choosing.
325    *
326    * @param f a <code>File</code> object
327    * @return <code>true</code> if <code>f</code> is a real file or directory.
328    */

329   public boolean isFileSystem(File f) {
330     return true;
331   }
332   
333   /*
334    * Is dir the root of a tree in the file system, such as a drive
335    * or partition. Example: Returns true for "C:\" on Windows 98.
336    *
337    * @See also isRoot
338    */

339   public boolean isFileSystemRoot(File dir) {
340     return isRoot(dir);
341   }
342   
343   /*
344    * Used by UI classes to decide whether to display a special icon
345    * for drives or partitions, e.g. a "hard disk" icon.
346    *
347    * The default implementation has no way of knowing, so always returns false.
348    *
349    * @param dir a directory
350    * @return <code>false</code> always
351      */

352   public boolean isDrive(File dir) {
353     return false;
354   }
355
356   /*
357    * Used by UI classes to decide whether to display a special icon
358    * for a floppy disk. Implies isDrive(dir).
359    *
360    * The default implementation has no way of knowing, so always returns false.
361    *
362    * @param dir a directory
363    * @return <code>false</code> always
364    */

365   public boolean isFloppyDrive(File dir) {
366     return false;
367   }
368   
369   /*
370    * Used by UI classes to decide whether to display a special icon
371    * for a computer node, e.g. "My Computer" or a network server.
372    *
373    * The default implementation has no way of knowing, so always returns false.
374    *
375    * @param dir a directory
376    * @return <code>false</code> always
377    */

378   public boolean isComputerNode(File dir) {
379     return false;
380   }
381
382   
383   /**
384    * On Windows, a file can appear in multiple folders, other than its
385    * parent directory in the filesystem. Folder could for example be the
386    * "Desktop" folder which is not the same as file.getParentFile().
387    *
388    * @param folder a <code>File</code> object repesenting a directory or special folder
389    * @param file a <code>File</code> object
390    * @return <code>true</code> if <code>folder</code> is a directory or special folder and contains <code>file</code>.
391    */

392   public boolean isParent(File folder, File file) {
393     if (folder == null || file == null) {
394       return false;
395     } else {
396       return folder.equals(file.getParentFile());
397     }
398   }
399   
400   /**
401    * Type description for a file, directory, or folder as it would be displayed in
402    * a system file browser. Example from Windows: the "Desktop" folder
403    * is desribed as "Desktop".
404    *
405    * The Windows implementation gets information from the ShellFolder class.
406    */

407   public String JavaDoc getSystemTypeDescription(File f) {
408     if (f != null) {
409       return ("mail folder");
410     } else {
411       return null;
412     }
413   }
414
415    /**
416      * Name of a file, directory, or folder as it would be displayed in
417      * a system file browser. Example from Windows: the "M:\" directory
418      * displays as "CD-ROM (M:)"
419      *
420      * The default implementation gets information from the ShellFolder class.
421      *
422      * @param f a <code>File</code> object
423      * @return the file name as it would be displayed by a native file chooser
424      * @see JFileChooser#getName
425      */

426     public String JavaDoc getSystemDisplayName(File f) {
427       String JavaDoc name = null;
428       if (f != null) {
429     name = f.getName();
430       }
431       return name;
432     }
433
434   /**
435    * Icon for a file, directory, or folder as it would be displayed in
436    * a system file browser. Example from Windows: the "M:\" directory
437    * displays a CD-ROM icon.
438    *
439    * The default implementation gets information from the ShellFolder class.
440    *
441    * @param f a <code>File</code> object
442    * @return an icon as it would be displayed by a native file chooser
443    * @see JFileChooser#getIcon
444    */

445   public Icon getSystemIcon(File f) {
446     if (f != null) {
447       return UIManager.getIcon(f.isDirectory() ? "FileView.directoryIcon" : "FileView.fileIcon");
448     } else {
449       return null;
450     }
451   }
452   
453   
454   /* Not inherited. */
455   
456   public File getDefaultRoot() {
457     if (roots == null)
458       {
459     File[] localRoots = getRoots();
460     if (localRoots != null && localRoots.length > 0)
461       return localRoots[0];
462     else
463       return null;
464       }
465     return roots[0];
466   }
467   
468   /**
469    * This finds the Root with the given name, if any.
470    */

471     public FolderFileWrapper findRoot(String JavaDoc name) {
472       for (int i = 0; i < roots.length; i++) {
473     if (roots[i].getPath().equals(name))
474       return roots[i];
475       }
476       return null;
477     }
478
479   /**
480    * Returns the Logger object for this class.
481    */

482   public java.util.logging.Logger JavaDoc getLogger() {
483     return java.util.logging.Logger.getLogger("Pooka.debug.gui.filechooser");
484   }
485
486 }
487
488
Popular Tags