KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javadoc > search > ShowDocAction


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
21 package org.netbeans.modules.javadoc.search;
22
23 import javax.swing.JEditorPane JavaDoc;
24 import org.openide.cookies.EditorCookie;
25 import org.openide.nodes.Node;
26 import org.openide.util.HelpCtx;
27 import org.openide.util.NbBundle;
28 import org.openide.util.actions.CookieAction;
29 import org.openide.windows.TopComponent;
30
31 import java.awt.*;
32
33 /**
34  * Try to find generated and mounted documentation for selected node.
35  * //!!! It has mixed semantics with the find doc action because
36  * it tries to inspect opened editor too
37  *
38  * @author Petr Suchomel
39  * @version 1.0
40  */

41 public final class ShowDocAction extends CookieAction {
42
43     static final long serialVersionUID =3578357584245478L;
44
45     public ShowDocAction() {
46         putValue("noIconInMenu", Boolean.TRUE); // NOI18N
47
}
48     /** Human presentable name of the action. This should be
49     * presented as an item in a menu.
50     * @return the name of the action
51     */

52     public String JavaDoc getName () {
53         return NbBundle.getBundle( ShowDocAction.class ).getString ("CTL_SHOWDOC_MenuItem"); //NOI18N
54
}
55
56     /** Cookie classes contains one class returned by cookie () method.
57     */

58     protected final Class JavaDoc[] cookieClasses () {
59         return new Class JavaDoc[] { EditorCookie.class };
60     }
61
62     /** All must be DataFolders or JavaDataObjects
63     */

64     protected int mode () {
65         return MODE_EXACTLY_ONE;
66     }
67
68     /** Help context where to find more about the action.
69     * @return the help context for this action
70     */

71     public HelpCtx getHelpCtx () {
72         return new HelpCtx (ShowDocAction.class);
73     }
74
75     /** This method is called by one of the "invokers" as a result of
76     * some user's action that should lead to actual "performing" of the action.
77     * This default implementation calls the assigned actionPerformer if it
78     * is not null otherwise the action is ignored.
79     */

80     public void performAction ( Node[] nodes ) {
81         IndexSearch indexSearch = IndexSearch.getDefault();
82                 
83         if( nodes.length == 1 && nodes[0] != null ) {
84             String JavaDoc toFind = findTextFromNode(nodes[0]);
85             if (toFind != null)
86                 indexSearch.setTextToFind( toFind );
87         }
88         indexSearch.open ();
89         indexSearch.requestActive();
90     }
91     
92     /**
93      * Attempts to find a suitable text from the node.
94      */

95     private String JavaDoc findTextFromNode(Node n) {
96         EditorCookie ec = (EditorCookie)n.getCookie(EditorCookie.class);
97         // no editor underneath the node --> node's name is the only searchable text.
98
if (ec != null) {
99             JEditorPane JavaDoc[] panes = ec.getOpenedPanes();
100             if (panes != null) {
101                 TopComponent activetc = TopComponent.getRegistry().getActivated();
102                 for (int i = 0; i < panes.length; i++) {
103                     if (activetc.isAncestorOf(panes[i])) {
104                         // we have found the correct JEditorPane
105
String JavaDoc s = GetJavaWord.forPane(panes[i]);
106                         if (s != null)
107                             return s;
108                         else
109                             break;
110                     }
111                 }
112             }
113         }
114         return n.getName();
115     }
116
117     protected boolean asynchronous() {
118         return false;
119     }
120 }
121
Popular Tags