001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.spi.lexer;
043:
044: import org.netbeans.api.lexer.InputAttributes;
045: import org.netbeans.api.lexer.Language;
046:
047: /**
048: * Mutable attributed character sequence allowing to listen for changes in its text.
049: *
050: * <p>
051: * The input can temporarily be made inactive which leads to dropping
052: * of all the present tokens for the input until the input becomes
053: * active again.
054: * </p>
055: *
056: * @author Miloslav Metelka
057: * @version 1.00
058: */
059:
060: public abstract class MutableTextInput<I> {
061:
062: private TokenHierarchyControl<I> thc;
063:
064: /**
065: * Get the language suitable for lexing of this input.
066: *
067: * @return language language by which the text of this input should be lexed.
068: * <br/>
069: * This method is only checked upon creation of token hierarchy.
070: * <br/>
071: * If this method returns null the token hierarchy cannot be created
072: * and will be returned as null upon asking until this method will return
073: * non-null value.
074: */
075: protected abstract Language<?> language();
076:
077: /**
078: * Get the character sequence provided and maintained by this input.
079: */
080: protected abstract CharSequence text();
081:
082: /**
083: * Get lexer-specific information about this input
084: * or null if there is no specific information.
085: * <br>
086: * The attributes are typically retrieved by the lexer.
087: */
088: protected abstract InputAttributes inputAttributes();
089:
090: /**
091: * Get object that logically provides the text
092: * for this mutable text input.
093: * <br/>
094: * For example it may be a swing text document instance
095: * {@link javax.swing.text.Document} in case the result of {@link #text()}
096: * is the content of the document.
097: *
098: * @return non-null mutable input source.
099: */
100: protected abstract I inputSource();
101:
102: /**
103: * Called by infrastructure to check if the underlying input source is currently
104: * read-locked by the current thread.
105: * <br/>
106: * This method is only called when turning on the logger for token hierarchy updates:
107: * <code>-J-Dorg.netbeans.lib.lexer.TokenHierarchyUpdate.level=FINE</code>
108: * <br/>
109: * It is expected that if write-lock means read-lock too i.e. if {@link #isWriteLocked()}
110: * returns true that this method will also return true automatically.
111: * <br/>
112: * The following operations require read-locking:
113: * <ul>
114: * <li>Creation and using of token sequence {@link org.netbeans.api.lexer.TokenHierarchy#tokenSequence()}.</li>
115: * <li>Creation of token sequence list {@link org.netbeans.api.lexer.TokenHierarchy#tokenSequenceList(LanguagePath,int,int)}.</li>
116: * </ul>
117: *
118: * @return true if the underlying input source is read-locked by the current thread
119: * (or if unsure e.g. if there is a read-lock present but it's not possible
120: * to determine whether it was this or other thread that performed the locking).
121: * <br/>
122: * Returning false means that the input source is surely unlocked which will be
123: * reported as a serious error by the infrastructure.
124: */
125: protected abstract boolean isReadLocked();
126:
127: /**
128: * Called by infrastructure to check if the underlying input source is currently
129: * write-locked by the current thread.
130: * <br/>
131: * This method is only called when turning on the logger for token hierarchy updates:
132: * <code>-J-Dorg.netbeans.lib.lexer.TokenHierarchyUpdate.level=FINE</code>
133: * <br/>
134: * The following operations require write-locking:
135: * <ul>
136: * <li>Text modification {@link org.netbeans.spi.lexer.TokenHierarchyControl#textModified(int,int,CharSequence,int)}</li>
137: * <li>Creation of custom embedding by {@link org.netbeans.api.lexer.TokenSequence#createEmbedding(Language,int,int)}.</li>
138: * <li>Removal of custom embedding by {@link org.netbeans.api.lexer.TokenSequence#removeEmbedding(Language)}.</li>
139: * </ul>
140: *
141: * @return true if the underlying input source is write-locked by the current thread
142: * (or if unsure).
143: * Returning false means that the input source is surely unlocked which will be
144: * reported as a serious error by the infrastructure.
145: */
146: protected abstract boolean isWriteLocked();
147:
148: /**
149: * Get token hierarchy control for this mutable text input.
150: * <br>
151: * Each mutable text input can hold it in a specific way
152: * e.g. swing document can use
153: * <code>getProperty(TokenHierarchyControl.class)</code>.
154: */
155: public final TokenHierarchyControl<I> tokenHierarchyControl() {
156: synchronized (this ) {
157: if (thc == null) {
158: thc = new TokenHierarchyControl<I>(this);
159: }
160: return thc;
161: }
162: }
163:
164: }
|