KickJava   Java API By Example, From Geeks To Geeks.

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


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.Position JavaDoc;
23
24
25 /**
26  * Multipurpose mark that can be used
27  * both as the traditional swing mark
28  * or the bias mark.
29  *
30  * @author Miloslav Metelka
31  * @version 1.00
32  */

33
34 final class MultiMark {
35
36     /** Whether mark has a backward (or forward) bias */
37     static final int BACKWARD_BIAS = 1;
38
39     /** Whether mark was disposed and can no longer be used */
40     static final int VALID = 2;
41
42     /** Storage of the marks uses this flag
43      * to indicate that the diposed mark was physically removed
44      * from the underlying array.
45      */

46     static final int REMOVED = 4;
47     
48     /** Whether mark behaves so that it conforms to the behavior
49      * of the swing positions. This behavior requires the mark
50      * to keep its offset to be zero once the mark
51      * reaches the zero offset (by removal in the document).
52      */

53     static final int COMPATIBLE = 8;
54     
55     /** Whether the compatible mark has zero offset regardless of
56      * what MarkVector.getOffset() would return.
57      */

58     static final int ZERO = 16;
59     
60     /** Offset at which the mark is located in the document. */
61     int rawOffset;
62
63     /** Composition of the flags */
64     int flags;
65     
66     /** Mark vector that hosts this mark. */
67     MarkVector markVector;
68     
69     /** Construct compatible mark */
70     MultiMark(MarkVector markVector, int offset) {
71         this(markVector, offset,
72             (offset != 0) ? COMPATIBLE : (COMPATIBLE | ZERO | BACKWARD_BIAS));
73     }
74     
75     /** Construct bias mark */
76     MultiMark(MarkVector markVector, int offset, Position.Bias JavaDoc bias) {
77         this(markVector, offset,
78             (bias == Position.Bias.Backward) ? BACKWARD_BIAS : 0);
79     }
80         
81     /** Construct new mark. The mark is invalid by default.
82      */

83     private MultiMark(MarkVector markVector, int offset, int flags) {
84         this.markVector = markVector;
85         this.rawOffset = offset; // will be corrected once the mark is inserted
86
this.flags = flags;
87     }
88
89     /** @return the bias of this mark. It will be either
90      * {@link javax.swing.text.Position.Bias.Forward}
91      * or {@link javax.swing.text.Position.Bias.Backward}.
92      */

93     public Position.Bias JavaDoc getBias() {
94         return ((flags & BACKWARD_BIAS) != 0)
95             ? Position.Bias.Backward
96             : Position.Bias.Forward;
97     }
98     
99     /** Get the position of this mark */
100     public int getOffset() {
101         synchronized (markVector) {
102             if ((flags & VALID) != 0) {
103                 return ((flags & ZERO) == 0)
104                     ? markVector.getOffset(rawOffset)
105                     : 0;
106             } else { // already disposed
107
throw new IllegalStateException JavaDoc();
108             }
109         }
110     }
111
112     /** Mark will no longer represent a valid place in the document.
113      * Attempts to use the mark will result into throwing of
114      * {@link java.lang.IllegalStateException}.
115      * @throws IllegalStateException if the mark was already disposed before.
116      */

117     public void dispose() {
118         synchronized (markVector) {
119             if ((flags & VALID) != 0) {
120                 flags &= ~VALID;
121                 markVector.notifyMarkDisposed();
122             } else { // already disposed before
123
throw new IllegalStateException JavaDoc();
124             }
125         }
126     }
127     
128     /** @return true if this mark was not disposed yet.
129      */

130     public boolean isValid() {
131         synchronized(markVector) {
132             return ((flags & VALID) != 0);
133         }
134     }
135
136     public String JavaDoc toString() {
137         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
138         synchronized(markVector) {
139             if ((flags & VALID) != 0) {
140                 sb.append("offset=" + getOffset()); // NOI18N
141
} else {
142                 sb.append("removed"); // NOI18N
143
}
144             sb.append(", bias="); // NOI18N
145
sb.append(getBias());
146             
147             return sb.toString();
148         }
149     }
150
151     public String JavaDoc toStringDetail() {
152         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
153         synchronized(markVector) {
154             sb.append(System.identityHashCode(this));
155             sb.append(" ("); // NOI18N
156
sb.append(rawOffset);
157             sb.append(" -> "); // NOI18N
158
if ((flags & VALID) != 0) {
159                 sb.append(getOffset());
160             } else {
161                 sb.append('X');
162                 sb.append(markVector.getOffset(rawOffset));
163                 sb.append('X');
164             }
165             sb.append(", "); // NOI18N
166
sb.append(((flags & BACKWARD_BIAS) != 0) ? 'B' : 'F');
167             if ((flags & VALID) != 0) {
168                 sb.append('V');
169             }
170             if ((flags & REMOVED) != 0) {
171                 sb.append('R');
172             }
173             if ((flags & COMPATIBLE) != 0) {
174                 sb.append('C');
175             }
176             if ((flags & ZERO) != 0) {
177                 sb.append('Z');
178             }
179             sb.append(')');
180
181             return sb.toString();
182         }
183     }
184
185
186 }
187
Popular Tags