KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

24   public PookaFileSystemView() {
25     Vector JavaDoc v = Pooka.getStoreManager().getStoreList();
26     
27     storeList = new StoreInfo[v.size()];
28     for (int i = 0; i < v.size(); i++) {
29       storeList[i] = (StoreInfo) v.elementAt(i);
30     }
31     
32     getRoots();
33   }
34   
35   /**
36    * This creates a PookaFileSystemView out of a Store object.
37    */

38   public PookaFileSystemView(StoreInfo newStore) {
39     storeList = new StoreInfo[] { newStore };
40     getLogger().info("creating new PookaFileSystemView for store " + newStore.getStoreID());
41     
42     getRoots();
43   }
44   
45   /**
46    * This creates a new Folder and FolderInfoFileWrapper in the Folder
47    * corresponding to the directory dir with the name filename.
48    *
49    * @dir a FolderInfoFileWrapper representing either a FolderInfo or
50    * a StoreInfo
51    * @filename a string representing a FolderInfo name.
52    */

53   public File createFileObject(File dir, String JavaDoc filename) {
54     if (dir != null)
55       getLogger().info("calling createFileObject on directory " + dir.getName() + " (" + dir.getPath() + "), filename " + filename);
56     else
57       getLogger().info("calling createFileObject on directory null, filename " + filename);
58     
59     if (dir != null && dir instanceof FolderInfoFileWrapper)
60       return ((FolderInfoFileWrapper)dir).getFileByName(filename);
61     else
62       return null;
63   }
64   
65   /**
66    * @filename is an IMAP folder name.
67    */

68   public File createFileObject(String JavaDoc filename) {
69     
70     // todo jph: strip off any leading directoy separators. we
71
// want to call getFileByName with a relative path (in this case
72
// to the root directory) always.
73

74     getLogger().info("running createFileObject2 on filename '" + filename + "'");
75     
76     if (roots == null || roots.length == 0) {
77       getLogger().info("root == null");
78       return null;
79     }
80     
81     getLogger().info("root != null");
82     
83     if (filename.equals("/") || filename.equals("")) {
84       return roots[0];
85     }
86     
87     int firstSlash = filename.indexOf('/');
88     String JavaDoc storeName = null;
89     String JavaDoc filePart = "";
90     if (firstSlash > -1) {
91       storeName = filename.substring(0, firstSlash);
92       getLogger().info("store name is " + storeName);
93       if (firstSlash < filename.length()) {
94     filePart = filename.substring(firstSlash + 1);
95     getLogger().info("file name is " + filePart);
96       }
97     } else {
98       getLogger().info("store name is " + filename);
99       
100       storeName = filename;
101     }
102     
103     getLogger().info("store name is " + filename + ", file name is " + filePart);
104
105     FolderInfoFileWrapper currentRoot = findRoot(storeName);
106     if (currentRoot == null) {
107       getLogger().info("found no matching store root for " + storeName + ".");
108       return new File(filename);
109     }
110     
111     File returnValue = currentRoot.getFileByName(filePart);
112
113     getLogger().info("returning " + returnValue + " for " + currentRoot + ".getFileByName(\"" + filePart + "\")");
114     
115     return returnValue;
116     
117   }
118   
119   /**
120    * Creates a new File object for f with correct behavior for a file system
121    * root directory.
122    */

123   /*
124   protected File createFileSystemRoot(File f) {
125     
126   }
127   */

128
129   /**
130    * Creates a new Folder under the containingDir.
131    */

132   public File createNewFolder(File containingDir) throws java.io.IOException JavaDoc {
133     throw new IOException (Pooka.getProperty("error.folderinfofilewrapper.cantcreate", "Cannot create new Folders here. Use Subscribe instead."));
134
135   }
136
137   /**
138    * Gets the child for the file.
139    */

140   public File getChild(File parent, String JavaDoc filename) {
141     if (parent instanceof FolderInfoFileWrapper) {
142       return ((FolderInfoFileWrapper) parent).getChildFile(filename);
143     } else {
144       return new File(parent, filename);
145     }
146   }
147
148   /**
149    * Gets the default starting directory for the file chooser.
150    */

