KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbjarproject > ui > logicalview > libraries > 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.j2ee.ejbjarproject.ui.logicalview.libraries;
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
35 /**
36  * Action for showing Javadoc. The action looks up
37  * the {@link ShowJavadocAction.JavadocProvider} in the
38  * activated node's Lookup and delegates to it.
39  * @author Tomas Zezula
40  */

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

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

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

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

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

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