]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/richtext/styledtext/StyleModifier.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / richtext / styledtext / StyleModifier.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 com.ibm.richtext.textlayout.attributes.AttributeMap;\r
16 import com.ibm.richtext.textlayout.attributes.AttributeSet;\r
17 \r
18 /**\r
19  * StyleModifier is the base class for operations on AttributeMap.  To implement\r
20  * an operation on AttributeMap, subclass StyleModifier and override\r
21  * <code>modifyStyle</code>.  StyleModifiers are used by MText.\r
22  * <p>\r
23  * For convenience, this class contains factory methods which will create a\r
24  * StyleModifier for\r
25  * certain common operations: attribute union, attribute removal, and AttributeMap\r
26  * replacement.\r
27  * @see AttributeMap\r
28  * @see AttributeSet\r
29  * @see MText\r
30  */\r
31 /*\r
32  * {jbr} StyleModifier is not the best name for this class - styles are immutable and never\r
33  * really modified.\r
34  */\r
35 public class StyleModifier\r
36 {\r
37     static final String COPYRIGHT =\r
38                 "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";\r
39     /**\r
40      * Create a StyleModifier.\r
41      */\r
42     protected StyleModifier() {\r
43     }\r
44 \r
45     /**\r
46      * Return the result of this StyleModifier's operation on the given style.\r
47      * Default implementation just returns the given style.\r
48      * @param style the AttributeMap to perform the operation on\r
49      */\r
50     public AttributeMap modifyStyle(AttributeMap style)\r
51     {\r
52         return style;\r
53     }\r
54 \r
55     /**\r
56      * A StyleModifier which simply returns the given style.\r
57      */\r
58     public static final StyleModifier IDENTITY = new StyleModifier();\r
59 \r
60     /**\r
61      * Create a StyleModifier whose operation is\r
62      * <code>style.addAttributes(s)</code>,\r
63      * where <code>style</code> is the AttributeMap passed to\r
64      * <code>modifyStyle</code>.\r
65      * @param s the AttributeMap to union with\r
66      * @return a StyleModifier for this operation\r
67      */\r
68     public static StyleModifier createAddModifier(AttributeMap s) {\r
69 \r
70         return new StyleAddModifier(s);\r
71     }\r
72 \r
73     /**\r
74      * Create a StyleModifier whose operation is\r
75      * <code>style.addAttribute(key, value)</code>,\r
76      * where <code>style</code> is the AttributeMap passed to\r
77      * <code>modifyStyle</code>.\r
78      * @param key the key to add\r
79      * @param value the value to add\r
80      * @return a StyleModifier for this operation\r
81      */\r
82     public static StyleModifier createAddModifier(Object key,\r
83                                                   Object value) {\r
84 \r
85         return new AttributeAddModifier(key, value);\r
86     }\r
87 \r
88     /**\r
89      * Create a StyleModifier whose operation returns <code>s</code>,\r
90      * ignoring the parameter to <code>modifyStyle</code>.\r
91      * @param s the AttributeMap which will replace any other AttributeMap\r
92      * @return a StyleModifier for this operation\r
93      */\r
94     public static StyleModifier createReplaceModifier(AttributeMap s) {\r
95 \r
96         return new StyleReplaceModifier(s);\r
97     }\r
98 \r
99     /**\r
100      * Create a StyleModifier whose operation is\r
101      * <code>style.removeAttributes(s)</code>,\r
102      * where <code>style</code> is the AttributeMap passed to\r
103      * <code>modifyStyle</code>.\r
104      * @param s the AttributeSet of attributes to remove\r
105      * @return a StyleModifier for this operation\r
106      */\r
107     public static StyleModifier createRemoveModifier(AttributeSet s) {\r
108 \r
109         return new StyleRemoveModifier(s);\r
110     }\r
111 \r
112     static final class AttributeAddModifier extends StyleModifier {\r
113 \r
114         private Object fKey;\r
115         private Object fValue;\r
116 \r
117         public AttributeAddModifier(Object key, Object value) {\r
118 \r
119             fKey = key;\r
120             fValue = value;\r
121         }\r
122 \r
123         public AttributeMap modifyStyle(AttributeMap style) {\r
124 \r
125             return style.addAttribute(fKey, fValue);\r
126         }\r
127     }\r
128 \r
129     /**\r
130      * Create this with the styles to add.  These styles will add to and override any already\r
131      * present in the style passed to modifyStyle.\r
132      */\r
133     static final class StyleAddModifier extends StyleModifier\r
134     {\r
135         private AttributeMap fStyle;\r
136 \r
137         public StyleAddModifier(AttributeMap style)\r
138         {\r
139             if (style == null) {\r
140                 throw new IllegalArgumentException("style is null");\r
141             }\r
142             fStyle = style;\r
143         }\r
144 \r
145         public AttributeMap modifyStyle(AttributeMap style)\r
146         {\r
147             return style.addAttributes(fStyle);\r
148         }\r
149     }\r
150 \r
151     /**\r
152      * Create this with the styles to replace.  All style runs will have only these\r
153      * styles.\r
154      */\r
155     static final class StyleReplaceModifier extends StyleModifier\r
156     {\r
157         private AttributeMap fStyle;\r
158 \r
159         public StyleReplaceModifier(AttributeMap style)\r
160         {\r
161             if (style == null) {\r
162                 throw new IllegalArgumentException("style is null");\r
163             }\r
164             fStyle = style;\r
165         }\r
166 \r
167         public AttributeMap modifyStyle(AttributeMap style)\r
168         {\r
169             return fStyle;\r
170         }\r
171     }\r
172 \r
173     static final class StyleRemoveModifier extends StyleModifier {\r
174 \r
175         private AttributeSet fRemoveSet;\r
176 \r
177         public StyleRemoveModifier(AttributeSet removeSet) {\r
178 \r
179             if (removeSet == null) {\r
180                 throw new IllegalArgumentException("set is null");\r
181             }\r
182             fRemoveSet = removeSet;\r
183         }\r
184 \r
185         public AttributeMap modifyStyle(AttributeMap style) {\r
186 \r
187             return style.removeAttributes(fRemoveSet);\r
188         }\r
189     }\r
190 }\r