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.textlayout.attributes;
\r
15 import java.io.Serializable;
\r
18 * This class provides a cannonical mapping between fields in TextAttribute
\r
19 * and instances of itself. It is used by AttributeMap to serialize
\r
20 * and deserialize TextAttribute to preserve uniqueness of TextAttribute
\r
21 * instances (ie so that TextAttribute instances remain singletons),
\r
22 * and to provide compatability between 1.1 and 1.2 versions of
\r
25 * Example use - instead of doing this:
\r
27 * out.writeObject(anAttribute);
\r
28 * </pre></blockquote>
\r
31 * out.writeObject(AttributeKey.mapAttributeToKey(anAttribute));
\r
32 * </pre></blockquote>
\r
33 * Similarly, instead of this:
\r
35 * anAttribute = in.readObject();
\r
36 * </pre></blockquote>
\r
39 * anAttribute = AttributeKey.mapKeyToAttribute(in.readObject());
\r
40 * </pre></blockquote>
\r
42 * If anAttribute is not a known TextAttribute, then <code>mapAttributeToKey</code>
\r
43 * will just return its argument. Similarly, <code>mapKeyToAttribute</code> will
\r
44 * return its argument if the argument is not a known AttributeKey.
\r
47 /*public*/ final class AttributeKey implements Serializable {
\r
50 In this implementation, two parallel Vectors are
\r
51 maintained. TextAttribute(i) maps to AttributeKey(i).
\r
52 For compatability with existing data, this mapping must
\r
53 be maintained in the future! So, when new attributes
\r
54 are added, add them to the end of the list.
\r
56 static final String COPYRIGHT =
\r
57 "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
\r
58 private static final long serialVersionUID = 3772371253277107294L;
\r
60 private static Object[] fgTextAttributes;
\r
61 private static Object[] fgAttributeKeys;
\r
64 fgTextAttributes = new Object[] {
\r
66 TextAttribute.FAMILY,
\r
67 TextAttribute.WEIGHT,
\r
68 TextAttribute.POSTURE,
\r
70 TextAttribute.SUPERSCRIPT,
\r
71 TextAttribute.FOREGROUND,
\r
72 TextAttribute.BACKGROUND,
\r
73 TextAttribute.UNDERLINE,
\r
74 TextAttribute.STRIKETHROUGH,
\r
75 TextAttribute.CHAR_REPLACEMENT,
\r
76 TextAttribute.EXTRA_LINE_SPACING,
\r
77 TextAttribute.FIRST_LINE_INDENT,
\r
78 TextAttribute.MIN_LINE_SPACING,
\r
79 TextAttribute.LINE_FLUSH,
\r
80 TextAttribute.LEADING_MARGIN,
\r
81 TextAttribute.TRAILING_MARGIN,
\r
82 TextAttribute.TAB_RULER,
\r
83 TextAttribute.RUN_DIRECTION,
\r
84 TextAttribute.BIDI_EMBEDDING,
\r
85 TextAttribute.JUSTIFICATION,
\r
88 final int attrCount = fgTextAttributes.length;
\r
89 fgAttributeKeys = new Object[attrCount];
\r
91 for (int i=0; i < attrCount; i += 1) {
\r
92 fgAttributeKeys[i] = new AttributeKey(i);
\r
97 * Return the TextAttribute corresponding to the given key.
\r
98 * If key is an instance of AttributeKey it will be mapped to
\r
99 * a TextAttribute. Otherwise, the key is returned.
\r
100 * @param key the key to map to a TextAttribute field
\r
101 * @return the TextAttribute for <code>key</code> if <code>key</code>
\r
102 * is an AttributeKey; otherwise <code>key</code> is returned
\r
104 /*public*/ static Object mapKeyToAttribute(Object key) {
\r
107 AttributeKey aKey = (AttributeKey) key;
\r
108 if (aKey.fId < fgTextAttributes.length) {
\r
109 return fgTextAttributes[aKey.fId];
\r
115 catch(ClassCastException e) {
\r
121 * If attribute is a known TextAttribute, return an AttributeKey
\r
122 * for it. Otherwise the object is returned.
\r
123 * @param attribute the attribute to map to an AttributeKey
\r
124 * @return an AttributeKey for <code>attribute</code>
\r
125 * if <code>attribute</code> is a known attribute; otherwise
\r
126 * <code>attribute</code> is returned
\r
128 /*public*/ static Object mapAttributeToKey(Object attribute) {
\r
130 final int attrCount = fgTextAttributes.length;
\r
132 for (int index = 0; index < attrCount; index += 1) {
\r
133 if (fgTextAttributes[index].equals(attribute)) {
\r
134 return fgAttributeKeys[index];
\r
144 private AttributeKey(int id) {
\r
149 public boolean equals(Object rhs) {
\r
152 return ((AttributeKey)rhs).fId == fId;
\r
154 catch(ClassCastException e) {
\r
159 public int hashCode() {
\r