KickJava   Java API By Example, From Geeks To Geeks.

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


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.javadoc.search;
21
22 import org.openide.windows.TopComponent;
23 import org.openide.cookies.EditorCookie;
24 import org.openide.nodes.Node;
25 import org.openide.text.NbDocument;
26
27 import javax.swing.JEditorPane JavaDoc;
28 import javax.swing.text.Document JavaDoc;
29 import javax.swing.text.Element JavaDoc;
30 import javax.swing.text.StyledDocument JavaDoc;
31 import javax.swing.text.BadLocationException JavaDoc;
32
33
34 /**
35  * Tries to find actual focused java word.
36  *
37  * @author Petr Hrebejk
38  */

39 final class GetJavaWord extends Object JavaDoc {
40
41
42     static String JavaDoc getCurrentJavaWord() {
43         Node[] n = TopComponent.getRegistry ().getActivatedNodes ();
44
45         if (n.length == 1) {
46             EditorCookie ec = (EditorCookie) n[0].getCookie (EditorCookie.class);
47             if (ec != null) {
48                 JEditorPane JavaDoc[] panes = ec.getOpenedPanes ();
49                 if ( panes == null )
50                     return null;
51                 if (panes.length > 0) {
52                     return forPane(panes[0]);
53                 }
54             }
55         }
56
57         return null;
58     }
59     
60     static String JavaDoc forPane(JEditorPane JavaDoc p) {
61         if (p == null) return null;
62  
63         String JavaDoc selection = p.getSelectedText ();
64  
65         if ( selection != null && selection.length() > 0 ) {
66             return selection;
67         } else {
68  
69             // try to guess which word is underneath the caret's dot.
70

71             Document doc = p.getDocument();
72             Element JavaDoc lineRoot;
73  
74             if (doc instanceof StyledDocument JavaDoc) {
75                 lineRoot = NbDocument.findLineRootElement((StyledDocument JavaDoc)doc);
76             } else {
77                 lineRoot = doc.getDefaultRootElement();
78             }
79             int dot = p.getCaret().getDot();
80             Element JavaDoc line = lineRoot.getElement(lineRoot.getElementIndex(dot));
81  
82             if (line == null) return null;
83  
84             String JavaDoc text = null;
85             try {
86                 text = doc.getText(line.getStartOffset(),
87                     line.getEndOffset() - line.getStartOffset());
88             } catch (BadLocationException JavaDoc e) {
89                 return null;
90             }
91             
92             if ( text == null )
93                 return null;
94             int pos = dot - line.getStartOffset();
95
96             if ( pos < 0 || pos >= text.length() )
97                 return null;
98
99             int bix, eix;
100
101             for( bix = Character.isJavaIdentifierPart( text.charAt( pos ) ) ? pos : pos - 1;
102                     bix >= 0 && Character.isJavaIdentifierPart( text.charAt( bix ) ); bix-- );
103             for( eix = pos; eix < text.length() && Character.isJavaIdentifierPart( text.charAt( eix )); eix++ );
104
105             return bix == eix ? null : text.substring( bix + 1, eix );
106         }
107     }
108 }
109
Popular Tags