KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > ui > ShowJavadocAction


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.apisupport.project.ui;
21
22 import java.net.URL JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import org.openide.ErrorManager;
25 import org.openide.awt.HtmlBrowser;
26 import org.openide.awt.StatusDisplayer;
27 import org.openide.filesystems.FileObject;
28 import org.openide.filesystems.URLMapper;
29 import org.openide.nodes.Node;
30 import org.openide.util.NbBundle;
31 import org.openide.util.HelpCtx;
32 import org.openide.util.actions.NodeAction;
33
34 // XXX this class is more or less copy-pasted PlatformNode from j2seproject.
35
// Get rid of it as soon as "some" Libraries Node API is provided.
36

37 /**
38  * Action for showing Javadoc. The action looks up
39  * the {@link ShowJavadocAction.JavadocProvider} in the
40  * activated node's Lookup and delegates to it.
41  * @author Tomas Zezula
42  */

43 final class ShowJavadocAction extends NodeAction {
44     
45     /**
46      * Implementation of this interfaces has to be placed
47      * into the node's Lookup to allow {@link ShowJavadocAction}
48      * on the node.
49      */

50     public static interface JavadocProvider {
51         
52         /**
53          * Checks if the node can provide Javaodc
54          * @return true if the action should be enabled
55          */

56         public abstract boolean hasJavadoc();
57         
58         /**
59          * Opens javadoc page in the browser
60          */

61         public abstract void showJavadoc();
62     }
63     
64     protected void performAction(Node[] activatedNodes) {
65         if (activatedNodes.length!=1) {
66             return;
67         }
68         JavadocProvider jd = (JavadocProvider) activatedNodes[0].getLookup().lookup(JavadocProvider.class);
69         if (jd == null) {
70             return;
71         }
72         jd.showJavadoc();
73     }
74     
75     protected boolean enable(Node[] activatedNodes) {
76         if (activatedNodes.length!=1) {
77             return false;
78         }
79         JavadocProvider jd = (JavadocProvider) activatedNodes[0].getLookup().lookup(JavadocProvider.class);
80         if (jd == null) {
81             return false;
82         }
83         return jd.hasJavadoc();
84     }
85     
86     public final String JavaDoc getName() {
87         return NbBundle.getMessage(ShowJavadocAction.class,"CTL_ShowJavadoc");
88     }
89     
90     public final HelpCtx getHelpCtx() {
91         return new HelpCtx(ShowJavadocAction.class);
92     }
93     
94     public final boolean asynchronous() {
95         return false;
96     }
97     
98     /**
99      * Opens the IDE default browser with given URL
100      * @param javadoc URL of the javadoc page
101      * @param displayName the name of file to be displayed, typically the package name for class
102      * or project name for project.
103      */

104     static void showJavaDoc(URL JavaDoc javadoc, String JavaDoc displayName) {
105         if (javadoc!=null) {
106             HtmlBrowser.URLDisplayer.getDefault().showURL(javadoc);
107         } else {
108             StatusDisplayer.getDefault().setStatusText(
109                     NbBundle.getMessage(ShowJavadocAction.class, "TXT_NoJavadoc",
110                     displayName));
111         }
112     }
113     
114     /**
115      * Locates a javadoc page by a relative name and an array of javadoc roots
116      * @param resource the relative name of javadoc page
117      * @param urls the array of javadoc roots
118      * @return the URL of found javadoc page or null if there is no such a page.
119      */

120     static URL JavaDoc findJavadoc(String JavaDoc resource, URL JavaDoc urls[]) {
121         for (int i = 0; i < urls.length; i++) {
122             String JavaDoc base = urls[i].toExternalForm();
123             if (!base.endsWith("/")) { // NOI18N
124
base+="/"; // NOI18N
125
}
126             try {
127                 URL JavaDoc u = new URL JavaDoc(base+resource);
128                 FileObject fo = URLMapper.findFileObject(u);
129                 if (fo != null) {
130                     return u;
131                 }
132             } catch (MalformedURLException JavaDoc ex) {
133                 ErrorManager.getDefault().log(ErrorManager.ERROR,
134                         "Cannot create URL for " + base + resource + ". " + ex.toString()); //NOI18N
135
continue;
136             }
137         }
138         return null;
139     }
140     
141 }
142
Popular Tags