1 19 20 21 package org.netbeans.modules.group; 22 23 import java.io.BufferedReader ; 24 import java.io.BufferedWriter ; 25 import java.io.File ; 26 import java.io.IOException ; 27 import java.io.InputStreamReader ; 28 import java.io.OutputStreamWriter ; 29 import java.text.MessageFormat ; 30 import java.util.ArrayList ; 31 import java.util.HashSet ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 import java.util.Set ; 35 import org.openide.ErrorManager; 36 import org.openide.filesystems.FileChangeAdapter; 37 import org.openide.filesystems.FileEvent; 38 import org.openide.filesystems.FileLock; 39 import org.openide.filesystems.FileObject; 40 import org.openide.filesystems.FileUtil; 41 import org.openide.loaders.DataFolder; 42 import org.openide.loaders.DataLoader; 43 import org.openide.loaders.DataObject; 44 import org.openide.loaders.DataObjectExistsException; 45 import org.openide.loaders.TemplateWizard; 46 import org.openide.nodes.Node; 47 import org.openide.nodes.Node.Cookie; 48 import org.openide.util.HelpCtx; 49 import org.openide.util.NbBundle; 50 51 88 public class GroupShadow extends DataObject 89 implements DataObject.Container { 90 91 92 static final long serialVersionUID =-5086491126656157958L; 93 94 95 public static final String GS_EXTENSION = GroupShadowLoader.GS_EXTENSION; 96 97 98 public static final String PROP_SHOW_LINKS = "showlinks"; 100 101 public static final String PROP_TEMPLATE_ALL = "templateall"; 103 104 public static final String PROP_TEMPLATE_PATTERN = "templatepattern"; 106 107 public static final String PROP_USE_PATTERN = "usepattern"; 109 110 static boolean showLinks = true; 111 112 private static TemplateWizard.Iterator groupTemplateIterator = null; 114 115 116 private GroupShadow gsprocessed = null; 117 118 119 private static synchronized TemplateWizard.Iterator 120 getGroupTemplateIterator() { 121 if (groupTemplateIterator == null) { 122 groupTemplateIterator = new GroupTemplateIterator(); 123 } 124 return groupTemplateIterator; 125 } 126 127 128 134 public GroupShadow(final FileObject fo, DataLoader dl) 135 throws DataObjectExistsException, 136 IllegalArgumentException , 137 IOException { 138 super(fo, dl); 139 fo.addFileChangeListener( 140 new FileChangeAdapter() { 141 public void fileChanged(FileEvent e) { GroupShadow.this.primaryFileChanged(); 143 } 144 }); 145 } 146 147 148 149 protected Node createNodeDelegate() { 150 GroupNode node = new GroupNode(this, new GroupNodeChildren(this)); 151 addPropertyChangeListener(node); 152 return node; 153 } 154 155 157 public boolean isDeleteAllowed () { 158 return !getPrimaryFile ().isReadOnly (); 159 } 160 161 163 public boolean isCopyAllowed () { 164 return true; 165 } 166 167 169 public boolean isMoveAllowed () { 170 return !getPrimaryFile ().isReadOnly (); 171 } 172 173 175 public boolean isRenameAllowed () { 176 return !getPrimaryFile ().isReadOnly (); 177 } 178 179 188 protected DataObject handleCopy (DataFolder f) throws IOException { 189 return handleCopy(f, getName()); 190 } 191 192 203 protected DataObject handleCopy (DataFolder f, 204 String name) throws IOException { 205 String newname = FileUtil.findFreeFileName(f.getPrimaryFile(), 206 name, 207 GS_EXTENSION); 208 FileObject fo = FileUtil.copyFile(getPrimaryFile(), 209 f.getPrimaryFile(), 210 newname); 211 216 217 return new GroupShadow(fo, getLoader()); 218 } 219 220 223 protected void handleDelete () throws IOException { 224 FileLock lock = getPrimaryFile ().lock (); 225 try { 226 getPrimaryFile ().delete (lock); 227 } finally { 228 lock.releaseLock (); 229 } 230 } 231 232 235 protected FileObject handleRename (String name) throws IOException { 236 FileLock lock = getPrimaryFile ().lock (); 237 try { 238 getPrimaryFile ().rename (lock, name, GS_EXTENSION); 239 } finally { 240 lock.releaseLock (); 241 } 242 return getPrimaryFile (); 243 } 244 245 254 protected FileObject handleMove (DataFolder f) throws IOException { 255 String name = FileUtil.findFreeFileName(f.getPrimaryFile(), 256 getName(), 257 GS_EXTENSION); 258 263 264 return FileUtil.moveFile (getPrimaryFile (), f.getPrimaryFile (), name); 265 } 266 267 268 270 public HelpCtx getHelpCtx() { 271 return new HelpCtx (GroupShadow.class); 272 } 273 274 285 public Cookie getCookie(Class cookie) { 286 if (cookie == TemplateWizard.Iterator.class) { 287 return getGroupTemplateIterator(); 288 } else { 289 return super.getCookie (cookie); 290 } 291 } 292 293 304 public static List readLinks(FileObject fo) throws IOException { 305 List list = new ArrayList (); 306 BufferedReader br = null; 307 try { 308 br = new BufferedReader (new InputStreamReader (fo.getInputStream())); 309 String line; 310 while ((line = br.readLine()) != null) { 311 File f = new File (line); 312 313 if (!f.isAbsolute()) { 314 315 File parentFolder = FileUtil.toFile(fo.getParent()); 317 318 f = new File (parentFolder, line).getAbsoluteFile(); 319 } 320 try { 321 322 f = f.getCanonicalFile(); 323 } catch (IOException ex) { 324 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 325 } 326 line = f.getAbsolutePath(); 327 line = line.replace('\\', '/'); 328 list.add(line); 329 } 330 } finally { 331 if (br != null) { 332 try { 333 br.close(); 334 } catch (IOException ex) { 335 336 } 337 } 338 } 339 return list; 340 } 341 342 350 public List readLinks() throws IOException { 351 return readLinks(getPrimaryFile()); 352 } 353 354 362 public static void writeLinks(List list, FileObject fo) throws IOException { 363 BufferedWriter bw = null; 364 FileLock lock = null; 365 try { 366 lock = fo.lock(); 367 bw = new BufferedWriter (new OutputStreamWriter ( 368 fo.getOutputStream(lock))); 369 for (Iterator i = list.iterator(); i.hasNext(); ) { 370 String line = (String ) i.next(); 371 File f = new File (line); 372 FileObject link = FileUtil.toFileObject(f); 373 374 if (f.isAbsolute()) { 375 FileObject parent = fo.getParent(); 376 377 if (FileUtil.isParentOf(parent, link)) { 378 line = FileUtil.getRelativePath(parent, link); 379 } else { 380 381 FileObject root = fo.getFileSystem().getRoot(); 382 if (root.equals(link.getFileSystem().getRoot())) { 383 384 StringBuffer lineBuf = new StringBuffer (80); 385 while (!root.equals(parent)) { 386 parent = parent.getParent(); 387 388 lineBuf.append("../"); 390 if (FileUtil.isParentOf(parent, link)) { 391 lineBuf.append(FileUtil.getRelativePath(parent, link)); 392 line = lineBuf.toString(); 393 break; 394 } 395 } 396 } 397 } 398 } 399 bw.write(line); 400 bw.newLine(); 401 } 402 } finally { 403 if (bw != null) { 404 try { 405 bw.close(); 406 } finally { 407 if (lock != null) { 408 lock.releaseLock(); 409 } 410 } 411 } 412 } 413 } 414 415 422 protected void writeLinks(List list) throws IOException { 423 writeLinks(list, getPrimaryFile()); 424 } 425 426 432 public static String getLinkName(FileObject fo) { 433 return fo.getPath(); 434 } 435 436 447 static DataObject getDataObjectByName(String filename, FileObject ref) 448 throws IOException { 449 FileObject file = ref.getFileSystem().findResource(filename); 450 return (file != null) ? DataObject.find(file) : null; 451 } 452 453 454 460 public DataObject[] getChildren() { 461 Object [] links = getLinks(); 462 463 if (links.length == 0) { 464 return new DataObject[0]; 465 } 466 467 int childrenCount = 0; 468 DataObject[] children = new DataObject[links.length]; 469 for (int i = 0; i < links.length; i++) { 470 Object link = links[i]; 471 if (link.getClass() == String .class) { continue; 473 } 474 children[childrenCount++] = (DataObject) link; 475 } 476 477 478 if (childrenCount != links.length) { 479 DataObject[] oldChildren = children; 480 children = new DataObject[childrenCount]; 481 System.arraycopy(oldChildren, 0, children, 0, childrenCount); 482 } 483 484 return children; 485 } 486 487 493 void primaryFileChanged() { 494 495 496 firePropertyChange(DataObject.Container.PROP_CHILDREN, null, null); 497 } 498 499 511 public Object [] getLinks() { 512 List filenames; 513 try { 514 filenames = readLinks(getPrimaryFile()); 515 } catch (IOException ex) { 516 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, ex); 517 return new Object [0]; 518 } 519 520 Set set = new HashSet (); 521 for (Iterator it = filenames.iterator(); it.hasNext(); ) { 522 String filename = (String ) it.next(); 523 try { 524 DataObject obj = getDataObjectByName(filename, getPrimaryFile()); 525 set.add(obj != null ? (Object ) obj 526 : (Object ) new String (filename)); 527 } catch (IOException ex) { 528 ex.printStackTrace(); 529 } 532 } 533 return set.toArray(); 534 } 535 536 550 public static String createName(String name, 551 String oldPrefix, 552 String newPrefix) { 553 if (name.startsWith(oldPrefix)) { 554 return newPrefix + name.substring(oldPrefix.length()); 555 } 556 return name; 557 } 558 559 564 private String replaceName(String name, String replacement) { 565 String fmt = getTemplatePattern(); 566 567 if (!isUsePattern() || fmt == null) { 568 return name.replaceAll("__.*?__", replacement); } 570 571 572 int i = name.lastIndexOf("__"); String postfix = (i > 0) ? name.substring(i + 2) : ""; 575 String subst = string3(name, replacement); 576 return MessageFormat.format( 577 fmt, 578 new String [] {name, replacement, postfix, subst}); 579 } 580 581 586 private String string3(String name, String replacement) { 587 588 String patch; 589 if (name.startsWith("__")) { patch = name; 591 } else { 592 patch = "__" + name; } 594 595 String s3 = substitute(patch, replacement); 596 597 if (s3.startsWith("__")) { s3 = s3.substring(2); 599 } 600 return s3; 601 } 602 603 616 private String substitute(String name, String replacement) { 617 int last; 618 int lastButOne; 619 StringBuffer sb = new StringBuffer (name); 620 while ((last = sb.lastIndexOf("__")) >= 0 && (lastButOne = sb.lastIndexOf("__", last - 2)) >= 0) { sb.replace(lastButOne, last + 2, replacement); 623 } 624 return sb.toString(); 625 } 626 627 629 protected DataObject handleCreateFromTemplate(DataFolder df, String name) throws IOException { 630 List createdObjs = createGroupFromTemplate(df, name, true); 631 return createdObjs != null && createdObjs.size() > 0 632 ? (DataObject) createdObjs.get(0) 633 : this; 634 } 635 636 641 List createGroupFromTemplate(DataFolder folder, 642 String name, 643 boolean root) throws IOException { 644 if (gsprocessed == null) { 645 gsprocessed = this; 646 } else { 647 return null; 648 } 649 650 if (name == null) { name = FileUtil.findFreeFileName(folder.getPrimaryFile(), 652 getPrimaryFile().getName(), 653 GS_EXTENSION); 654 } 655 Object [] objs = getLinks(); 656 ArrayList createdObjs = new ArrayList (objs.length + 1); 657 ArrayList linksList = new ArrayList (objs.length); 658 659 try { 660 for (int i = 0; i < objs.length; i++) { 661 if (objs[i] instanceof DataObject) { 662 DataObject original = (DataObject) objs[i]; 663 664 if (original instanceof GroupShadow) { 665 GroupShadow gs = (GroupShadow) original; 666 List items = gs.createGroupFromTemplate(folder, name, false); 667 if (items != null) { 668 for (int j = 0, n = items.size(); j < n; j++) { 669 DataObject obj = (DataObject) items.get(j); 670 createdObjs.add(obj); 671 linksList.add(getLinkName(obj.getPrimaryFile())); 672 if (j == 0 673 && obj instanceof GroupShadow 674 && gs.getTemplateAll()) 675 break; 676 } 677 } 679 } else { 680 String repName = replaceName(original.getName(), name); 681 DataObject newObj = original.createFromTemplate(folder, repName); 682 createdObjs.add(newObj); 683 linksList.add(getLinkName(newObj.getPrimaryFile())); 684 } 685 } 686 } 687 688 if (objs.length == 0 || getTemplateAll()) { String repName = root ? name : replaceName(getName(), name); 690 FileObject fo = folder.getPrimaryFile().createData(repName, GS_EXTENSION); 691 writeLinks(linksList, fo); 692 GroupShadow gs = (GroupShadow) DataObject.find(fo); 693 if (gs == null) { 694 gs = new GroupShadow(fo, getLoader()); 695 } 696 createdObjs.add(0, gs); 697 } 698 } 699 catch (IOException ex) { 700 throw ex; 701 } 702 catch (Error er) { 703 er.printStackTrace(); 704 throw er; 705 } 706 finally { 707 gsprocessed = null; 708 } 709 710 return createdObjs; 711 } 712 713 717 public void setShowLinks(boolean show) { 718 showLinks = show; 719 } 720 721 722 public boolean getShowLinks() { 723 return showLinks; 724 } 725 726 731 public void setTemplatePattern(String templatePattern) throws IOException { 732 final FileObject fo = getPrimaryFile(); 733 String old = getTemplatePattern(); 734 735 fo.setAttribute(PROP_TEMPLATE_PATTERN, templatePattern); 736 737 if (old != templatePattern) { 738 firePropertyChange(PROP_TEMPLATE_PATTERN, old, templatePattern); 739 } 740 741 } 742 743 744 public String getTemplatePattern(){ 745 Object value = getPrimaryFile().getAttribute(GroupShadow.PROP_TEMPLATE_PATTERN); 746 return (value instanceof String ) ? (String ) value 747 : "{1}"; } 749 750 751 752 public boolean isUsePattern() { 753 Object o = getPrimaryFile().getAttribute(GroupShadow.PROP_USE_PATTERN); 754 return Boolean.TRUE.equals(o); 755 } 756 757 758 759 public void setUsePattern(boolean usePattern) throws IOException { 760 FileObject fileObject = getPrimaryFile(); 761 boolean oldValue = isUsePattern(); 762 763 if (usePattern == oldValue) { 764 return; 765 } 766 fileObject.setAttribute(PROP_USE_PATTERN, Boolean.valueOf(usePattern)); 767 768 firePropertyChange(PROP_USE_PATTERN, Boolean.valueOf(oldValue), 769 Boolean.valueOf(usePattern)); 770 } 771 772 773 public boolean getTemplateAll() { 774 Object o = getPrimaryFile().getAttribute(GroupShadow.PROP_TEMPLATE_ALL); 775 return Boolean.TRUE.equals(o); 776 } 777 778 779 780 public void setTemplateAll(boolean templateAll) throws IOException { 781 final FileObject fo = getPrimaryFile(); 782 boolean oldtempl = getTemplateAll(); 783 784 fo.setAttribute(PROP_TEMPLATE_ALL, (templateAll ? Boolean.TRUE : null)); 785 786 if (oldtempl != templateAll) { 787 firePropertyChange(PROP_TEMPLATE_ALL, Boolean.valueOf(oldtempl), 788 Boolean.valueOf(templateAll)); 789 } 790 } 791 792 793 static String getLocalizedString (String s) { 794 return NbBundle.getBundle (GroupShadow.class).getString (s); 795 } 796 797 } 798 | Popular Tags |