01: /*
02: * Sun Public License Notice
03: *
04: * The contents of this file are subject to the Sun Public License
05: * Version 1.0 (the "License"). You may not use this file except in
06: * compliance with the License. A copy of the License is available at
07: * http://www.sun.com/
08: *
09: * The Original Code is NetBeans. The Initial Developer of the Original
10: * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
11: * Microsystems, Inc. All Rights Reserved.
12: */
13:
14: package org.netbeans.editor;
15:
16: import javax.swing.text.BadLocationException;
17: import javax.swing.text.Position;
18:
19: /**
20: * Position in document. This is enhanced version of Swing <CODE>Position</CODE>
21: * interface. It supports insert after feature. If Position has <CODE>insertAfter</CODE>
22: * flag set and text is inserted right at the mark's position, the position will
23: * NOT move.
24: *
25: * @author Miloslav Metelka
26: * @version 1.00
27: */
28:
29: class BasePosition implements Position {
30:
31: /** The mark that serves this position */
32: private Mark mark;
33:
34: /** Construct new position at specified offset */
35: BasePosition(DocOp op, int offset) throws BadLocationException {
36: this (op, offset, Position.Bias.Forward);
37: }
38:
39: /** Construct new position with insert after flag specified */
40: BasePosition(DocOp op, int offset, Position.Bias bias)
41: throws BadLocationException {
42: mark = op.insertMark(offset, bias == Position.Bias.Backward);
43: }
44:
45: /** Get offset in document for this position */
46: public int getOffset() {
47: try {
48: return mark.getOffset();
49: } catch (InvalidMarkException e) {
50: if (Boolean.getBoolean("netbeans.debug.exceptions")) { // NOI18N
51: e.printStackTrace();
52: }
53: return 0;
54: }
55: }
56:
57: /** Remove mark in finalize method */
58: protected void finalize() throws Throwable {
59: mark.remove();
60: super.finalize();
61: }
62:
63: }
|