Mercurial > projects > dwt-addons
comparison dwtx/jface/text/link/LinkedPosition.d @ 129:eb30df5ca28b
Added JFace Text sources
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 23 Aug 2008 19:10:48 +0200 |
parents | |
children | c4fb132a086c |
comparison
equal
deleted
inserted
replaced
128:8df1d4193877 | 129:eb30df5ca28b |
---|---|
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 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module dwtx.jface.text.link.LinkedPosition; | |
14 | |
15 import dwt.dwthelper.utils; | |
16 | |
17 | |
18 import dwtx.core.runtime.Assert; | |
19 import dwtx.jface.text.BadLocationException; | |
20 import dwtx.jface.text.DocumentEvent; | |
21 import dwtx.jface.text.IDocument; | |
22 import dwtx.jface.text.Position; | |
23 | |
24 /** | |
25 * A <code>Position</code> on a document that knows which document it is | |
26 * registered with and has a sequence number for tab stops. | |
27 * <p> | |
28 * Clients may extend this class. | |
29 * </p> | |
30 * @since 3.0 | |
31 */ | |
32 public class LinkedPosition : Position { | |
33 | |
34 /** The document this position belongs to. */ | |
35 private IDocument fDocument; | |
36 private int fSequenceNumber; | |
37 | |
38 /** | |
39 * Creates a new instance. | |
40 * | |
41 * @param document the document | |
42 * @param offset the offset of the position | |
43 * @param length the length of the position | |
44 * @param sequence the iteration sequence rank | |
45 */ | |
46 public LinkedPosition(IDocument document, int offset, int length, int sequence) { | |
47 super(offset, length); | |
48 Assert.isNotNull(document); | |
49 fDocument= document; | |
50 fSequenceNumber= sequence; | |
51 } | |
52 | |
53 /** | |
54 * Creates a new instance. Equivalent to calling | |
55 * <code>LinkedPosition(document, offset, length, LinkedPositionGroup.NO_STOP)</code> | |
56 * | |
57 * @param document the document | |
58 * @param offset the offset of the position | |
59 * @param length the length of the position | |
60 */ | |
61 public LinkedPosition(IDocument document, int offset, int length) { | |
62 this(document, offset, length, LinkedPositionGroup.NO_STOP); | |
63 } | |
64 | |
65 /** | |
66 * @return Returns the document. | |
67 */ | |
68 public IDocument getDocument() { | |
69 return fDocument; | |
70 } | |
71 | |
72 /* | |
73 * @see dwtx.jface.text.Position#equals(java.lang.Object) | |
74 */ | |
75 public bool equals(Object other) { | |
76 if (other instanceof LinkedPosition) { | |
77 LinkedPosition p= (LinkedPosition) other; | |
78 return p.offset is offset && p.length is length && p.fDocument is fDocument; | |
79 } | |
80 return false; | |
81 } | |
82 | |
83 /** | |
84 * Returns whether this position overlaps with <code>position</code>. | |
85 * | |
86 * @param position the position to check. | |
87 * @return <code>true</code> if this position overlaps with | |
88 * <code>position</code>,<code>false</code> otherwise | |
89 */ | |
90 public bool overlapsWith(LinkedPosition position) { | |
91 return position.getDocument() is fDocument && overlapsWith(position.getOffset(), position.getLength()); | |
92 } | |
93 | |
94 /** | |
95 * Returns whether this position includes <code>event</code>. | |
96 * | |
97 * @param event the event to check. | |
98 * @return <code>true</code> if this position includes <code>event</code>, | |
99 * <code>false</code> otherwise | |
100 */ | |
101 public bool includes(DocumentEvent event) { | |
102 return includes(event.getDocument(), event.getOffset(), event.getLength()); | |
103 } | |
104 | |
105 /** | |
106 * Returns whether this position includes <code>position</code>. | |
107 * | |
108 * @param position the position to check. | |
109 * @return <code>true</code> if this position includes | |
110 * <code>position</code>,<code>false</code> otherwise | |
111 */ | |
112 public bool includes(LinkedPosition position) { | |
113 return includes(position.getDocument(), position.getOffset(), position.getLength()); | |
114 } | |
115 | |
116 /** | |
117 * Overrides {@link Position#includes(int)}so every offset is considered | |
118 * included that lies in between the first and last offset of this position, | |
119 * and offsets that are right at the end of the position. | |
120 * | |
121 * @param pOffset the offset to check | |
122 * @return <code>true</code> if <code>pOffset</code> is in | |
123 * <code>[offset, offset + length]</code> | |
124 */ | |
125 public bool includes(int pOffset) { | |
126 return this.offset <= pOffset && pOffset <= this.offset + this.length; | |
127 } | |
128 | |
129 /** | |
130 * Returns whether this position includes the range given by | |
131 * <code>offset</code> and <code>length</code>. A range is included by | |
132 * a <code>LinkedPosition</code> if {@link #includes(int) includes(offset)} | |
133 * returns true for every offset in the range, including the borders of the | |
134 * range. | |
135 * | |
136 * @param doc the document that the given range refers to, may be <code>null</code> | |
137 * @param off the offset of the range, referring to <code>document</code> | |
138 * @param len the length of the range | |
139 * @return <code>true</code> if <code>doc</code> is the same document as | |
140 * this position refers to, and if the entire range is included in | |
141 * this position | |
142 */ | |
143 protected bool includes(IDocument doc, int off, int len) { | |
144 return doc is fDocument && off >= offset && len + off <= offset + length; | |
145 | |
146 } | |
147 | |
148 /** | |
149 * Returns the content of this position on the referenced document. | |
150 * | |
151 * @return the content of the document at this position | |
152 * @throws BadLocationException if the position is not valid | |
153 */ | |
154 public String getContent() throws BadLocationException { | |
155 return fDocument.get(offset, length); | |
156 } | |
157 | |
158 /** | |
159 * Returns the sequence number of this position. | |
160 * | |
161 * @return the sequence number of this position | |
162 */ | |
163 public int getSequenceNumber() { | |
164 return fSequenceNumber; | |
165 } | |
166 | |
167 /** | |
168 * Sets the sequence number of this position. | |
169 * | |
170 * @param sequence the new sequence number | |
171 */ | |
172 public void setSequenceNumber(int sequence) { | |
173 fSequenceNumber= sequence; | |
174 } | |
175 | |
176 /* | |
177 * @see dwtx.jface.text.Position#hashCode() | |
178 */ | |
179 public int hashCode() { | |
180 return fDocument.hashCode() | super.hashCode() | fSequenceNumber; | |
181 } | |
182 } |