KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > languages > SyntaxContext


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.api.languages;
21
22 import javax.swing.text.Document JavaDoc;
23 import javax.swing.text.JTextComponent JavaDoc;
24 import org.netbeans.api.languages.ASTToken;
25 import org.netbeans.api.lexer.TokenHierarchy;
26 import org.netbeans.api.lexer.TokenSequence;
27 import org.netbeans.modules.editor.NbEditorUtilities;
28 import org.openide.cookies.EditorCookie;
29 import org.openide.loaders.DataObject;
30
31
32 /**
33  * Represents context for methods called from nbs files. This version of context
34  * contains syntax information too.
35  *
36  * @author Jan Jancura
37  */

38 public abstract class SyntaxContext extends Context {
39
40     
41     /**
42      * Returns current AST path.
43      *
44      * @return current AST path
45      */

46     public abstract ASTPath getASTPath ();
47     
48     /**
49      * Creates a new SyntaxContext.
50      *
51      * @return a new SyntaxContext
52      */

53     public static SyntaxContext create (Document JavaDoc doc, ASTPath path) {
54         return new CookieImpl (doc, path);
55     }
56     
57     private static class CookieImpl extends SyntaxContext {
58         
59         private Document JavaDoc doc;
60         private ASTPath path;
61         private JTextComponent JavaDoc component;
62         private TokenSequence tokenSequence;
63         
64         CookieImpl (
65             Document JavaDoc doc,
66             ASTPath path
67         ) {
68             this.doc = doc;
69             this.path = path;
70         }
71         
72         public JTextComponent JavaDoc getJTextComponent () {
73             if (component == null) {
74                 DataObject dob = NbEditorUtilities.getDataObject (doc);
75                 EditorCookie ec = (EditorCookie) dob.getLookup ().lookup (EditorCookie.class);
76                 if (ec.getOpenedPanes ().length > 0)
77                     component = ec.getOpenedPanes () [0];
78             }
79             return component;
80         }
81         
82         public ASTPath getASTPath () {
83             return path;
84         }
85         
86         public Document JavaDoc getDocument () {
87             return doc;
88         }
89         
90         public TokenSequence getTokenSequence () {
91             if (tokenSequence == null) {
92                 TokenHierarchy th = TokenHierarchy.get (doc);
93                 tokenSequence = th.tokenSequence ();
94             }
95             Object JavaDoc leaf = path.getLeaf ();
96             if (leaf instanceof ASTToken)
97                 tokenSequence.move (((ASTToken) leaf).getOffset ());
98             else
99                 tokenSequence.move (((ASTNode) leaf).getOffset ());
100             tokenSequence.moveNext();
101             return tokenSequence;
102         }
103     }
104 }
105
106
107
Popular Tags