KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.swing.text.Document JavaDoc;
23 import javax.swing.text.AbstractDocument JavaDoc;
24 import javax.swing.text.Element JavaDoc;
25 import javax.swing.text.AttributeSet JavaDoc;
26 import javax.swing.text.Position JavaDoc;
27 import javax.swing.text.SimpleAttributeSet JavaDoc;
28
29 /**
30  * Line element implementation.
31  * <BR>The implementation consist of only one backward bias mark.
32  * There is a link to next mark to satisfy
33  * {@link javax.swing.text.Element#getEndOffset()}.
34  * <BR>This way allows to have just three objects
35  * (element, element-finalizer, mark) per line of text
36  * compared to seven (element, 2 * (position, position-finalizer, mark))
37  * in regular leaf element.
38  *
39  * @author Miloslav Metelka
40  * @version 1.00
41  */

42 final class LineElement implements Element JavaDoc, Position JavaDoc {
43     
44     /** Parent and root element */
45     private final LineRootElement root;
46     
47     /** Position at the begining of the line */
48     private final Position JavaDoc startPos;
49     
50     /** Next line or null if this is the last line. */
51     private final Position JavaDoc endPos;
52     
53     /** Attributes of this line element */
54     private AttributeSet JavaDoc attributes = null;
55     
56     private Syntax.StateInfo syntaxStateInfo;
57
58     LineElement(LineRootElement root, Position JavaDoc startPos, Position JavaDoc endPos) {
59         assert(startPos != null);
60         assert(endPos != null);
61
62         this.root = root;
63         this.startPos = startPos;
64         this.endPos = endPos;
65     }
66
67     public Document JavaDoc getDocument() {
68         return root.getDocument();
69     }
70
71     public int getOffset() {
72         return getStartOffset();
73     }
74
75     public int getStartOffset() {
76         return startPos.getOffset();
77     }
78     
79     Position JavaDoc getStartPosition() {
80         return startPos;
81     }
82
83     public int getEndOffset() {
84         return endPos.getOffset();
85     }
86     
87     Position JavaDoc getEndPosition() {
88         return endPos;
89     }
90
91     public Element JavaDoc getParentElement() {
92         return root;
93     }
94
95     public String JavaDoc getName() {
96         return AbstractDocument.ParagraphElementName;
97     }
98
99     public AttributeSet JavaDoc getAttributes() {
100         AttributeSet JavaDoc as = attributes;
101         return as == null ? SimpleAttributeSet.EMPTY : as;
102     }
103     
104     public void setAttributes(AttributeSet JavaDoc attributes) {
105         this.attributes = attributes;
106     }
107
108     public int getElementIndex(int offset) {
109         return -1;
110     }
111
112     public int getElementCount() {
113         return 0;
114     }
115
116     public Element JavaDoc getElement(int index) {
117         return null;
118     }
119
120     public boolean isLeaf() {
121         return true;
122     }
123     
124     Syntax.StateInfo getSyntaxStateInfo() {
125         return syntaxStateInfo;
126     }
127
128     void updateSyntaxStateInfo(Syntax syntax) {
129         if (syntaxStateInfo == null) {
130             syntaxStateInfo = syntax.createStateInfo();
131             assert (syntaxStateInfo != null);
132         }
133         syntax.storeState(syntaxStateInfo);
134     }
135
136     void clearSyntaxStateInfo() {
137         syntaxStateInfo = null;
138     }
139
140     public String JavaDoc toString() {
141         return "getStartOffset()=" + getStartOffset() // NOI18N
142
+ ", getEndOffset()=" + getEndOffset() // NOI18N
143
+ ", syntaxStateInfo=" + getSyntaxStateInfo(); // NOI18N
144
}
145
146 }
147
Popular Tags