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 com.ibm.richtext.textlayout.attributes.AttributeMap;
016: import com.ibm.richtext.textlayout.attributes.AttributeSet;
017:
018: /**
019: * StyleModifier is the base class for operations on AttributeMap. To implement
020: * an operation on AttributeMap, subclass StyleModifier and override
021: * <code>modifyStyle</code>. StyleModifiers are used by MText.
022: * <p>
023: * For convenience, this class contains factory methods which will create a
024: * StyleModifier for
025: * certain common operations: attribute union, attribute removal, and AttributeMap
026: * replacement.
027: * @see AttributeMap
028: * @see AttributeSet
029: * @see MText
030: */
031: /*
032: * {jbr} StyleModifier is not the best name for this class - styles are immutable and never
033: * really modified.
034: */
035: public class StyleModifier {
036: static final String COPYRIGHT = "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
037:
038: /**
039: * Create a StyleModifier.
040: */
041: protected StyleModifier() {
042: }
043:
044: /**
045: * Return the result of this StyleModifier's operation on the given style.
046: * Default implementation just returns the given style.
047: * @param style the AttributeMap to perform the operation on
048: */
049: public AttributeMap modifyStyle(AttributeMap style) {
050: return style;
051: }
052:
053: /**
054: * A StyleModifier which simply returns the given style.
055: */
056: public static final StyleModifier IDENTITY = new StyleModifier();
057:
058: /**
059: * Create a StyleModifier whose operation is
060: * <code>style.addAttributes(s)</code>,
061: * where <code>style</code> is the AttributeMap passed to
062: * <code>modifyStyle</code>.
063: * @param s the AttributeMap to union with
064: * @return a StyleModifier for this operation
065: */
066: public static StyleModifier createAddModifier(AttributeMap s) {
067:
068: return new StyleAddModifier(s);
069: }
070:
071: /**
072: * Create a StyleModifier whose operation is
073: * <code>style.addAttribute(key, value)</code>,
074: * where <code>style</code> is the AttributeMap passed to
075: * <code>modifyStyle</code>.
076: * @param key the key to add
077: * @param value the value to add
078: * @return a StyleModifier for this operation
079: */
080: public static StyleModifier createAddModifier(Object key,
081: Object value) {
082:
083: return new AttributeAddModifier(key, value);
084: }
085:
086: /**
087: * Create a StyleModifier whose operation returns <code>s</code>,
088: * ignoring the parameter to <code>modifyStyle</code>.
089: * @param s the AttributeMap which will replace any other AttributeMap
090: * @return a StyleModifier for this operation
091: */
092: public static StyleModifier createReplaceModifier(AttributeMap s) {
093:
094: return new StyleReplaceModifier(s);
095: }
096:
097: /**
098: * Create a StyleModifier whose operation is
099: * <code>style.removeAttributes(s)</code>,
100: * where <code>style</code> is the AttributeMap passed to
101: * <code>modifyStyle</code>.
102: * @param s the AttributeSet of attributes to remove
103: * @return a StyleModifier for this operation
104: */
105: public static StyleModifier createRemoveModifier(AttributeSet s) {
106:
107: return new StyleRemoveModifier(s);
108: }
109:
110: static final class AttributeAddModifier extends StyleModifier {
111:
112: private Object fKey;
113: private Object fValue;
114:
115: public AttributeAddModifier(Object key, Object value) {
116:
117: fKey = key;
118: fValue = value;
119: }
120:
121: public AttributeMap modifyStyle(AttributeMap style) {
122:
123: return style.addAttribute(fKey, fValue);
124: }
125: }
126:
127: /**
128: * Create this with the styles to add. These styles will add to and override any already
129: * present in the style passed to modifyStyle.
130: */
131: static final class StyleAddModifier extends StyleModifier {
132: private AttributeMap fStyle;
133:
134: public StyleAddModifier(AttributeMap style) {
135: if (style == null) {
136: throw new IllegalArgumentException("style is null");
137: }
138: fStyle = style;
139: }
140:
141: public AttributeMap modifyStyle(AttributeMap style) {
142: return style.addAttributes(fStyle);
143: }
144: }
145:
146: /**
147: * Create this with the styles to replace. All style runs will have only these
148: * styles.
149: */
150: static final class StyleReplaceModifier extends StyleModifier {
151: private AttributeMap fStyle;
152:
153: public StyleReplaceModifier(AttributeMap style) {
154: if (style == null) {
155: throw new IllegalArgumentException("style is null");
156: }
157: fStyle = style;
158: }
159:
160: public AttributeMap modifyStyle(AttributeMap style) {
161: return fStyle;
162: }
163: }
164:
165: static final class StyleRemoveModifier extends StyleModifier {
166:
167: private AttributeSet fRemoveSet;
168:
169: public StyleRemoveModifier(AttributeSet removeSet) {
170:
171: if (removeSet == null) {
172: throw new IllegalArgumentException("set is null");
173: }
174: fRemoveSet = removeSet;
175: }
176:
177: public AttributeMap modifyStyle(AttributeMap style) {
178:
179: return style.removeAttributes(fRemoveSet);
180: }
181: }
182: }
|