KickJava   Java API By Example, From Geeks To Geeks.

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


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.Element JavaDoc;
23 import javax.swing.text.AttributeSet JavaDoc;
24 import javax.swing.text.BadLocationException JavaDoc;
25
26 /**
27 * Leaf element is used on the leaf level of element tree.
28 *
29 * @author Miloslav Metelka
30 * @version 0.10
31 */

32
33 public class LeafElement extends BaseElement {
34
35     /** Mark giving start offset of this element */
36     protected Mark startMark;
37
38     /** Mark giving end offset of this element */
39     protected Mark endMark;
40
41     /** Does this view begin at line begining */
42     protected boolean bol;
43
44     /** Does this view end at line end */
45     protected boolean eol;
46
47     /** Create new document instance */
48     public LeafElement(BaseDocument doc, BaseElement parent, AttributeSet JavaDoc attrs,
49                        int startOffset, int endOffset, boolean bol, boolean eol) {
50         super(doc, parent, attrs);
51         this.bol = bol;
52         this.eol = eol;
53         // create marks for element start and end
54
try {
55             startMark = new Mark(true);
56             endMark = new Mark(false);
57             startMark.insert(doc, startOffset);
58             endMark.insert(doc, endOffset);
59         } catch (BadLocationException JavaDoc e) {
60             Utilities.annotateLoggable(e);
61         } catch (InvalidMarkException e) {
62             throw new IllegalStateException JavaDoc(e.toString());
63         }
64     }
65
66     protected void finalize() throws Throwable JavaDoc {
67         try {
68             startMark.remove();
69             endMark.remove();
70         } catch (InvalidMarkException e) {
71         }
72         super.finalize();
73     }
74
75     /** Get start mark of this element */
76     public final Mark getStartMark() {
77         return startMark;
78     }
79
80     /** Get start offset of this element */
81     public final int getStartOffset() {
82         try {
83             return startMark.getOffset();
84         } catch (InvalidMarkException e) {
85             return 0;
86         }
87     }
88
89     /** Get end mark of this element */
90     public final Mark getEndMark() {
91         return endMark;
92     }
93
94     /** Get end offset of this element */
95     public final int getEndOffset() {
96         try {
97             return endMark.getOffset();
98         } catch (InvalidMarkException e) {
99             return 0;
100         }
101     }
102
103     /** Is this view begining at begin of line */
104     public final boolean isBOL() {
105         return bol;
106     }
107
108     /** Is this view ending at end of line ? */
109     public final boolean isEOL() {
110         return eol;
111     }
112
113     /** Gets the child element index closest to the given offset.
114     * For leaf element this returns -1.
115     */

116     public int getElementIndex(int offset) {
117         return -1;
118     }
119
120     /** Get number of children of this element */
121     public int getElementCount() {
122         return 0;
123     }
124
125     /** Get child of this element at specified index or itself
126     * if the index is too big
127     */

128     public Element JavaDoc getElement(int index) {
129         return null;
130     }
131
132     /** Does this element have any children? */
133     public boolean isLeaf() {
134         return true;
135     }
136
137     public String JavaDoc toString() {
138         return "startOffset=" + getStartOffset() // NOI18N
139
+ ", endOffset=" + getEndMark(); // NOI18N
140
}
141
142 }
143
Popular Tags