KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > link > LinkedPosition


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.text.link;
12
13 import org.eclipse.core.runtime.Assert;
14
15 import org.eclipse.jface.text.BadLocationException;
16 import org.eclipse.jface.text.DocumentEvent;
17 import org.eclipse.jface.text.IDocument;
18 import org.eclipse.jface.text.Position;
19
20 /**
21  * A <code>Position</code> on a document that knows which document it is
22  * registered with and has a sequence number for tab stops.
23  * <p>
24  * Clients may extend this class.
25  * </p>
26  * @since 3.0
27  */

28 public class LinkedPosition extends Position {
29
30     /** The document this position belongs to. */
31     private IDocument fDocument;
32     private int fSequenceNumber;
33
34     /**
35      * Creates a new instance.
36      *
37      * @param document the document
38      * @param offset the offset of the position
39      * @param length the length of the position
40      * @param sequence the iteration sequence rank
41      */

42     public LinkedPosition(IDocument document, int offset, int length, int sequence) {
43         super(offset, length);
44         Assert.isNotNull(document);
45         fDocument= document;
46         fSequenceNumber= sequence;
47     }
48
49     /**
50      * Creates a new instance. Equivalent to calling
51      * <code>LinkedPosition(document, offset, length, LinkedPositionGroup.NO_STOP)</code>
52      *
53      * @param document the document
54      * @param offset the offset of the position
55      * @param length the length of the position
56      */

57     public LinkedPosition(IDocument document, int offset, int length) {
58         this(document, offset, length, LinkedPositionGroup.NO_STOP);
59     }
60
61     /**
62      * @return Returns the document.
63      */

64     public IDocument getDocument() {
65         return fDocument;
66     }
67
68     /*
69      * @see org.eclipse.jface.text.Position#equals(java.lang.Object)
70      */

71     public boolean equals(Object JavaDoc other) {
72         if (other instanceof LinkedPosition) {
73             LinkedPosition p= (LinkedPosition) other;
74             return p.offset == offset && p.length == length && p.fDocument == fDocument;
75         }
76         return false;
77     }
78
79     /**
80      * Returns whether this position overlaps with <code>position</code>.
81      *
82      * @param position the position to check.
83      * @return <code>true</code> if this position overlaps with
84      * <code>position</code>,<code>false</code> otherwise
85      */

86     public boolean overlapsWith(LinkedPosition position) {
87         return position.getDocument() == fDocument && overlapsWith(position.getOffset(), position.getLength());
88     }
89
90     /**
91      * Returns whether this position includes <code>event</code>.
92      *
93      * @param event the event to check.
94      * @return <code>true</code> if this position includes <code>event</code>,
95      * <code>false</code> otherwise
96      */

97     public boolean includes(DocumentEvent event) {
98         return includes(event.getDocument(), event.getOffset(), event.getLength());
99     }
100
101     /**
102      * Returns whether this position includes <code>position</code>.
103      *
104      * @param position the position to check.
105      * @return <code>true</code> if this position includes
106      * <code>position</code>,<code>false</code> otherwise
107      */

108     public boolean includes(LinkedPosition position) {
109         return includes(position.getDocument(), position.getOffset(), position.getLength());
110     }
111
112     /**
113      * Overrides {@link Position#includes(int)}so every offset is considered
114      * included that lies in between the first and last offset of this position,
115      * and offsets that are right at the end of the position.
116      *
117      * @param pOffset the offset to check
118      * @return <code>true</code> if <code>pOffset</code> is in
119      * <code>[offset, offset + length]</code>
120      */

121     public boolean includes(int pOffset) {
122         return this.offset <= pOffset && pOffset <= this.offset + this.length;
123     }
124
125     /**
126      * Returns whether this position includes the range given by
127      * <code>offset</code> and <code>length</code>. A range is included by
128      * a <code>LinkedPosition</code> if {@link #includes(int) includes(offset)}
129      * returns true for every offset in the range, including the borders of the
130      * range.
131      *
132      * @param doc the document that the given range refers to, may be <code>null</code>
133      * @param off the offset of the range, referring to <code>document</code>
134      * @param len the length of the range
135      * @return <code>true</code> if <code>doc</code> is the same document as
136      * this position refers to, and if the entire range is included in
137      * this position
138      */

139     protected boolean includes(IDocument doc, int off, int len) {
140         return doc == fDocument && off >= offset && len + off <= offset + length;
141
142     }
143
144     /**
145      * Returns the content of this position on the referenced document.
146      *
147      * @return the content of the document at this position
148      * @throws BadLocationException if the position is not valid
149      */

150     public String JavaDoc getContent() throws BadLocationException {
151         return fDocument.get(offset, length);
152     }
153
154     /**
155      * Returns the sequence number of this position.
156      *
157      * @return the sequence number of this position
158      */

159     public int getSequenceNumber() {
160         return fSequenceNumber;
161     }
162
163     /**
164      * Sets the sequence number of this position.
165      *
166      * @param sequence the new sequence number
167      */

168     public void setSequenceNumber(int sequence) {
169         fSequenceNumber= sequence;
170     }
171
172     /*
173      * @see org.eclipse.jface.text.Position#hashCode()
174      */

175     public int hashCode() {
176         return fDocument.hashCode() | super.hashCode() | fSequenceNumber;
177     }
178 }
179
Popular Tags