151   public File getDefaultDirectory() {
152     getLogger().info("calling getDefaultDirectory()");
153
154     return getDefaultRoot();
155   }
156
157   /**
158    * Returns all of the files under a particular directory.
159    */

160   public File[] getFiles(File dir, boolean useFileHiding) {
161     getLogger().info("running getFiles " + dir + ", " + useFileHiding + ".");
162     
163     if (dir instanceof FolderInfoFileWrapper) {
164       getLogger().info("getFiles: returning dir.listFiles()");
165       return ((FolderInfoFileWrapper)dir).listFiles();
166     } else {
167       getLogger().info("getFiles: dir isn't a FFW.");
168       if (dir == null) {
169     getLogger().info("getFiles: dir is null; returning null.");
170     return null; // FIXME: or set dir to root?
171
}
172       
173       // FIXME: ugly?
174

175       getLogger().info("getFiles: just returning the root.");
176       
177       File f = ((FolderInfoFileWrapper)getDefaultRoot()).getFileByName(dir.getAbsolutePath());
178       
179       if (f == null) {
180     getLogger().info("getFiles: tried returning the root, but got null. returning the root itself instead.");
181     return new FolderInfoFileWrapper[0];
182       }
183       
184       getLogger().info("getFiles: returning " + f + ".listFiles() for getFiles()");
185       return f.listFiles();
186     }
187   }
188   
189   /**
190    * Returns the user's home directory. Kind of a strange thing
191    * on a mail system...
192    */

193   public File getHomeDirectory() {
194     getLogger().info("running getHomeDirectory().");
195     
196     return getDefaultRoot();
197   }
198   
199   /**
200    * Returns the parent directory of the current File.
201    */

202   public File getParentDirectory(File dir) {
203     getLogger().info("running getParentDirectory on " + dir);
204     
205     if (dir == null)
206       return null; // at root
207

208     if (! (dir instanceof FolderInfoFileWrapper)) {
209       if (roots != null && roots.length > 0) {
210     dir = createFileObject(dir.getPath());
211       } else
212     return null; // FIXME error?
213

214     }
215     if (dir == null)
216       return null; // at root
217
File returnValue = dir.getParentFile();
218
219     return dir.getParentFile();
220   }
221   
222   /**
223    * Gets all the roots for this PookaFileSystemView.
224    */

225     public File[] getRoots() {
226       getLogger().info("calling getRoots() on PookaFileSystemView.");
227
228       if (roots != null) {
229     getLogger().info
230       ("root has already been set.");
231     return roots;
232       }
233
234       getLogger().info("setting folder f to store.getDefaultFolder().");
235       roots = new FolderInfoFileWrapper[storeList.length];
236       for (int i = 0; i < storeList.length; i++) {
237     roots[i] = new FolderInfoFileWrapper(storeList[i], null, storeList[i].getStoreID());
238       }
239       return roots;
240     }
241   
242   /**
243    * always returns false for now.
244    */

245   public boolean isHiddenFile(File f) {
246     return false;
247   }
248   
249   /**
250    * returns true for all files in the roots array.
251    */

252   public boolean isRoot(File f) {
253     if (f.getParentFile() == null || f.getParentFile() == f)
254       return true;
255     else
256       return false;
257   }
258   
259   
260   /**
261    * Returns true if the directory is traversable.
262    */

263   public Boolean JavaDoc isTraversable(File f) {
264     getLogger().info("pfsv: checking isTraversable on file " + f);
265     
266     if (f != null && f instanceof FolderInfoFileWrapper) {
267       if (((FolderInfoFileWrapper) f).isDirectory())
268     return new Boolean JavaDoc(true);
269       else
270     return new Boolean JavaDoc(false);
271     } else {
272       return new Boolean JavaDoc(false);
273     }
274   }
275   
276   /*
277    * Used by UI classes to decide whether to display a special icon
278    * for drives or partitions, e.g. a "hard disk" icon.
279    *
280    * The default implementation has no way of knowing, so always returns false.
281    *
282    * @param dir a directory
283    * @return <code>false</code> always
284      */

