KickJava   Java API By Example, From Geeks To Geeks.

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


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.JavadocForBinaryQueryImplementation;
26 import org.openide.ErrorManager;
27 import org.openide.filesystems.FileUtil;
28 import org.openide.util.Lookup;
29
30 /**
31  * A query to find Javadoc root for the given classpath root.
32  * @author David Konecny, Jesse Glick
33  * @since org.netbeans.api.java/1 1.4
34  */

35 public class JavadocForBinaryQuery {
36
37     private static final ErrorManager ERR = ErrorManager.getDefault().getInstance(JavadocForBinaryQuery.class.getName());
38     
39     private static final Lookup.Result<? extends JavadocForBinaryQueryImplementation> implementations =
40         Lookup.getDefault().lookupResult(JavadocForBinaryQueryImplementation.class);
41
42     private JavadocForBinaryQuery () {
43     }
44
45     /**
46      * Find Javadoc information for a classpath root containing Java classes.
47      * <p>
48      * These methods calls findJavadoc method on the JavadocForBinaryQueryImplementation
49      * instances registered in the lookup until null result is returned for given binaryRoot. The
50      * non null result is returned.
51      * </p>
52      * @param binary URL of a classpath root
53      * @return a result object encapsulating the answer (never null)
54      */

55     public static Result findJavadoc(URL JavaDoc binary) {
56         if (FileUtil.isArchiveFile(binary)) {
57             throw new IllegalArgumentException JavaDoc("File URL pointing to " + // NOI18N
58
"JAR is not valid classpath entry. Use jar: URL. Was: "+binary); // NOI18N
59
}
60         boolean log = ERR.isLoggable(ErrorManager.INFORMATIONAL);
61         if (log) ERR.log("JFBQ.findJavadoc: " + binary);
62         for (JavadocForBinaryQueryImplementation impl : implementations.allInstances()) {
63             Result r = impl.findJavadoc(binary);
64             if (r != null) {
65                 if (log) ERR.log(" got result " + Arrays.asList(r.getRoots()) + " from " + impl);
66                 return r;
67             } else {
68                 if (log) ERR.log(" got no result from " + impl);
69             }
70         }
71         if (log) ERR.log(" got no results from any impl");
72         return EMPTY_RESULT;
73     }
74
75     /**
76      * Result of finding Javadoc, encapsulating the answer as well as the
77      * ability to listen to it.
78      */

79     public interface Result {
80         
81         /**
82          * Get the Javadoc roots.
83          * Each root should contain the main <code>index.html</code>, so that
84          * for a class <samp>pkg.Class</samp> the generated documentation would
85          * have a path <samp>pkg/Class.html</samp> relative to one of the roots.
86          * @return array of roots of Javadoc documentation (may be empty but not null)
87          */

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

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

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