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 package com.ibm.richtext.styledtext;
\r
15 import java.io.Externalizable;
\r
16 import java.io.ObjectInput;
\r
17 import java.io.ObjectOutput;
\r
18 import java.io.IOException;
\r
21 * TabStop represents a position on a tab ruler. Each tab stop has a
\r
22 * position, giving its location on the ruler, and one of several
\r
23 * types. The type determines how a segment controled by this TabStop
\r
24 * is positioned on a line:
\r
26 * <li><code>kLeading</code> - the leading edge of the segment is aligned to
\r
27 * the TabStop's position</li>
\r
28 * <li><code>kCenter</code> - the segment is centered on this TabStop's
\r
30 * <li><code>kTrailing</code> - the trailing edge of the segment is aligned to
\r
31 * the TabStop's position</li>
\r
32 * <li><code>kDecimal</code> - the first decimal in the segment is aligned to
\r
33 * the TabStop's position</li>
\r
34 * <li><code>kAuto</code> - semantically the same as <code>kLeading</code>.
\r
35 * Used by tab rulers to indicate that all subsequent tab stops
\r
36 * will be at autospaced intervals.</li>
\r
40 public final class TabStop implements Externalizable
\r
42 static final String COPYRIGHT =
\r
43 "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
\r
44 private static final int CURRENT_VERSION = 1;
\r
45 private static final long serialVersionUID = 22356934;
\r
47 private byte fType; // left, center, right, decimal
\r
48 private int fPosition; // tab stop position from line origin.
\r
51 * A TabStop with this type aligns its segment's leading edge
\r
52 * to the TabStop's position.
\r
54 public static final byte kLeading = 0;
\r
57 * A TabStop with this type aligns its segment's center
\r
58 * to the TabStop's position.
\r
60 public static final byte kCenter = 1;
\r
63 * A TabStop with this type aligns its segment's trailing edge
\r
64 * to the TabStop's position.
\r
66 public static final byte kTrailing = 2;
\r
69 * A TabStop with this type aligns its segment's first decimal
\r
70 * to the TabStop's position.
\r
72 public static final byte kDecimal = 3;
\r
75 * A TabStop with this type aligns its segment's leading edge
\r
76 * to the TabStop's position. After a TabStop of this type,
\r
77 * all tabs are at autospace intervals. Usually, clients will
\r
78 * not construct TabStops with this type.
\r
80 public static final byte kAuto = 4;
\r
83 * Create a TabStop with position 0 and type <code>kLeading</code>.
\r
91 * Create a TabStop with the given position and type.
\r
92 * @param position the TabStop's position
\r
93 * @param type the TabStop's type. Must be one of constants
\r
96 public TabStop(int position, byte type) {
\r
98 if (type < kLeading || type > kAuto) {
\r
99 throw new IllegalArgumentException("Invalid tab type");
\r
102 fPosition = position;
\r
106 public void readExternal(ObjectInput in) throws IOException,
\r
107 ClassNotFoundException {
\r
109 int version = in.readInt();
\r
110 if (version != CURRENT_VERSION) {
\r
111 throw new IOException("Invalid version of TabStop.");
\r
113 fPosition = in.readInt();
\r
114 fType = in.readByte();
\r
117 public void writeExternal(ObjectOutput out) throws IOException {
\r
119 out.writeInt(CURRENT_VERSION);
\r
120 out.writeInt(fPosition);
\r
121 out.writeByte(fType);
\r
125 * Compare this to another Object. TabStops are equal if
\r
126 * their position and type are the same.
\r
128 public boolean equals(Object rhs)
\r
136 rhsTab = (TabStop) rhs;
\r
138 catch(ClassCastException e) {
\r
142 return fType == rhsTab.fType && fPosition == rhsTab.fPosition;
\r
146 * Return the hash code for this TabStop. The hash code is
\r
147 * <code>position << type</code>.
\r
149 public int hashCode() {
\r
151 return fPosition << fType;
\r
154 public String toString()
\r
158 case kLeading: typeChar = 'L'; break;
\r
159 case kCenter: typeChar = 'C'; break;
\r
160 case kTrailing: typeChar = 'R'; break;
\r
161 case kDecimal: typeChar = 'D'; break;
\r
162 case kAuto: typeChar = 'A'; break;
\r
163 default: typeChar = '?'; break;
\r
165 return "TabStop[" + Integer.toString(fPosition) + typeChar + ']';
\r
169 * Return the type of this TabStop. Will be one of the constants
\r
172 public byte getType() {
\r
177 * Return the position of this TabStop.
\r
179 public int getPosition() {
\r