1 package net.suberic.pooka.gui.filechooser; 2 import javax.swing.*; 3 import java.io.*; 4 import java.util.Vector ; 5 import net.suberic.pooka.*; 6 7 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 path; 16 17 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 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 57 public FolderInfoFileWrapper(FolderInfo f, FolderInfoFileWrapper p, String 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 75 public FolderInfoFileWrapper(StoreInfo s, FolderInfoFileWrapper p, String 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 90 public boolean canRead() { 91 return true; 92 } 93 94 97 public boolean canWrite() { 98 return true; 99 } 100 101 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 113 public boolean delete() { 114 return false; 115 } 116 117 120 public void deleteOnExit() { 121 } 122 123 126 public boolean equals(Object 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 141 public boolean exists() { 142 return true; 143 } 144 145 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 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 176 public String 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 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 209 public File getCanonicalFile() { 210 return this; 211 } 212 213 216 public String getCanonicalPath() { 217 return getAbsolutePath(); 218 } 219 220 223 public String 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 235 public String getParent() { 236 if (parent != null) 237 return parent.getAbsolutePath(); 238 else 239 return this.getAbsolutePath(); 240 } 241 242 245 public File getParentFile() { 246 return parent; 247 } 248 249 252 public String getPath() { 253 return path; 254 } 255 256 259 public boolean isAbsolute() { 260 return (parent == null || parent == this); 261 } 262 263 266 public boolean isDirectory() { 267 if (store != null) 268 return true; 269 270 if (folder != null) { 271 Vector v = folder.getChildren(); 272 if (v != null) 273 return (v.size() > 0); 274 275 } 276 277 return false; 278 } 279 280 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 293 public boolean isHidden() { 294 return false; 295 } 296 297 300 public long lastModified() { 301 return 0; 302 } 303 304 307 public String [] list() { 308 if (isDirectory()) { 309 if (children == null) 310 loadChildren(); 311 if (children != null) { 312 String [] returnValue = new String [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 327 public String [] list(FilenameFilter filter) { 328 String [] children = list(); 329 String [] matching = new String [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 [] returnValue = new String [retValueCounter]; 338 339 for (int i = 0; i < retValueCounter; i++) 340 returnValue[i] = matching[i]; 341 342 return returnValue; 343 } 344 345 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 413 public boolean mkdir() { 414 return false; 415 } 416 417 420 public boolean mkdirs() { 421 return false; 422 } 423 424 425 428 public boolean renameTo(File dest) { 429 return false; 430 } 431 432 435 public FolderInfo getFolderInfo() { 436 return folder; 437 } 438 439 442 public StoreInfo getStoreInfo() { 443 return store; 444 } 445 446 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 origChildren = null; 464 if (store != null) 465 origChildren = new Vector (store.getChildren()); 466 else if (folder != null) 467 origChildren = new Vector (folder.getChildren()); 468 469 if (origChildren == null) 470 origChildren = new Vector (); 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 488 public synchronized void refreshChildren() { 489 { 490 getLogger().info(Thread.currentThread().getName() + ": calling refreshChildren() on " + getAbsolutePath()); 491 } 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 origChildren = null; 503 if (store != null) 504 origChildren = new Vector (store.getChildren()); 505 else if (folder != null) 506 origChildren = new Vector (folder.getChildren()); 507 508 if (origChildren == null) 509 origChildren = new Vector (); 510 511 FolderInfoFileWrapper[] tmpChildren = new FolderInfoFileWrapper[origChildren.size()]; 512 for (int i = 0; i < origChildren.size(); i++) { 513 514 FolderInfo fi = (FolderInfo) origChildren.get(i); 518 519 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 540 public FolderInfoFileWrapper getFileByName(String filename) { 541 542 543 getLogger().info("calling getFileByName(" + filename + ") on folder " + getName() + " (" + getPath() + ") (abs " + getAbsolutePath() + ")"); 544 545 String origFilename = new String (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; } 556 557 559 String subdirFile = null; 560 561 int dirMarker = filename.indexOf('/'); 562 while (dirMarker == 0) { 563 filename = filename.substring(1); 564 dirMarker = filename.indexOf('/'); 565 } 566 567 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 FolderInfoFileWrapper tmp = currentFile.getFileByName(subdirFile); 577 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 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 filename) { 610 return filename.startsWith("/"); 611 } 612 613 public String filenameAsRelativeToRoot(String filename) { 614 String relative = filename; 615 while (relative.length() > 0 & isAbsolute (relative)) { 616 relative = relative.substring(1); 617 } 618 619 return relative; 620 } 621 622 623 626 public java.util.logging.Logger getLogger() { 627 return java.util.logging.Logger.getLogger("Pooka.debug.gui.filechooser"); 628 } 629 630 } 631 | Popular Tags |