001: /*
002: * (C) Copyright IBM Corp. 1998-2004. All Rights Reserved.
003: *
004: * The program is provided "as is" without any warranty express or
005: * implied, including the warranty of non-infringement and the implied
006: * warranties of merchantibility and fitness for a particular purpose.
007: * IBM will not be liable for any damages suffered by you as a result
008: * of using the Program. In no event will IBM be liable for any
009: * special, indirect or consequential damages or lost profits even if
010: * IBM has been advised of the possibility of their occurrence. IBM
011: * will not be liable for any third party claims against you.
012: */
013: // Revision: 03 1.16 richtext/TextOffset.java, richtext, richtext
014: /*
015: 9/5/96 {jbr} added set and equals methods
016: */
017:
018: package com.ibm.richtext.textformat;
019:
020: /**
021: * A TextOffset indicates both an integer offset into text and a placement
022: * on one of the characters adjacent to the offset. An offset is a
023: * position between two characters; offset n
024: * is between character n-1 and character n. The placement specifies whether
025: * it is associated with the character
026: * after the offset
027: * (character n) or the character before the offset (character n-1).
028: * <p>
029: * Knowing which character the TextOffset is associated with is necessary
030: * when displaying carets. In bidirectional text, a single offset may
031: * have two distinct carets. Also, in multiline text, an offset at a line
032: * break has a possible caret on each line.
033: * <p>
034: * Most clients will not be interested in the placement, and will just use
035: * the offset.
036: */
037: public final class TextOffset {
038: static final String COPYRIGHT = "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
039: /**
040: * Indicates that the TextOffset is associated with character
041: * <code>fOffset - 1</code> - ie the character before its offset.
042: */
043: public final static boolean BEFORE_OFFSET = true;
044:
045: /**
046: * Indicates that the TextOffset is associated with character
047: * <code>fOffset</code> - ie the character after its offset.
048: */
049: public final static boolean AFTER_OFFSET = false;
050:
051: /**
052: * The offset into the text.
053: */
054: public int fOffset = 0;
055:
056: /**
057: * The placement - before or after.
058: */
059: public boolean fPlacement = AFTER_OFFSET;
060:
061: /**
062: * Constructs a new TextOffset
063: * @param offset the offset into the text to represent. Placement is implicitly AFTER_OFFSET.
064: */
065: public TextOffset(int offset) {
066: if (offset < 0)
067: throw new IllegalArgumentException(
068: "Offset is negative in TextOffset constructor.");
069:
070: fOffset = offset;
071:
072: fPlacement = AFTER_OFFSET;
073: }
074:
075: /**
076: * Constructs a new TextOffset at 0, with placement AFTER_OFFSET.
077: */
078: public TextOffset() {
079: this (0);
080: }
081:
082: /**
083: * Constructs a new TextOffset with the given offset and placement.
084: * @param offset the offset into the text
085: * @param placement indicates the position of the caret; one of BEFORE_OFFSET or AFTER_OFFSET
086: */
087: public TextOffset(int offset, boolean placement) {
088: if (offset < 0)
089: throw new IllegalArgumentException(
090: "TextOffset constructor offset < 0: " + offset);
091:
092: fOffset = offset;
093: fPlacement = placement;
094: }
095:
096: /**
097: * Constructs a new TextOffset from an existing one.
098: * @param rhs the TextOffset to copy
099: */
100: public TextOffset(TextOffset rhs) {
101:
102: this (rhs.fOffset, rhs.fPlacement);
103: }
104:
105: /**
106: * Set the value of the TextOffset
107: * @param offset the offset into the text
108: * @param placement indicates the position of the caret; one of BEFORE_OFFSET or AFTER_OFFSET
109: */
110: public void setOffset(int offset, boolean placement) {
111: if (offset < 0)
112: throw new IllegalArgumentException(
113: "TextOffset setOffset offset < 0: " + offset);
114:
115: fOffset = offset;
116: fPlacement = placement;
117: }
118:
119: /**
120: * Compare this to another Object.
121: */
122: public boolean equals(Object other) {
123:
124: try {
125: return equals((TextOffset) other);
126: } catch (ClassCastException e) {
127: return false;
128: }
129: }
130:
131: /**
132: * Return true if offset and placement are the same.
133: *
134: * @param other offset to compare against
135: * @return true if both offsets are equal
136: */
137: public boolean equals(TextOffset other) {
138:
139: return fOffset == other.fOffset
140: && fPlacement == other.fPlacement;
141: }
142:
143: /**
144: * Return the hashCode for this object.
145: */
146: public int hashCode() {
147:
148: return fPlacement == AFTER_OFFSET ? fOffset : -fOffset;
149: }
150:
151: /**
152: * Return true if this offset is 'greaterThan' other. If the fOffset fields are equal, the
153: * placement field is considered, and AFTER_OFFSET is considered 'greaterThan' BEFORE_OFFSET.
154: *
155: * @param other the other offset
156: * @return true if this offset appears after other
157: */
158: public boolean greaterThan(TextOffset other) {
159: return fOffset > other.fOffset
160: || (fOffset == other.fOffset
161: && fPlacement == AFTER_OFFSET && other.fPlacement == BEFORE_OFFSET);
162: }
163:
164: /**
165: * Return true if this offset is 'lessThan' other. If the fOffset fields are equal, the
166: * placement field is considered, and BEFORE_OFFSET is considered 'lessThan' AFTER_OFFSET.
167: *
168: * @param other the other offset
169: * @return true if this offset appears before other
170: */
171: public boolean lessThan(TextOffset other) {
172:
173: return fOffset < other.fOffset
174: || (fOffset == other.fOffset
175: && fPlacement == BEFORE_OFFSET && other.fPlacement == AFTER_OFFSET);
176: }
177:
178: /**
179: * Copy the value of another TextOffset into this
180: * @param other the TextOffset to copy
181: */
182: public void assign(TextOffset other) {
183: fOffset = other.fOffset;
184: fPlacement = other.fPlacement;
185: }
186:
187: /**
188: * Return a string representation of this object.
189: */
190: public String toString() {
191:
192: return "[" + (fPlacement ? "before " : "after ") + fOffset
193: + "]";
194: }
195: }
|