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-2006 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.editor;
043:
044: /**
045: * Token-item presents a token as a piece information
046: * without dependence on a character buffer and it enables
047: * to chain the token-items in both directions.
048: *
049: * @author Miloslav Metelka
050: * @version 1.00
051: */
052:
053: public interface TokenItem {
054:
055: /** Get the token-id of this token-item */
056: public TokenID getTokenID();
057:
058: /** Get the token-id of this token-item */
059: public TokenContextPath getTokenContextPath();
060:
061: /** Get the position of the token in the document */
062: public int getOffset();
063:
064: /** Get the image of this token. */
065: public String getImage();
066:
067: /** Get next token-item in the text. It returns null
068: * if there's no more next tokens in the text. It can throw
069: * <tt>IllegalStateException</tt> in case the document
070: * was changed so the token-item chain becomes invalid.
071: */
072: public TokenItem getNext();
073:
074: /** Get previous token-item in the text. It returns null
075: * if there's no more previous tokens in the text. It can throw
076: * <tt>IllegalStateException</tt> in case the document
077: * was changed so the token-item chain becomes invalid.
078: */
079: public TokenItem getPrevious();
080:
081: /** Abstract implementation that doesn't contain chaining methods. */
082: public static abstract class AbstractItem implements TokenItem {
083:
084: private TokenID tokenID;
085:
086: private TokenContextPath tokenContextPath;
087:
088: private String image;
089:
090: private int offset;
091:
092: public AbstractItem(TokenID tokenID,
093: TokenContextPath tokenContextPath, int offset,
094: String image) {
095: this .tokenID = tokenID;
096: this .tokenContextPath = tokenContextPath;
097: this .offset = offset;
098: this .image = image;
099: }
100:
101: public TokenID getTokenID() {
102: return tokenID;
103: }
104:
105: public TokenContextPath getTokenContextPath() {
106: return tokenContextPath;
107: }
108:
109: public int getOffset() {
110: return offset;
111: }
112:
113: public String getImage() {
114: return image;
115: }
116:
117: public String toString() {
118: return "'"
119: + org.netbeans.editor.EditorDebug
120: .debugString(getImage()) // NOI18N
121: + "', tokenID=" + getTokenID() + ", tcp="
122: + getTokenContextPath() // NOI18N
123: + ", offset=" + getOffset(); // NOI18N
124: }
125:
126: }
127:
128: /** Implementation useful for delegation. */
129: public static class FilterItem implements TokenItem {
130:
131: protected TokenItem delegate;
132:
133: public FilterItem(TokenItem delegate) {
134: this .delegate = delegate;
135: }
136:
137: public TokenID getTokenID() {
138: return delegate.getTokenID();
139: }
140:
141: public TokenContextPath getTokenContextPath() {
142: return delegate.getTokenContextPath();
143: }
144:
145: public int getOffset() {
146: return delegate.getOffset();
147: }
148:
149: public String getImage() {
150: return delegate.getImage();
151: }
152:
153: public TokenItem getNext() {
154: return delegate.getNext();
155: }
156:
157: public TokenItem getPrevious() {
158: return delegate.getPrevious();
159: }
160:
161: public String toString() {
162: return delegate.toString();
163: }
164:
165: }
166:
167: }
|