KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > clientproject > 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.j2ee.clientproject.ui;
21
22 import java.net.URL JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.text.MessageFormat JavaDoc;
25 import org.openide.ErrorManager;
26 import org.openide.awt.HtmlBrowser;
27 import org.openide.awt.StatusDisplayer;
28 import org.openide.filesystems.FileObject;
29 import org.openide.filesystems.URLMapper;
30 import org.openide.nodes.Node;
31 import org.openide.util.NbBundle;
32 import org.openide.util.HelpCtx;
33 import org.openide.util.actions.NodeAction;
34
35
36 /**
37  * Action for showing Javadoc. The action looks up
38  * the {@link ShowJavadocAction.JavadocProvider} in the
39  * activated node's Lookup and delegates to it.
40  * @author Tomas Zezula
41  */

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

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

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

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

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

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