001: /*
002: * Sun Public License Notice
003: *
004: * The contents of this file are subject to the Sun Public License
005: * Version 1.0 (the "License"). You may not use this file except in
006: * compliance with the License. A copy of the License is available at
007: * http://www.sun.com/
008: *
009: * The Original Code is NetBeans. The Initial Developer of the Original
010: * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
011: * Microsystems, Inc. All Rights Reserved.
012: */
013:
014: package org.netbeans.editor;
015:
016: import javax.swing.text.Position;
017:
018: /**
019: * Marks hold the relative position in the document.
020: *
021: * @author Miloslav Metelka
022: * @version 1.00
023: */
024:
025: /**
026: * Class defining basic type of mark. This is a mark used most frequently. It's
027: * instances are inserted into the leaf plane of the tree.
028: */
029: public class Mark {
030:
031: /** Document marks to which this mark belongs. */
032: private DocOp op;
033:
034: /** Offset at which the mark is located in the document. */
035: int offset;
036:
037: /** Line at which the mark is located in the document. */
038: int line;
039:
040: /**
041: * Bias of the mark. It is either false for
042: * {@link javax.swing.text.Position.Bias.Forward} or true for
043: * {@link javax.swing.text.Position.Bias.Backward}
044: */
045: boolean backwardBias;
046:
047: /** The mark is valid if it's inserted in the marks structure */
048: boolean valid;
049:
050: /** Construct new mark with forward bias. */
051: public Mark() {
052: this (Position.Bias.Forward);
053: }
054:
055: public Mark(Position.Bias bias) {
056: this (bias == Position.Bias.Backward);
057: }
058:
059: /**
060: * Construct new mark.
061: *
062: * @param backwardBias
063: * whether the inserts performed right at the position of this
064: * mark will go after this mark i.e. this mark will not move
065: * forward when inserting right at its position. This flag
066: * corresponds to <tt>Position.Bias.Backward</tt>.
067: */
068: public Mark(boolean backwardBias) {
069: this .backwardBias = backwardBias;
070: }
071:
072: /** Package private constructor for cover marks */
073: Mark(int offset, int line, boolean backwardBias) {
074: this .offset = offset;
075: this .line = line;
076: this .backwardBias = backwardBias;
077: }
078:
079: /** Get the position of this mark */
080: public final int getOffset() throws InvalidMarkException {
081: try {
082: DocOp lop = op; // local copy
083: synchronized (lop) {
084: if (lop == op) {
085: return lop.marks.getOffset(this );
086:
087: } else {
088: throw new InvalidMarkException();
089: }
090: }
091: } catch (NullPointerException e) {
092: throw new InvalidMarkException(e.toString());
093: }
094: }
095:
096: /** Get the line number of this mark */
097: public final int getLine() throws InvalidMarkException {
098: try {
099: DocOp lop = op; // local copy
100: synchronized (lop) {
101: if (lop == op) {
102: return lop.marks.getLine(this );
103:
104: } else {
105: throw new InvalidMarkException();
106: }
107: }
108: } catch (NullPointerException e) {
109: throw new InvalidMarkException(e.toString());
110: }
111: }
112:
113: /**
114: * Get the insertAfter flag. Replaced by {@link #getBackwardBias()}
115: *
116: * @deprecated
117: */
118: public final boolean getInsertAfter() {
119: return backwardBias;
120: }
121:
122: /**
123: * @return true if the mark has backward bias or false if it has forward
124: * bias.
125: */
126: public final boolean getBackwardBias() {
127: return getInsertAfter();
128: }
129:
130: /**
131: * @return the bias of this mark. It will be either
132: * {@link javax.swing.text.Position.Bias.Forward} or
133: * {@link javax.swing.text.Position.Bias.Backward}.
134: */
135: public final Position.Bias getBias() {
136: return backwardBias ? Position.Bias.Backward
137: : Position.Bias.Forward;
138: }
139:
140: final void setOp(DocOp op) {
141: this .op = op;
142: }
143:
144: /**
145: * Mark will no longer represent a valid place in the document. Although it
146: * will not be removed from the structure that holds the marks it will be
147: * done later automatically.
148: */
149: public final void dispose() {
150: try {
151: DocOp lop = op;
152: synchronized (lop) {
153: if (lop == op && valid) {
154: lop.marks.dispose(this );
155:
156: } else { // invalid mark
157: throw new IllegalStateException();
158: }
159: }
160: } catch (NullPointerException e) {
161: throw new IllegalStateException(e.toString());
162: }
163: }
164:
165: /**
166: * Remove mark from the structure holding the marks. The mark can be
167: * inserted again into some document.
168: */
169: public final void remove() throws InvalidMarkException {
170: try {
171: DocOp lop = op;
172: synchronized (op) {
173: if (lop == op && valid) {
174: op.marks.remove(this );
175: op = null;
176:
177: } else { // invalid mark
178: throw new InvalidMarkException();
179: }
180: }
181: } catch (NullPointerException e) {
182: throw new InvalidMarkException(e.toString());
183: }
184: }
185:
186: final void removeDisposed() {
187: op = null;
188: }
189:
190: /**
191: * Compare this mark to some position.
192: *
193: * @param pos
194: * tested position
195: * @return zero - if the marks have the same position less than zero - if
196: * this mark is before the position greater than zero - if this mark
197: * is after the position
198: */
199: public final int compare(int pos) throws InvalidMarkException {
200: return getOffset() - pos;
201: }
202:
203: /**
204: * This function is called from removeUpdater when mark occupies the removal
205: * area. The mark can decide what to do next. If it doesn't redefine this
206: * method it will be simply moved to the begining of removal area. It is
207: * valid to add or remove other mark from this method. It is even possible
208: * (but not very useful) to add the mark to the removal area. However that
209: * mark will not be notified about current removal.
210: *
211: * @deprecated It will not be supported in the future.
212: */
213: protected void removeUpdateAction(int pos, int len) {
214: }
215:
216: /**
217: * @return true if this mark is currently inserted in the document or false
218: * otherwise.
219: */
220: public final boolean isValid() {
221: try {
222: synchronized (op) {
223: return valid;
224: }
225: } catch (NullPointerException e) {
226: return false;
227: }
228: }
229:
230: /** Get info about <CODE>Mark</CODE>. */
231: public String toString() {
232: return "rawOffset=" + offset // NOI18N
233: + ", rawLine=" + line // NOI18N
234: + ", backwardBias=" + backwardBias; // NOI18N
235: }
236:
237: }
|