KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > java > queries > SourceForBinaryQuery


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.api.java.queries;
21
22 import java.net.URL JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import javax.swing.event.ChangeListener JavaDoc;
25 import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation;
26 import org.openide.ErrorManager;
27 import org.openide.filesystems.FileObject;
28 import org.openide.filesystems.FileUtil;
29 import org.openide.util.Lookup;
30
31 /**
32  * The query is used for finding sources for binaries.
33  * The examples of usage of this query are:
34  * <ul>
35  * <li><p>finding source for library</p></li>
36  * <li><p>finding src.zip for platform</p></li>
37  * <li><p>finding source folder for compiled jar or build folder</p></li>
38  * </ul>
39  * @see SourceForBinaryQueryImplementation
40  * @since org.netbeans.api.java/1 1.4
41  */

42 public class SourceForBinaryQuery {
43     
44     private static final ErrorManager ERR = ErrorManager.getDefault().getInstance(SourceForBinaryQuery.class.getName());
45     
46     private static final Lookup.Result<? extends SourceForBinaryQueryImplementation> implementations =
47         Lookup.getDefault().lookupResult (SourceForBinaryQueryImplementation.class);
48
49     private SourceForBinaryQuery () {
50     }
51
52     /**
53      * Returns the source root for given binary root (for example, src folder for jar file or build folder).
54      * @param binaryRoot the ClassPath root of compiled files.
55      * @return a result object encapsulating the answer (never null)
56      */

57     public static Result findSourceRoots (URL JavaDoc binaryRoot) {
58         if (FileUtil.isArchiveFile(binaryRoot)) {
59             throw new IllegalArgumentException JavaDoc("File URL pointing to " + // NOI18N
60
"JAR is not valid classpath entry. Use jar: URL. Was: "+binaryRoot); // NOI18N
61
}
62         if (!binaryRoot.toExternalForm().endsWith("/")) {
63             throw new IllegalArgumentException JavaDoc ("Folder URL must end with '/'. Was: "+binaryRoot);
64         }
65         boolean log = ERR.isLoggable(ErrorManager.INFORMATIONAL);
66         if (log) ERR.log("SFBQ.findSourceRoots: " + binaryRoot);
67         for (SourceForBinaryQueryImplementation impl : implementations.allInstances()) {
68             Result result = impl.findSourceRoots(binaryRoot);
69             if (result != null) {
70                 if (log) ERR.log(" got result " + Arrays.asList(result.getRoots()) + " from " + impl);
71                 return result;
72             }
73         }
74         return EMPTY_RESULT;
75     }
76     
77     /**
78      * Result of finding sources, encapsulating the answer as well as the
79      * ability to listen to it.
80      */

81     public interface Result {
82         
83         /**
84          * Get the source roots.
85          * @return array of roots of sources (may be empty but not null)
86          */

87         FileObject[] getRoots();
88         
89         /**
90          * Add a listener to changes in the roots.
91          * @param l a listener to add
92          */

93         void addChangeListener(ChangeListener JavaDoc l);
94         
95         /**
96          * Remove a listener to changes in the roots.
97          * @param l a listener to remove
98          */

99         void removeChangeListener(ChangeListener JavaDoc l);
100         
101     }
102     
103     private static final Result EMPTY_RESULT = new EmptyResult();
104     private static final class EmptyResult implements Result {
105         private static final FileObject[] NO_ROOTS = new FileObject[0];
106         EmptyResult() {}
107         public FileObject[] getRoots() {
108             return NO_ROOTS;
109         }
110         public void addChangeListener(ChangeListener JavaDoc l) {}
111         public void removeChangeListener(ChangeListener JavaDoc l) {}
112     }
113
114 }
115
Popular Tags