KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > railsprojects > ui > ShowRDocAction


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.ruby.railsprojects.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 RDoc. The action looks up
38  * the {@link ShowRDocAction.RDocProvider} in the
39  * activated node's Lookup and delegates to it.
40  *
41  * (Based on the ShowJavaDocAction in J2SE projects)
42  *
43  * @author Tomas Zezula
44  * @author Tor Norbye
45  */

46 final class ShowRDocAction extends NodeAction {
47
48     /**
49      * Implementation of this interfaces has to be placed
50      * into the node's Lookup to allow {@link ShowRDocAction}
51      * on the node.
52      */

53     public static interface RDocProvider {
54
55         /**
56          * Checks if the node can provide RDoc
57          * @return true if the action should be enabled
58          */

59         public abstract boolean hasRDoc ();
60
61         /**
62          * Opens rdoc page in the browser
63          */

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

107     static void showRDoc (URL JavaDoc rdoc, String JavaDoc displayName) {
108         if (rdoc!=null) {
109             HtmlBrowser.URLDisplayer.getDefault().showURL(rdoc);
110         }
111         else {
112             StatusDisplayer.getDefault().setStatusText(MessageFormat.format(NbBundle.getMessage(ShowRDocAction.class,
113                     "TXT_NoRDoc"), new Object JavaDoc[] {displayName})); //NOI18N
114
}
115     }
116
117     /**
118      * Locates a rdoc page by a relative name and an array of rdoc roots
119      * @param resource the relative name of rdoc page
120      * @param urls the array of rdoc roots
121      * @return the URL of found rdoc page or null if there is no such a page.
122      */

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