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 org.openide.windows.TopComponent;
045: import org.openide.cookies.EditorCookie;
046: import org.openide.nodes.Node;
047: import org.openide.text.NbDocument;
048:
049: import javax.swing.JEditorPane;
050: import javax.swing.text.Document;
051: import javax.swing.text.Element;
052: import javax.swing.text.StyledDocument;
053: import javax.swing.text.BadLocationException;
054:
055: /**
056: * Tries to find actual focused java word.
057: *
058: * @author Petr Hrebejk
059: */
060: final class GetJavaWord extends Object {
061:
062: static String getCurrentJavaWord() {
063: Node[] n = TopComponent.getRegistry().getActivatedNodes();
064:
065: if (n.length == 1) {
066: EditorCookie ec = (EditorCookie) n[0]
067: .getCookie(EditorCookie.class);
068: if (ec != null) {
069: JEditorPane[] panes = ec.getOpenedPanes();
070: if (panes == null)
071: return null;
072: if (panes.length > 0) {
073: return forPane(panes[0]);
074: }
075: }
076: }
077:
078: return null;
079: }
080:
081: static String forPane(JEditorPane p) {
082: if (p == null)
083: return null;
084:
085: String selection = p.getSelectedText();
086:
087: if (selection != null && selection.length() > 0) {
088: return selection;
089: } else {
090:
091: // try to guess which word is underneath the caret's dot.
092:
093: Document doc = p.getDocument();
094: Element lineRoot;
095:
096: if (doc instanceof StyledDocument) {
097: lineRoot = NbDocument
098: .findLineRootElement((StyledDocument) doc);
099: } else {
100: lineRoot = doc.getDefaultRootElement();
101: }
102: int dot = p.getCaret().getDot();
103: Element line = lineRoot.getElement(lineRoot
104: .getElementIndex(dot));
105:
106: if (line == null)
107: return null;
108:
109: String text = null;
110: try {
111: text = doc.getText(line.getStartOffset(), line
112: .getEndOffset()
113: - line.getStartOffset());
114: } catch (BadLocationException e) {
115: return null;
116: }
117:
118: if (text == null)
119: return null;
120: int pos = dot - line.getStartOffset();
121:
122: if (pos < 0 || pos >= text.length())
123: return null;
124:
125: int bix, eix;
126:
127: for (bix = Character.isJavaIdentifierPart(text.charAt(pos)) ? pos
128: : pos - 1; bix >= 0
129: && Character.isJavaIdentifierPart(text.charAt(bix)); bix--)
130: ;
131: for (eix = pos; eix < text.length()
132: && Character.isJavaIdentifierPart(text.charAt(eix)); eix++)
133: ;
134:
135: return bix == eix ? null : text.substring(bix + 1, eix);
136: }
137: }
138: }
|