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 ; 8 9 13 14 public class MailFileSystemView 15 extends javax.swing.filechooser.FileSystemView { 16 17 StoreInfo[] storeList; 18 FolderFileWrapper[] roots = null; 19 20 24 public MailFileSystemView() { 25 Vector 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 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 69 public File createFileObject(File dir, String 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 85 public File createFileObject(String filename) { 86 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 storeName = null; 108 String 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 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 ((FolderFileWrapper)containingDir).getFileByName(newFolder.getName()); 160 } else { 161 return null; 162 163 } 165 } catch (MessagingException me) { 166 Pooka.getUIFactory().showError(Pooka.getProperty("error.creatingFolder", "Error creating folder: "), me); 167 } 168 169 return null; 170 } 171 172 175 public File getChild(File parent, String filename) { 176 if (parent instanceof FolderFileWrapper) { 177 return ((FolderFileWrapper) parent).getChildFile(filename); 178 } else { 179 return new File(parent, filename); 180 } 181 } 182 183 186 public File getDefaultDirectory() { 187 return getDefaultRoot(); 188 } 189 190 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; } 209 210 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 233 public File getHomeDirectory() { 234 235 getLogger().info("running getHomeDirectory()."); 236 237 return getDefaultRoot(); 238 } 239 240 243 public File getParentDirectory(File dir) { 244 245 getLogger().info("running getParentDirectory on " + dir); 246 247 if (dir == null) 248 return null; 250 if (! (dir instanceof FolderFileWrapper)) { 251 if (roots != null && roots.length > 0) { 252 dir = createFileObject(dir.getPath()); 253 } else 254 return null; 256 } 257 if (dir == null) 258 return null; 260 return dir.getParentFile(); 261 } 262 263 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; } 289 } 290 291 294 public boolean isHiddenFile(File f) { 295 return false; 296 } 297 298 301 public boolean isRoot(File f) { 302 if (f.getParentFile() == null) 303 return true; 304 else 305 return false; 306 } 307 308 311 public Boolean isTraversable(File f) { 312 if (f != null && f instanceof FolderFileWrapper) { 313 if (((FolderFileWrapper) f).isDirectory()) 314 return new Boolean (true); 315 else 316 return new Boolean (false); 317 } else 318 return new Boolean (false); 319 } 320 321 329 public boolean isFileSystem(File f) { 330 return true; 331 } 332 333 339 public boolean isFileSystemRoot(File dir) { 340 return isRoot(dir); 341 } 342 343 352 public boolean isDrive(File dir) { 353 return false; 354 } 355 356 365 public boolean isFloppyDrive(File dir) { 366 return false; 367 } 368 369 378 public boolean isComputerNode(File dir) { 379 return false; 380 } 381 382 383 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 407 public String getSystemTypeDescription(File f) { 408 if (f != null) { 409 return ("mail folder"); 410 } else { 411 return null; 412 } 413 } 414 415 426 public String getSystemDisplayName(File f) { 427 String name = null; 428 if (f != null) { 429 name = f.getName(); 430 } 431 return name; 432 } 433 434 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 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 471 public FolderFileWrapper findRoot(String 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 482 public java.util.logging.Logger getLogger() { 483 return java.util.logging.Logger.getLogger("Pooka.debug.gui.filechooser"); 484 } 485 486 } 487 488 | Popular Tags |