285   public boolean isDrive(File dir) {
286     return false;
287   }
288
289   /*
290    * Used by UI classes to decide whether to display a special icon
291    * for a floppy disk. Implies isDrive(dir).
292    *
293    * The default implementation has no way of knowing, so always returns false.
294    *
295    * @param dir a directory
296    * @return <code>false</code> always
297    */

298   public boolean isFloppyDrive(File dir) {
299     return false;
300   }
301   
302   /*
303    * Used by UI classes to decide whether to display a special icon
304    * for a computer node, e.g. "My Computer" or a network server.
305    *
306    * The default implementation has no way of knowing, so always returns false.
307    *
308    * @param dir a directory
309    * @return <code>false</code> always
310    */

311   public boolean isComputerNode(File dir) {
312     return false;
313   }
314
315   
316   /**
317    * On Windows, a file can appear in multiple folders, other than its
318    * parent directory in the filesystem. Folder could for example be the
319    * "Desktop" folder which is not the same as file.getParentFile().
320    *
321    * @param folder a <code>File</code> object repesenting a directory or special folder
322    * @param file a <code>File</code> object
323    * @return <code>true</code> if <code>folder</code> is a directory or special folder and contains <code>file</code>.
324    */

325   public boolean isParent(File folder, File file) {
326     if (folder == null || file == null) {
327       return false;
328     } else {
329       return folder.equals(file.getParentFile());
330     }
331   }
332   
333   /**
334    * Type description for a file, directory, or folder as it would be displayed in
335    * a system file browser. Example from Windows: the "Desktop" folder
336    * is desribed as "Desktop".
337    *
338    * The Windows implementation gets information from the ShellFolder class.
339    */

340   public String JavaDoc getSystemTypeDescription(File f) {
341     if (f != null) {
342       return ("mail folder");
343     } else {
344       return null;
345     }
346   }
347
348    /**
349      * Name of a file, directory, or folder as it would be displayed in
350      * a system file browser. Example from Windows: the "M:\" directory
351      * displays as "CD-ROM (M:)"
352      *
353      * The default implementation gets information from the ShellFolder class.
354      *
355      * @param f a <code>File</code> object
356      * @return the file name as it would be displayed by a native file chooser
357      * @see JFileChooser#getName
358      */

359     public String JavaDoc getSystemDisplayName(File f) {
360       String JavaDoc name = null;
361       if (f != null) {
362     name = f.getName();
363       }
364       return name;
365     }
366
367   /**
368    * Icon for a file, directory, or folder as it would be displayed in
369    * a system file browser. Example from Windows: the "M:\" directory
370    * displays a CD-ROM icon.
371    *
372    * The default implementation gets information from the ShellFolder class.
373    *
374    * @param f a <code>File</code> object
375    * @return an icon as it would be displayed by a native file chooser
376    * @see JFileChooser#getIcon
377    */

378   public Icon getSystemIcon(File f) {
379     if (f != null) {
380       return UIManager.getIcon(f.isDirectory() ? "FileView.directoryIcon" : "FileView.fileIcon");
381     } else {
382       return null;
383     }
384   }
385   
386
387   /* Not inherited. */
388   
389   public File getDefaultRoot() {
390     getLogger().info("running getDefaultRoot().");
391
392     if (roots == null) {
393       File[] localRoots = getRoots();
394       if (localRoots != null && localRoots.length > 0)
395     return localRoots[0];
396       else
397     return null;
398     }
399     
400     getLogger().info("returning " + roots[0] + " as default root.");
401
402     return roots[0];
403   }
404   
405   /**
406    * This finds the Root with the given name, if any.
407    */

408   public FolderInfoFileWrapper findRoot(String JavaDoc name) {
409     for (int i = 0; i < roots.length; i++) {
410       if (roots[i].getPath().equals(name))
411     return roots[i];
412     }
413     return null;
414   }
415
416   /**
417    * Returns the Logger object for this class.
418    */

419   public java.util.logging.Logger JavaDoc getLogger() {
420     return java.util.logging.Logger.getLogger("Pooka.debug.gui.filechooser");
421   }
422 }
423
Popular Tags