KickJava   Java API By Example, From Geeks To Geeks.

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


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.lexer.TokenSequence;
25 import org.netbeans.modules.editor.NbEditorUtilities;
26 import org.openide.cookies.EditorCookie;
27 import org.openide.loaders.DataObject;
28
29 /**
30  * Represents context for methods called from nbs files.
31  *
32  * @author Jan Jancura
33  */

34 public abstract class Context {
35
36     
37     /**
38      * Returns instance of editor.
39      *
40      * @return instance of editor
41      */

42     public abstract JTextComponent JavaDoc getJTextComponent ();
43     
44     /**
45      * Returns instance of {@link javax.swing.text.Document}.
46      *
47      * @return instance of {@link javax.swing.text.Document}
48      */

49     public abstract Document JavaDoc getDocument ();
50     
51     /**
52      * Returns instance of {@link org.netbeans.api.lexer.TokenSequence}.
53      *
54      * @return instance of {@link org.netbeans.api.lexer.TokenSequence}
55      */

56     public abstract TokenSequence getTokenSequence ();
57     
58     /**
59      * Creates a new Context.
60      *
61      * @return a new Context
62      */

63     public static Context create (Document JavaDoc doc, TokenSequence tokenSequence) {
64         return new CookieImpl (doc, tokenSequence);
65     }
66     
67     private static class CookieImpl extends Context {
68         
69         private Document JavaDoc doc;
70         private JTextComponent JavaDoc component;
71         private TokenSequence tokenSequence;
72         
73         CookieImpl (
74             Document JavaDoc doc,
75             TokenSequence tokenSequence
76         ) {
77             this.doc = doc;
78             this.tokenSequence = tokenSequence;
79         }
80         
81         public JTextComponent JavaDoc getJTextComponent () {
82             if (component == null) {
83                 DataObject dob = NbEditorUtilities.getDataObject (doc);
84                 EditorCookie ec = (EditorCookie) dob.getLookup ().lookup (EditorCookie.class);
85                 if (ec.getOpenedPanes ().length > 0)
86                     component = ec.getOpenedPanes () [0];
87             }
88             return component;
89         }
90         
91         public Document JavaDoc getDocument () {
92             return doc;
93         }
94         
95         public TokenSequence getTokenSequence () {
96             return tokenSequence;
97         }
98     }
99 }
100
101
102
Popular Tags