KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > lexer > TokenHierarchyControl


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.spi.lexer;
21
22 import org.netbeans.api.lexer.Language;
23 import org.netbeans.api.lexer.TokenHierarchy;
24 import org.netbeans.api.lexer.TokenId;
25 import org.netbeans.lib.lexer.TokenHierarchyOperation;
26
27 /**
28  * Control class for managing token hierarchy of a mutable text input.
29  *
30  * @author Miloslav Metelka
31  * @version 1.00
32  */

33
34 public final class TokenHierarchyControl<I> {
35
36     private MutableTextInput<I> input;
37
38     private TokenHierarchyOperation<I,?> operation;
39
40     TokenHierarchyControl(MutableTextInput<I> input) {
41         this.input = input;
42     }
43     
44     private <T extends TokenId> void init() {
45         Language<? extends TokenId> language = input.language();
46         if (language != null) {
47             this.operation = createOperation(language);
48         }
49     }
50     
51     private <T extends TokenId> TokenHierarchyOperation<I,T> createOperation(Language<T> language) {
52         return new TokenHierarchyOperation<I,T>(input, language);
53     }
54     
55     public synchronized TokenHierarchy<I> tokenHierarchy() {
56         if (operation == null) {
57             init();
58         }
59         return (operation != null)
60             ? operation.tokenHierarchy()
61             : null;
62     }
63     
64     /**
65      * Notify that the text of the mutable text input was modified.
66      *
67      * @param offset &gt;=0 offset where the modification occurred.
68      * @param removedLength &gt;=0 number of characters removed from the input.
69      * @param removedText text removed from the input. If it's not available
70      * to determine the removed text then this parameter may be null.
71      * <br>
72      * Providing of the removed text allows the incremental
73      * algorithm to use an efficient token validation if possible.
74      * @param insertedLength &gt;=0 number of characters inserted at the offset
75      * after the removal.
76      */

77     public void textModified(int offset,
78     int removedLength, CharSequence JavaDoc removedText,
79     int insertedLength) {
80         if (operation != null) {
81             operation.textModified(offset, removedLength, removedText, insertedLength);
82         }
83     }
84
85     /**
86      * Making the token hierarchy inactive will release all the tokens in the hierarchy
87      * so that there will be no tokens.
88      */

89     public void setActive(boolean active) {
90         if (operation != null) {
91             operation.setActive(active);
92         }
93     }
94     
95     public boolean isActive() {
96         return (operation != null) ? operation.isActive() : false;
97     }
98
99     /**
100      * Rebuild token hierarchy completely.
101      * <br/>
102      * This may be necessary if lexing depends on some input properties
103      * that get changed.
104      * <br/>
105      * This method will drop all present tokens and let them to be lazily recreated.
106      * <br/>
107      * This method should only be invoked under modification lock over the mutable
108      * input source (e.g. a document's write-lock).
109      * Otherwise all the active token sequences would fail with
110      * <code>ConcurrentModificationException</code>.
111      */

112     public void rebuild() {
113         operation.rebuild();
114     }
115
116 }
117
Popular Tags