KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > languages > features > EditorTokenInput


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.languages.features;
21
22 import org.netbeans.modules.languages.parser.TokenInput;
23 import org.netbeans.api.languages.ASTToken;
24 import org.netbeans.api.lexer.Token;
25 import org.netbeans.api.lexer.TokenHierarchy;
26 import org.netbeans.api.lexer.TokenSequence;
27 import org.netbeans.api.languages.ASTToken;
28 import org.openide.text.NbDocument;
29
30 import javax.swing.text.StyledDocument JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Set JavaDoc;
34
35
36 /**
37  *
38  * @author Jan Jancura
39  */

40 public class EditorTokenInput extends TokenInput {
41
42     private TokenSequence tokenSequence;
43     private List JavaDoc tokens = new ArrayList JavaDoc ();
44     private int index = 0;
45     private Set JavaDoc filter;
46     private StyledDocument JavaDoc doc;
47     private String JavaDoc mimeType;
48     
49
50     public static EditorTokenInput create (
51         Set JavaDoc filter,
52         StyledDocument JavaDoc doc
53     ) {
54         return new EditorTokenInput (filter, doc);
55     }
56
57     private EditorTokenInput (
58         Set JavaDoc filter,
59         StyledDocument JavaDoc doc
60     ) {
61         tokenSequence = TokenHierarchy.get (doc).tokenSequence ();
62         this.filter = filter;
63         this.doc = doc;
64         mimeType = tokenSequence.language ().mimeType ();
65     }
66
67     public ASTToken next (int i) {
68         while (index + i - 1 >= tokens.size ()) {
69             ASTToken token = nextToken ();
70             if (token == null) return null;
71             tokens.add (token);
72         }
73         return (ASTToken) tokens.get (index + i - 1);
74     }
75     
76     private ASTToken nextToken () {
77         do {
78             if (!tokenSequence.moveNext ()) return null;
79         } while (
80             filter.contains (
81                 tokenSequence.token ().id ().name ()
82             )
83         );
84         Token token = tokenSequence.token ();
85         return ASTToken.create (
86             tokenSequence.language ().mimeType (),
87             token.id ().name (),
88             token.text ().toString (),
89             tokenSequence.offset ()
90         );
91     }
92
93     public boolean eof () {
94         return next (1) == null;
95     }
96
97     public int getIndex () {
98         return index;
99     }
100
101     public int getOffset () {
102         ASTToken t = null;
103         if (eof ()) {
104             if (getIndex () == 0) return 0;
105             t = ((ASTToken) tokens.get (tokens.size () - 1));
106             return t.getOffset () + t.getLength ();
107         } else {
108             t = (ASTToken) next (1);
109             return t.getOffset ();
110         }
111     }
112
113     public ASTToken read () {
114         ASTToken next = next (1);
115         index++;
116         return next;
117     }
118
119     public void setIndex (int index) {
120         this.index = index;
121     }
122
123     public String JavaDoc getString (int from) {
124         throw new InternalError JavaDoc ();
125     }
126     
127     public String JavaDoc toString () {
128         int offset = next (1) == null ?
129             doc.getLength () : next (1).getOffset ();
130         int lineNumber = NbDocument.findLineNumber (doc, offset);
131         return (String JavaDoc) doc.getProperty ("title") + ":" +
132             (lineNumber + 1) + "," +
133             (offset - NbDocument.findLineOffset (doc, lineNumber) + 1);
134 // StringBuffer sb = new StringBuffer ();
135
// TokenItem t = next;
136
// int i = 0;
137
// while (i < 10) {
138
// if (t == null) break;
139
// EditorToken et = (EditorToken) t.getTokenID ();
140
// sb.append (Token.create (
141
// et.getMimeType (),
142
// et.getType (),
143
// t.getImage (),
144
// null
145
// ));
146
// t = t.getNext ();
147
// i++;
148
// }
149
// return sb.toString ();
150
}
151 }
152
Popular Tags