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: package com.ibm.richtext.styledtext;
014:
015: import java.io.Externalizable;
016: import java.io.ObjectInput;
017: import java.io.ObjectOutput;
018: import java.io.IOException;
019:
020: /**
021: * TabStop represents a position on a tab ruler. Each tab stop has a
022: * position, giving its location on the ruler, and one of several
023: * types. The type determines how a segment controled by this TabStop
024: * is positioned on a line:
025: * <ul>
026: * <li><code>kLeading</code> - the leading edge of the segment is aligned to
027: * the TabStop's position</li>
028: * <li><code>kCenter</code> - the segment is centered on this TabStop's
029: * position</li>
030: * <li><code>kTrailing</code> - the trailing edge of the segment is aligned to
031: * the TabStop's position</li>
032: * <li><code>kDecimal</code> - the first decimal in the segment is aligned to
033: * the TabStop's position</li>
034: * <li><code>kAuto</code> - semantically the same as <code>kLeading</code>.
035: * Used by tab rulers to indicate that all subsequent tab stops
036: * will be at autospaced intervals.</li>
037: * </ul>
038: * @see MTabRuler
039: */
040: public final class TabStop implements Externalizable {
041: static final String COPYRIGHT = "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
042: private static final int CURRENT_VERSION = 1;
043: private static final long serialVersionUID = 22356934;
044:
045: private byte fType; // left, center, right, decimal
046: private int fPosition; // tab stop position from line origin.
047:
048: /**
049: * A TabStop with this type aligns its segment's leading edge
050: * to the TabStop's position.
051: */
052: public static final byte kLeading = 0;
053:
054: /**
055: * A TabStop with this type aligns its segment's center
056: * to the TabStop's position.
057: */
058: public static final byte kCenter = 1;
059:
060: /**
061: * A TabStop with this type aligns its segment's trailing edge
062: * to the TabStop's position.
063: */
064: public static final byte kTrailing = 2;
065:
066: /**
067: * A TabStop with this type aligns its segment's first decimal
068: * to the TabStop's position.
069: */
070: public static final byte kDecimal = 3;
071:
072: /**
073: * A TabStop with this type aligns its segment's leading edge
074: * to the TabStop's position. After a TabStop of this type,
075: * all tabs are at autospace intervals. Usually, clients will
076: * not construct TabStops with this type.
077: */
078: public static final byte kAuto = 4;
079:
080: /**
081: * Create a TabStop with position 0 and type <code>kLeading</code>.
082: */
083: public TabStop() {
084:
085: this (0, kLeading);
086: }
087:
088: /**
089: * Create a TabStop with the given position and type.
090: * @param position the TabStop's position
091: * @param type the TabStop's type. Must be one of constants
092: * in this class.
093: */
094: public TabStop(int position, byte type) {
095:
096: if (type < kLeading || type > kAuto) {
097: throw new IllegalArgumentException("Invalid tab type");
098: }
099:
100: fPosition = position;
101: fType = type;
102: }
103:
104: public void readExternal(ObjectInput in) throws IOException,
105: ClassNotFoundException {
106:
107: int version = in.readInt();
108: if (version != CURRENT_VERSION) {
109: throw new IOException("Invalid version of TabStop.");
110: }
111: fPosition = in.readInt();
112: fType = in.readByte();
113: }
114:
115: public void writeExternal(ObjectOutput out) throws IOException {
116:
117: out.writeInt(CURRENT_VERSION);
118: out.writeInt(fPosition);
119: out.writeByte(fType);
120: }
121:
122: /**
123: * Compare this to another Object. TabStops are equal if
124: * their position and type are the same.
125: */
126: public boolean equals(Object rhs) {
127: if (rhs == null) {
128: return false;
129: }
130:
131: TabStop rhsTab;
132: try {
133: rhsTab = (TabStop) rhs;
134: } catch (ClassCastException e) {
135: return false;
136: }
137:
138: return fType == rhsTab.fType && fPosition == rhsTab.fPosition;
139: }
140:
141: /**
142: * Return the hash code for this TabStop. The hash code is
143: * <code>position << type</code>.
144: */
145: public int hashCode() {
146:
147: return fPosition << fType;
148: }
149:
150: public String toString() {
151: char typeChar;
152: switch (fType) {
153: case kLeading:
154: typeChar = 'L';
155: break;
156: case kCenter:
157: typeChar = 'C';
158: break;
159: case kTrailing:
160: typeChar = 'R';
161: break;
162: case kDecimal:
163: typeChar = 'D';
164: break;
165: case kAuto:
166: typeChar = 'A';
167: break;
168: default:
169: typeChar = '?';
170: break;
171: }
172: return "TabStop[" + Integer.toString(fPosition) + typeChar
173: + ']';
174: }
175:
176: /**
177: * Return the type of this TabStop. Will be one of the constants
178: * in this class.
179: */
180: public byte getType() {
181: return fType;
182: }
183:
184: /**
185: * Return the position of this TabStop.
186: */
187: public int getPosition() {
188: return fPosition;
189: }
190: }
|