KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > ClassDataObject


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc Node createNodeDelegate() {
48         return new JavaNode (this, false);
49     }
50
51     public @Override JavaDoc <T extends Cookie> T getCookie(Class JavaDoc<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 JavaDoc 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); //NOI18N
72
}
73                 } else if (binaryRoot != null) {
74                     resourceName = cp.getResourceName(fo,'/',false); //NOI18N
75
}
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"); //NOI18N
84
}
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"); //NOI18N
92
}
93                 } else {
94                     if (resourceName == null)
95                         resourceName = fo.getName();
96                     StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(ClassDataObject.class,"TXT_NoSources",
97                             resourceName.replace('/','.'))); //NOI18N
98
}
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