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 com.ibm.richtext.textlayout.attributes.AttributeMap;
\r
16 import com.ibm.richtext.textlayout.attributes.AttributeSet;
\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
23 * For convenience, this class contains factory methods which will create a
\r
25 * certain common operations: attribute union, attribute removal, and AttributeMap
\r
32 * {jbr} StyleModifier is not the best name for this class - styles are immutable and never
\r
35 public class StyleModifier
\r
37 static final String COPYRIGHT =
\r
38 "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
\r
40 * Create a StyleModifier.
\r
42 protected StyleModifier() {
\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
50 public AttributeMap modifyStyle(AttributeMap style)
\r
56 * A StyleModifier which simply returns the given style.
\r
58 public static final StyleModifier IDENTITY = new StyleModifier();
\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
68 public static StyleModifier createAddModifier(AttributeMap s) {
\r
70 return new StyleAddModifier(s);
\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
82 public static StyleModifier createAddModifier(Object key,
\r
85 return new AttributeAddModifier(key, value);
\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
94 public static StyleModifier createReplaceModifier(AttributeMap s) {
\r
96 return new StyleReplaceModifier(s);
\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
107 public static StyleModifier createRemoveModifier(AttributeSet s) {
\r
109 return new StyleRemoveModifier(s);
\r
112 static final class AttributeAddModifier extends StyleModifier {
\r
114 private Object fKey;
\r
115 private Object fValue;
\r
117 public AttributeAddModifier(Object key, Object value) {
\r
123 public AttributeMap modifyStyle(AttributeMap style) {
\r
125 return style.addAttribute(fKey, fValue);
\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
133 static final class StyleAddModifier extends StyleModifier
\r
135 private AttributeMap fStyle;
\r
137 public StyleAddModifier(AttributeMap style)
\r
139 if (style == null) {
\r
140 throw new IllegalArgumentException("style is null");
\r
145 public AttributeMap modifyStyle(AttributeMap style)
\r
147 return style.addAttributes(fStyle);
\r
152 * Create this with the styles to replace. All style runs will have only these
\r
155 static final class StyleReplaceModifier extends StyleModifier
\r
157 private AttributeMap fStyle;
\r
159 public StyleReplaceModifier(AttributeMap style)
\r
161 if (style == null) {
\r
162 throw new IllegalArgumentException("style is null");
\r
167 public AttributeMap modifyStyle(AttributeMap style)
\r
173 static final class StyleRemoveModifier extends StyleModifier {
\r
175 private AttributeSet fRemoveSet;
\r
177 public StyleRemoveModifier(AttributeSet removeSet) {
\r
179 if (removeSet == null) {
\r
180 throw new IllegalArgumentException("set is null");
\r
182 fRemoveSet = removeSet;
\r
185 public AttributeMap modifyStyle(AttributeMap style) {
\r
187 return style.removeAttributes(fRemoveSet);
\r