]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/richtext/styledtext/TabStop.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / richtext / styledtext / TabStop.java
1 /*\r
2  * (C) Copyright IBM Corp. 1998-2004.  All Rights Reserved.\r
3  *\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
12  */\r
13 package com.ibm.richtext.styledtext;\r
14 \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
19 \r
20 /**\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
25  * <ul>\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
29  *     position</li>\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
37  * </ul>\r
38  * @see MTabRuler\r
39  */\r
40 public final class TabStop implements Externalizable\r
41 {\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
46 \r
47     private byte fType;    // left, center, right, decimal\r
48     private int fPosition; // tab stop position from line origin.\r
49 \r
50     /**\r
51      * A TabStop with this type aligns its segment's leading edge\r
52      * to the TabStop's position.\r
53      */\r
54     public static final byte kLeading = 0;\r
55 \r
56     /**\r
57      * A TabStop with this type aligns its segment's center\r
58      * to the TabStop's position.\r
59      */\r
60     public static final byte kCenter = 1;\r
61 \r
62     /**\r
63      * A TabStop with this type aligns its segment's trailing edge\r
64      * to the TabStop's position.\r
65      */\r
66     public static final byte kTrailing = 2;\r
67 \r
68     /**\r
69      * A TabStop with this type aligns its segment's first decimal\r
70      * to the TabStop's position.\r
71      */\r
72     public static final byte kDecimal = 3;\r
73 \r
74     /**\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
79      */\r
80     public static final byte kAuto = 4;\r
81 \r
82     /**\r
83      * Create a TabStop with position 0 and type <code>kLeading</code>.\r
84      */\r
85     public TabStop() {\r
86 \r
87        this(0, kLeading);\r
88     }\r
89 \r
90     /**\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
94      *      in this class.\r
95      */\r
96     public TabStop(int position, byte type) {\r
97 \r
98         if (type < kLeading || type > kAuto) {\r
99             throw new IllegalArgumentException("Invalid tab type");\r
100         }\r
101 \r
102         fPosition = position;\r
103         fType = type;\r
104     }\r
105 \r
106     public void readExternal(ObjectInput in) throws IOException,\r
107                                             ClassNotFoundException {\r
108 \r
109         int version = in.readInt();\r
110         if (version != CURRENT_VERSION) {\r
111             throw new IOException("Invalid version of TabStop.");\r
112         }\r
113         fPosition = in.readInt();\r
114         fType = in.readByte();\r
115     }\r
116 \r
117     public void writeExternal(ObjectOutput out) throws IOException {\r
118 \r
119         out.writeInt(CURRENT_VERSION);\r
120         out.writeInt(fPosition);\r
121         out.writeByte(fType);\r
122     }\r
123 \r
124     /**\r
125      * Compare this to another Object.  TabStops are equal if\r
126      * their position and type are the same.\r
127      */\r
128     public boolean equals(Object rhs)\r
129     {\r
130         if (rhs == null) {\r
131             return false;\r
132         }\r
133 \r
134         TabStop rhsTab;\r
135         try {\r
136             rhsTab = (TabStop) rhs;\r
137         }\r
138         catch(ClassCastException e) {\r
139             return false;\r
140         }\r
141 \r
142         return fType == rhsTab.fType && fPosition == rhsTab.fPosition;\r
143     }\r
144 \r
145     /**\r
146      * Return the hash code for this TabStop.  The hash code is\r
147      * <code>position << type</code>.\r
148      */\r
149     public int hashCode() {\r
150 \r
151         return fPosition << fType;\r
152     }\r
153 \r
154     public String toString()\r
155     {\r
156         char typeChar;\r
157         switch (fType) {\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
164         }\r
165         return "TabStop[" + Integer.toString(fPosition) + typeChar + ']';\r
166     }\r
167 \r
168     /**\r
169      * Return the type of this TabStop.  Will be one of the constants\r
170      * in this class.\r
171      */\r
172     public byte getType() {\r
173         return fType;\r
174     }\r
175 \r
176     /**\r
177      * Return the position of this TabStop.\r
178      */\r
179     public int getPosition() {\r
180         return fPosition;\r
181     }\r
182 }\r
183 \r