KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > SyntaxUpdateTokens


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.editor;
21
22 import java.util.Collections JavaDoc;
23 import java.util.List JavaDoc;
24 import javax.swing.event.DocumentEvent JavaDoc;
25 import javax.swing.text.AbstractDocument JavaDoc;
26 import javax.swing.text.BadLocationException JavaDoc;
27 import javax.swing.text.Document JavaDoc;
28 import javax.swing.text.Element JavaDoc;
29 import javax.swing.undo.AbstractUndoableEdit JavaDoc;
30 import org.openide.ErrorManager;
31
32 /**
33  * Notification about the tokens being relexed as result of a document
34  * modification.
35  * <br>
36  * The SPIs may use <code>Document.putProperty(SyntaxUpdateNotify.class, inst)</code>
37  * to assign an instance of this class to the particular document.
38  * <br>
39  * The client <code>DocumentListener</code>s can then retrieve
40  * the list of the relexed tokens of interest by using
41  *
42  * @author Miloslav Metelka
43  * @version 1.00
44  */

45 public abstract class SyntaxUpdateTokens {
46     
47     /**
48      * Retrieve unmodifiable list of relexed tokens of interest for the particular
49      * document event.
50      *
51      * @param evt document event for which the list of tokens is being retrieved.
52      * @return list of {@link TokenInfo}s describing the tokens of interest
53      * that were relexed because of the document modification.
54      */

55     public static List JavaDoc getTokenInfoList(DocumentEvent JavaDoc evt) {
56         if (!(evt instanceof BaseDocumentEvent)) {
57             return Collections.EMPTY_LIST;
58         }
59         
60         return ((BaseDocumentEvent)evt).getSyntaxUpdateTokenList();
61     }
62
63     /**
64      * Create list of tokens of interest for the whole document.
65      * <br>
66      * Document is readlocked during this operation.
67      *
68      * @param doc document for which the list of tokens is being retrieved.
69      * @return list of {@link TokenInfo}s describing the tokens of interest
70      * throughout the whole document.
71      */

72     public static List JavaDoc getTokenInfoList(Document JavaDoc doc) {
73         SyntaxUpdateTokens suTokens = (SyntaxUpdateTokens)doc.getProperty(SyntaxUpdateTokens.class);
74         
75         if (suTokens == null || !(doc instanceof BaseDocument)) {
76             return Collections.EMPTY_LIST;
77         }
78         
79         List JavaDoc tokenList;
80         BaseDocument bdoc = (BaseDocument)doc;
81         bdoc.readLock();
82         try {
83             suTokens.syntaxUpdateStart();
84             try {
85                 bdoc.getSyntaxSupport().tokenizeText(
86                     new AllTokensProcessor(suTokens), 0, bdoc.getLength(), true);
87             } catch (BadLocationException JavaDoc e) {
88                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
89             } finally {
90                 tokenList = suTokens.syntaxUpdateEnd();
91             }
92             
93         } finally {
94             bdoc.readUnlock();
95         }
96         return tokenList;
97     }
98     
99     /**
100      * Notification that updating of the lexical states is starting
101      * for the last performed modification.
102      * <br>
103      * The list of the token infos should be cleared here.
104      */

105     public abstract void syntaxUpdateStart();
106     
107     /**
108      * Notification that updating of lexical states has ended
109      * so there will be no more tokens relexed for the modification.
110      * <br>
111      * If there are any tokens of interest they should be returned
112      * from this method.
113      *
114      * @return list of tokens of interest. The returned list will
115      * be copied by the infrastructure so the originally returned
116      * instance can continue to be used.
117      */

118     public abstract List JavaDoc syntaxUpdateEnd();
119     
120     /**
121      * Notification that a token was found during updating
122      * of lexical states. If this class is interested in providing
123      * info about this token to clients then it should create
124      *
125      */

126     public abstract void syntaxUpdateToken(TokenID id, TokenContextPath contextPath, int offset, int length);
127     
128     
129     public class TokenInfo {
130         
131         private final TokenID id;
132         
133         private final TokenContextPath contextPath;
134         
135         private final int offset;
136         
137         private final int length;
138         
139         public TokenInfo(TokenID id, TokenContextPath contextPath, int offset, int length) {
140             this.id = id;
141             this.contextPath = contextPath;
142             this.offset = offset;
143             this.length = length;
144         }
145         
146         public final TokenID getID() {
147             return id;
148         }
149         
150         public final TokenContextPath getContextPath() {
151             return contextPath;
152         }
153         
154         public final int getOffset() {
155             return offset;
156         }
157         
158         public int getLength() {
159             return length;
160         }
161         
162         public String JavaDoc toString() {
163             return "id=" + id + ", ctx=" + contextPath + ", off=" + offset + ", len=" + length; // NOI18N
164
}
165
166     }
167     
168     static final class AllTokensProcessor implements TokenProcessor {
169         
170         private SyntaxUpdateTokens suTokens;
171         
172         private int bufferStartOffset;
173         
174         AllTokensProcessor(SyntaxUpdateTokens suTokens) {
175             this.suTokens = suTokens;
176         }
177         
178         public void nextBuffer(char[] buffer, int offset, int len, int startPos, int preScan, boolean lastBuffer) {
179             bufferStartOffset = startPos - offset;
180         }
181         
182         public boolean token(TokenID tokenID, TokenContextPath tokenContextPath, int tokenBufferOffset, int tokenLength) {
183             suTokens.syntaxUpdateToken(tokenID, tokenContextPath, tokenBufferOffset, tokenLength);
184             return true;
185         }
186         
187         public int eot(int offset) {
188             return 0;
189         }
190         
191     }
192
193
194
195 }
196
Popular Tags