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: import javax.swing.text.Document;
045: import javax.swing.text.AbstractDocument;
046: import javax.swing.text.Element;
047: import javax.swing.text.AttributeSet;
048: import javax.swing.text.Position;
049: import javax.swing.text.SimpleAttributeSet;
050:
051: /**
052: * Line element implementation.
053: * <BR>The implementation consist of only one backward bias mark.
054: * There is a link to next mark to satisfy
055: * {@link javax.swing.text.Element#getEndOffset()}.
056: * <BR>This way allows to have just three objects
057: * (element, element-finalizer, mark) per line of text
058: * compared to seven (element, 2 * (position, position-finalizer, mark))
059: * in regular leaf element.
060: *
061: * @author Miloslav Metelka
062: * @version 1.00
063: */
064: final class LineElement implements Element, Position {
065:
066: /** Parent and root element */
067: private final LineRootElement root;
068:
069: /** Position at the begining of the line */
070: private final Position startPos;
071:
072: /** Next line or null if this is the last line. */
073: private final Position endPos;
074:
075: /** Attributes of this line element */
076: private AttributeSet attributes = null;
077:
078: private Syntax.StateInfo syntaxStateInfo;
079:
080: LineElement(LineRootElement root, Position startPos, Position endPos) {
081: assert (startPos != null);
082: assert (endPos != null);
083:
084: this .root = root;
085: this .startPos = startPos;
086: this .endPos = endPos;
087: }
088:
089: public Document getDocument() {
090: return root.getDocument();
091: }
092:
093: public int getOffset() {
094: return getStartOffset();
095: }
096:
097: public int getStartOffset() {
098: return startPos.getOffset();
099: }
100:
101: Position getStartPosition() {
102: return startPos;
103: }
104:
105: public int getEndOffset() {
106: return endPos.getOffset();
107: }
108:
109: Position getEndPosition() {
110: return endPos;
111: }
112:
113: public Element getParentElement() {
114: return root;
115: }
116:
117: public String getName() {
118: return AbstractDocument.ParagraphElementName;
119: }
120:
121: public AttributeSet getAttributes() {
122: AttributeSet as = attributes;
123: return as == null ? SimpleAttributeSet.EMPTY : as;
124: }
125:
126: public void setAttributes(AttributeSet attributes) {
127: this .attributes = attributes;
128: }
129:
130: public int getElementIndex(int offset) {
131: return -1;
132: }
133:
134: public int getElementCount() {
135: return 0;
136: }
137:
138: public Element getElement(int index) {
139: return null;
140: }
141:
142: public boolean isLeaf() {
143: return true;
144: }
145:
146: Syntax.StateInfo getSyntaxStateInfo() {
147: return syntaxStateInfo;
148: }
149:
150: void updateSyntaxStateInfo(Syntax syntax) {
151: if (syntaxStateInfo == null) {
152: syntaxStateInfo = syntax.createStateInfo();
153: assert (syntaxStateInfo != null);
154: }
155: syntax.storeState(syntaxStateInfo);
156: }
157:
158: void clearSyntaxStateInfo() {
159: syntaxStateInfo = null;
160: }
161:
162: public String toString() {
163: return "getStartOffset()=" + getStartOffset() // NOI18N
164: + ", getEndOffset()=" + getEndOffset() // NOI18N
165: + ", syntaxStateInfo=" + getSyntaxStateInfo(); // NOI18N
166: }
167:
168: }
|