2 * (C) Copyright IBM Corp. 1998-2004. All Rights Reserved.
\r
4 * The program is provided "as is" without any warranty express or
\r
5 * implied, including the warranty of non-infringement and the implied
\r
6 * warranties of merchantibility and fitness for a particular purpose.
\r
7 * IBM will not be liable for any damages suffered by you as a result
\r
8 * of using the Program. In no event will IBM be liable for any
\r
9 * special, indirect or consequential damages or lost profits even if
\r
10 * IBM has been advised of the possibility of their occurrence. IBM
\r
11 * will not be liable for any third party claims against you.
\r
13 // Revision: 03 1.16 richtext/TextOffset.java, richtext, richtext
\r
16 9/5/96 {jbr} added set and equals methods
\r
19 package com.ibm.richtext.textformat;
\r
22 * A TextOffset indicates both an integer offset into text and a placement
\r
23 * on one of the characters adjacent to the offset. An offset is a
\r
24 * position between two characters; offset n
\r
25 * is between character n-1 and character n. The placement specifies whether
\r
26 * it is associated with the character
\r
28 * (character n) or the character before the offset (character n-1).
\r
30 * Knowing which character the TextOffset is associated with is necessary
\r
31 * when displaying carets. In bidirectional text, a single offset may
\r
32 * have two distinct carets. Also, in multiline text, an offset at a line
\r
33 * break has a possible caret on each line.
\r
35 * Most clients will not be interested in the placement, and will just use
\r
38 public final class TextOffset
\r
40 static final String COPYRIGHT =
\r
41 "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
\r
43 * Indicates that the TextOffset is associated with character
\r
44 * <code>fOffset - 1</code> - ie the character before its offset.
\r
46 public final static boolean BEFORE_OFFSET = true;
\r
49 * Indicates that the TextOffset is associated with character
\r
50 * <code>fOffset</code> - ie the character after its offset.
\r
52 public final static boolean AFTER_OFFSET = false;
\r
55 * The offset into the text.
\r
57 public int fOffset = 0;
\r
60 * The placement - before or after.
\r
62 public boolean fPlacement = AFTER_OFFSET;
\r
65 * Constructs a new TextOffset
\r
66 * @param offset the offset into the text to represent. Placement is implicitly AFTER_OFFSET.
\r
68 public TextOffset(int offset)
\r
71 throw new IllegalArgumentException("Offset is negative in TextOffset constructor.");
\r
75 fPlacement = AFTER_OFFSET;
\r
79 * Constructs a new TextOffset at 0, with placement AFTER_OFFSET.
\r
81 public TextOffset() {
\r
86 * Constructs a new TextOffset with the given offset and placement.
\r
87 * @param offset the offset into the text
\r
88 * @param placement indicates the position of the caret; one of BEFORE_OFFSET or AFTER_OFFSET
\r
90 public TextOffset(int offset, boolean placement)
\r
93 throw new IllegalArgumentException("TextOffset constructor offset < 0: " + offset);
\r
96 fPlacement = placement;
\r
100 * Constructs a new TextOffset from an existing one.
\r
101 * @param rhs the TextOffset to copy
\r
103 public TextOffset(TextOffset rhs) {
\r
105 this(rhs.fOffset, rhs.fPlacement);
\r
109 * Set the value of the TextOffset
\r
110 * @param offset the offset into the text
\r
111 * @param placement indicates the position of the caret; one of BEFORE_OFFSET or AFTER_OFFSET
\r
113 public void setOffset(int offset, boolean placement)
\r
116 throw new IllegalArgumentException("TextOffset setOffset offset < 0: " + offset);
\r
119 fPlacement = placement;
\r
123 * Compare this to another Object.
\r
125 public boolean equals(Object other) {
\r
128 return equals((TextOffset)other);
\r
130 catch(ClassCastException e) {
\r
136 * Return true if offset and placement are the same.
\r
138 * @param other offset to compare against
\r
139 * @return true if both offsets are equal
\r
141 public boolean equals(TextOffset other) {
\r
143 return fOffset == other.fOffset && fPlacement == other.fPlacement;
\r
147 * Return the hashCode for this object.
\r
149 public int hashCode() {
\r
151 return fPlacement==AFTER_OFFSET? fOffset : -fOffset;
\r
155 * Return true if this offset is 'greaterThan' other. If the fOffset fields are equal, the
\r
156 * placement field is considered, and AFTER_OFFSET is considered 'greaterThan' BEFORE_OFFSET.
\r
158 * @param other the other offset
\r
159 * @return true if this offset appears after other
\r
161 public boolean greaterThan(TextOffset other)
\r
163 return fOffset > other.fOffset ||
\r
164 (fOffset == other.fOffset && fPlacement == AFTER_OFFSET && other.fPlacement == BEFORE_OFFSET);
\r
168 * Return true if this offset is 'lessThan' other. If the fOffset fields are equal, the
\r
169 * placement field is considered, and BEFORE_OFFSET is considered 'lessThan' AFTER_OFFSET.
\r
171 * @param other the other offset
\r
172 * @return true if this offset appears before other
\r
174 public boolean lessThan(TextOffset other) {
\r
176 return fOffset < other.fOffset ||
\r
177 (fOffset == other.fOffset && fPlacement == BEFORE_OFFSET && other.fPlacement == AFTER_OFFSET);
\r
181 * Copy the value of another TextOffset into this
\r
182 * @param other the TextOffset to copy
\r
184 public void assign(TextOffset other) {
\r
185 fOffset = other.fOffset;
\r
186 fPlacement = other.fPlacement;
\r
190 * Return a string representation of this object.
\r
192 public String toString() {
\r
194 return "[" + (fPlacement ? "before " : "after ") + fOffset + "]";
\r