001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.javadoc.search;
043:
044: import javax.swing.JEditorPane;
045: import org.openide.cookies.EditorCookie;
046: import org.openide.nodes.Node;
047: import org.openide.util.HelpCtx;
048: import org.openide.util.NbBundle;
049: import org.openide.util.actions.CookieAction;
050: import org.openide.windows.TopComponent;
051:
052: import java.awt.*;
053:
054: /**
055: * Try to find generated and mounted documentation for selected node.
056: * //!!! It has mixed semantics with the find doc action because
057: * it tries to inspect opened editor too
058: *
059: * @author Petr Suchomel
060: * @version 1.0
061: */
062: public final class ShowDocAction extends CookieAction {
063:
064: static final long serialVersionUID = 3578357584245478L;
065:
066: public ShowDocAction() {
067: putValue("noIconInMenu", Boolean.TRUE); // NOI18N
068: }
069:
070: /** Human presentable name of the action. This should be
071: * presented as an item in a menu.
072: * @return the name of the action
073: */
074: public String getName() {
075: return NbBundle.getBundle(ShowDocAction.class).getString(
076: "CTL_SHOWDOC_MenuItem"); //NOI18N
077: }
078:
079: /** Cookie classes contains one class returned by cookie () method.
080: */
081: protected final Class[] cookieClasses() {
082: return new Class[] { EditorCookie.class };
083: }
084:
085: /** All must be DataFolders or JavaDataObjects
086: */
087: protected int mode() {
088: return MODE_EXACTLY_ONE;
089: }
090:
091: /** Help context where to find more about the action.
092: * @return the help context for this action
093: */
094: public HelpCtx getHelpCtx() {
095: return new HelpCtx(ShowDocAction.class);
096: }
097:
098: /** This method is called by one of the "invokers" as a result of
099: * some user's action that should lead to actual "performing" of the action.
100: * This default implementation calls the assigned actionPerformer if it
101: * is not null otherwise the action is ignored.
102: */
103: public void performAction(Node[] nodes) {
104: IndexSearch indexSearch = IndexSearch.getDefault();
105:
106: if (nodes.length == 1 && nodes[0] != null) {
107: String toFind = findTextFromNode(nodes[0]);
108: if (toFind != null)
109: indexSearch.setTextToFind(toFind);
110: }
111: indexSearch.open();
112: indexSearch.requestActive();
113: }
114:
115: /**
116: * Attempts to find a suitable text from the node.
117: */
118: private String findTextFromNode(Node n) {
119: EditorCookie ec = (EditorCookie) n
120: .getCookie(EditorCookie.class);
121: // no editor underneath the node --> node's name is the only searchable text.
122: if (ec != null) {
123: JEditorPane[] panes = ec.getOpenedPanes();
124: if (panes != null) {
125: TopComponent activetc = TopComponent.getRegistry()
126: .getActivated();
127: for (int i = 0; i < panes.length; i++) {
128: if (activetc.isAncestorOf(panes[i])) {
129: // we have found the correct JEditorPane
130: String s = GetJavaWord.forPane(panes[i]);
131: if (s != null)
132: return s;
133: else
134: break;
135: }
136: }
137: }
138: }
139: return n.getName();
140: }
141:
142: protected boolean asynchronous() {
143: return false;
144: }
145: }
|