1 19 20 package org.netbeans.modules.java; 21 22 import org.netbeans.api.java.classpath.ClassPath; 23 import org.netbeans.api.java.queries.SourceForBinaryQuery; 24 import org.netbeans.spi.java.classpath.support.ClassPathSupport; 25 import org.openide.ErrorManager; 26 import org.openide.awt.StatusDisplayer; 27 import org.openide.cookies.OpenCookie; 28 import org.openide.filesystems.FileObject; 29 import org.openide.filesystems.FileStateInvalidException; 30 import org.openide.filesystems.FileUtil; 31 import org.openide.loaders.DataObject; 32 import org.openide.loaders.DataObjectExistsException; 33 import org.openide.loaders.DataObjectNotFoundException; 34 import org.openide.loaders.MultiDataObject; 35 import org.openide.loaders.MultiFileLoader; 36 import org.openide.nodes.Node; 37 import org.openide.nodes.Node.Cookie; 38 import org.openide.util.NbBundle; 39 40 41 public final class ClassDataObject extends MultiDataObject { 42 43 public ClassDataObject(FileObject pf, MultiFileLoader loader) throws DataObjectExistsException { 44 super(pf, loader); 45 } 46 47 public @Override Node createNodeDelegate() { 48 return new JavaNode (this, false); 49 } 50 51 public @Override <T extends Cookie> T getCookie(Class <T> type) { 52 if (type.isAssignableFrom(OpenSourceCookie.class)) { 53 return type.cast(new OpenSourceCookie()); 54 } else { 55 return super.getCookie (type); 56 } 57 } 58 59 private final class OpenSourceCookie implements OpenCookie { 60 61 public void open() { 62 try { 63 FileObject fo = getPrimaryFile(); 64 FileObject binaryRoot = null; 65 String resourceName = null; 66 ClassPath cp = ClassPath.getClassPath(fo, ClassPath.COMPILE); 67 if (cp == null || (binaryRoot = cp.findOwnerRoot(fo))==null) { 68 cp = ClassPath.getClassPath(fo, ClassPath.EXECUTE); 69 if (cp != null) { 70 binaryRoot = cp.findOwnerRoot(fo); 71 resourceName = cp.getResourceName(fo,'/',false); } 73 } else if (binaryRoot != null) { 74 resourceName = cp.getResourceName(fo,'/',false); } 76 FileObject[] sourceRoots = null; 77 if (binaryRoot != null) { 78 sourceRoots = SourceForBinaryQuery.findSourceRoots(binaryRoot.getURL()).getRoots(); 79 } 80 FileObject resource = null; 81 if (sourceRoots != null && sourceRoots.length>0) { 82 cp = ClassPathSupport.createClassPath(sourceRoots); 83 resource = cp.findResource(resourceName+ ".java"); } 85 if (resource !=null ) { 86 DataObject sourceFile = DataObject.find(resource); 87 OpenCookie oc = sourceFile.getCookie(OpenCookie.class); 88 if (oc != null) { 89 oc.open(); 90 } else { 91 ErrorManager.getDefault().log("SourceFile: "+FileUtil.getFileDisplayName (resource) +" has no OpenCookie"); } 93 } else { 94 if (resourceName == null) 95 resourceName = fo.getName(); 96 StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(ClassDataObject.class,"TXT_NoSources", 97 resourceName.replace('/','.'))); } 99 } catch (FileStateInvalidException e) { 100 ErrorManager.getDefault().notify(e); 101 } catch (DataObjectNotFoundException nf) { 102 ErrorManager.getDefault().notify(nf); 103 } 104 } 105 } 106 } 107 | Popular Tags |