comparison dwtx/jface/text/Position.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, 2007 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.Position;
14
15 import dwt.dwthelper.utils;
16
17 import dwtx.core.runtime.Assert;
18
19
20 /**
21 * Positions describe text ranges of a document. Positions are adapted to
22 * changes applied to that document. The text range is specified by an offset
23 * and a length. Positions can be marked as deleted. Deleted positions are
24 * considered to no longer represent a valid text range in the managing
25 * document.
26 * <p>
27 * Positions attached to documents are usually updated by position updaters.
28 * Because position updaters are freely definable and because of the frequency
29 * in which they are used, the fields of a position are made publicly
30 * accessible. Clients other than position updaters are not allowed to access
31 * these public fields.
32 * </p>
33 * <p>
34 * Positions cannot be used as keys in hash tables as they override
35 * <code>equals</code> and <code>hashCode</code> as they would be value
36 * objects.
37 * </p>
38 *
39 * @see dwtx.jface.text.IDocument
40 */
41 public class Position {
42
43 /** The offset of the position */
44 public int offset;
45 /** The length of the position */
46 public int length;
47 /** Indicates whether the position has been deleted */
48 public bool isDeleted;
49
50 /**
51 * Creates a new position with the given offset and length 0.
52 *
53 * @param offset the position offset, must be >= 0
54 */
55 public Position(int offset) {
56 this(offset, 0);
57 }
58
59 /**
60 * Creates a new position with the given offset and length.
61 *
62 * @param offset the position offset, must be >= 0
63 * @param length the position length, must be >= 0
64 */
65 public Position(int offset, int length) {
66 Assert.isTrue(offset >= 0);
67 Assert.isTrue(length >= 0);
68 this.offset= offset;
69 this.length= length;
70 }
71
72 /**
73 * Creates a new, not initialized position.
74 */
75 protected Position() {
76 }
77
78 /*
79 * @see java.lang.Object#hashCode()
80 */
81 public int hashCode() {
82 int deleted= isDeleted ? 0 : 1;
83 return (offset << 24) | (length << 16) | deleted;
84 }
85
86 /**
87 * Marks this position as deleted.
88 */
89 public void delete() {
90 isDeleted= true;
91 }
92
93 /**
94 * Marks this position as not deleted.
95 *
96 * @since 2.0
97 */
98 public void undelete() {
99 isDeleted= false;
100 }
101
102 /*
103 * @see java.lang.Object#equals(java.lang.Object)
104 */
105 public bool equals(Object other) {
106 if (other instanceof Position) {
107 Position rp= (Position) other;
108 return (rp.offset is offset) && (rp.length is length);
109 }
110 return super.equals(other);
111 }
112
113 /**
114 * Returns the length of this position.
115 *
116 * @return the length of this position
117 */
118 public int getLength() {
119 return length;
120 }
121
122 /**
123 * Returns the offset of this position.
124 *
125 * @return the offset of this position
126 */
127 public int getOffset() {
128 return offset;
129 }
130
131 /**
132 * Checks whether the given index is inside
133 * of this position's text range.
134 *
135 * @param index the index to check
136 * @return <code>true</code> if <code>index</code> is inside of this position
137 */
138 public bool includes(int index) {
139
140 if (isDeleted)
141 return false;
142
143 return (this.offset <= index) && (index < this.offset + length);
144 }
145
146 /**
147 * Checks whether the intersection of the given text range
148 * and the text range represented by this position is empty
149 * or not.
150 *
151 * @param rangeOffset the offset of the range to check
152 * @param rangeLength the length of the range to check
153 * @return <code>true</code> if intersection is not empty
154 */
155 public bool overlapsWith(int rangeOffset, int rangeLength) {
156
157 if (isDeleted)
158 return false;
159
160 int end= rangeOffset + rangeLength;
161 int thisEnd= this.offset + this.length;
162
163 if (rangeLength > 0) {
164 if (this.length > 0)
165 return this.offset < end && rangeOffset < thisEnd;
166 return rangeOffset <= this.offset && this.offset < end;
167 }
168
169 if (this.length > 0)
170 return this.offset <= rangeOffset && rangeOffset < thisEnd;
171 return this.offset is rangeOffset;
172 }
173
174 /**
175 * Returns whether this position has been deleted or not.
176 *
177 * @return <code>true</code> if position has been deleted
178 */
179 public bool isDeleted() {
180 return isDeleted;
181 }
182
183 /**
184 * Changes the length of this position to the given length.
185 *
186 * @param length the new length of this position
187 */
188 public void setLength(int length) {
189 Assert.isTrue(length >= 0);
190 this.length= length;
191 }
192
193 /**
194 * Changes the offset of this position to the given offset.
195 *
196 * @param offset the new offset of this position
197 */
198 public void setOffset(int offset) {
199 Assert.isTrue(offset >= 0);
200 this.offset= offset;
201 }
202 }