KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > text > html > MuxingAttributeSet


1 /*
2  * @(#)MuxingAttributeSet.java 1.4 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing.text.html;
8
9 import javax.swing.text.*;
10 import java.io.Serializable JavaDoc;
11 import java.util.*;
12
13 /**
14  * An implementation of <code>AttributeSet</code> that can multiplex
15  * across a set of <code>AttributeSet</code>s.
16  *
17  * @version 1.4 12/19/03
18  */

19 class MuxingAttributeSet implements AttributeSet, Serializable JavaDoc {
20     /**
21      * Creates a <code>MuxingAttributeSet</code> with the passed in
22      * attributes.
23      */

24     public MuxingAttributeSet(AttributeSet[] attrs) {
25         this.attrs = attrs;
26     }
27
28     /**
29      * Creates an empty <code>MuxingAttributeSet</code>. This is intended for
30      * use by subclasses only, and it is also intended that subclasses will
31      * set the constituent <code>AttributeSet</code>s before invoking any
32      * of the <code>AttributeSet</code> methods.
33      */

34     protected MuxingAttributeSet() {
35     }
36
37     /**
38      * Directly sets the <code>AttributeSet</code>s that comprise this
39      * <code>MuxingAttributeSet</code>.
40      */

41     protected synchronized void setAttributes(AttributeSet[] attrs) {
42         this.attrs = attrs;
43     }
44
45     /**
46      * Returns the <code>AttributeSet</code>s multiplexing too. When the
47      * <code>AttributeSet</code>s need to be referenced, this should be called.
48      */

49     protected synchronized AttributeSet[] getAttributes() {
50         return attrs;
51     }
52
53     /**
54      * Inserts <code>as</code> at <code>index</code>. This assumes
55      * the value of <code>index</code> is between 0 and attrs.length,
56      * inclusive.
57      */

58     protected synchronized void insertAttributeSetAt(AttributeSet as,
59                                                      int index) {
60         int numAttrs = attrs.length;
61         AttributeSet newAttrs[] = new AttributeSet[numAttrs + 1];
62         if (index < numAttrs) {
63             if (index > 0) {
64                 System.arraycopy(attrs, 0, newAttrs, 0, index);
65                 System.arraycopy(attrs, index, newAttrs, index + 1,
66                                  numAttrs - index);
67             }
68             else {
69                 System.arraycopy(attrs, 0, newAttrs, 1, numAttrs);
70             }
71         }
72         else {
73             System.arraycopy(attrs, 0, newAttrs, 0, numAttrs);
74         }
75         newAttrs[index] = as;
76         attrs = newAttrs;
77     }
78
79     /**
80      * Removes the AttributeSet at <code>index</code>. This assumes
81      * the value of <code>index</code> is greater than or equal to 0,
82      * and less than attrs.length.
83      */

84     protected synchronized void removeAttributeSetAt(int index) {
85         int numAttrs = attrs.length;
86         AttributeSet[] newAttrs = new AttributeSet[numAttrs - 1];
87         if (numAttrs > 0) {
88             if (index == 0) {
89                 // FIRST
90
System.arraycopy(attrs, 1, newAttrs, 0, numAttrs - 1);
91             }
92             else if (index < (numAttrs - 1)) {
93                 // MIDDLE
94
System.arraycopy(attrs, 0, newAttrs, 0, index);
95                 System.arraycopy(attrs, index + 1, newAttrs, index,
96                                  numAttrs - index - 1);
97             }
98             else {
99                 // END
100
System.arraycopy(attrs, 0, newAttrs, 0, numAttrs - 1);
101             }
102         }
103         attrs = newAttrs;
104     }
105
106     // --- AttributeSet methods ----------------------------
107

108     /**
109      * Gets the number of attributes that are defined.
110      *
111      * @return the number of attributes
112      * @see AttributeSet#getAttributeCount
113      */

114     public int getAttributeCount() {
115         AttributeSet[] as = getAttributes();
116         int n = 0;
117         for (int i = 0; i < as.length; i++) {
118             n += as[i].getAttributeCount();
119         }
120         return n;
121     }
122
123     /**
124      * Checks whether a given attribute is defined.
125      * This will convert the key over to CSS if the
126      * key is a StyleConstants key that has a CSS
127      * mapping.
128      *
129      * @param key the attribute key
130      * @return true if the attribute is defined
131      * @see AttributeSet#isDefined
132      */

133     public boolean isDefined(Object JavaDoc key) {
134         AttributeSet[] as = getAttributes();
135         for (int i = 0; i < as.length; i++) {
136             if (as[i].isDefined(key)) {
137                 return true;
138             }
139         }
140         return false;
141     }
142
143     /**
144      * Checks whether two attribute sets are equal.
145      *
146      * @param attr the attribute set to check against
147      * @return true if the same
148      * @see AttributeSet#isEqual
149      */

150     public boolean isEqual(AttributeSet attr) {
151         return ((getAttributeCount() == attr.getAttributeCount()) &&
152                 containsAttributes(attr));
153     }
154
155     /**
156      * Copies a set of attributes.
157      *
158      * @return the copy
159      * @see AttributeSet#copyAttributes
160      */

161     public AttributeSet copyAttributes() {
162         AttributeSet[] as = getAttributes();
163         MutableAttributeSet a = new SimpleAttributeSet();
164         int n = 0;
165         for (int i = as.length - 1; i >= 0; i--) {
166             a.addAttributes(as[i]);
167         }
168         return a;
169     }
170
171     /**
172      * Gets the value of an attribute. If the requested
173      * attribute is a StyleConstants attribute that has
174      * a CSS mapping, the request will be converted.
175      *
176      * @param key the attribute name
177      * @return the attribute value
178      * @see AttributeSet#getAttribute
179      */

180     public Object JavaDoc getAttribute(Object JavaDoc key) {
181         AttributeSet[] as = getAttributes();
182         int n = as.length;
183         for (int i = 0; i < n; i++) {
184             Object JavaDoc o = as[i].getAttribute(key);
185             if (o != null) {
186                 return o;
187             }
188         }
189         return null;
190     }
191
192     /**
193      * Gets the names of all attributes.
194      *
195      * @return the attribute names
196      * @see AttributeSet#getAttributeNames
197      */

198     public Enumeration getAttributeNames() {
199         return new MuxingAttributeNameEnumeration();
200     }
201
202     /**
203      * Checks whether a given attribute name/value is defined.
204      *
205      * @param name the attribute name
206      * @param value the attribute value
207      * @return true if the name/value is defined
208      * @see AttributeSet#containsAttribute
209      */

210     public boolean containsAttribute(Object JavaDoc name, Object JavaDoc value) {
211         return value.equals(getAttribute(name));
212     }
213
214     /**
215      * Checks whether the attribute set contains all of
216      * the given attributes.
217      *
218      * @param attrs the attributes to check
219      * @return true if the element contains all the attributes
220      * @see AttributeSet#containsAttributes
221      */

222     public boolean containsAttributes(AttributeSet attrs) {
223         boolean result = true;
224
225         Enumeration names = attrs.getAttributeNames();
226         while (result && names.hasMoreElements()) {
227             Object JavaDoc name = names.nextElement();
228             result = attrs.getAttribute(name).equals(getAttribute(name));
229         }
230
231         return result;
232     }
233
234     /**
235      * Returns null, subclasses may wish to do something more
236      * intelligent with this.
237      */

238     public AttributeSet getResolveParent() {
239         return null;
240     }
241
242     /**
243      * The <code>AttributeSet</code>s that make up the resulting
244      * <code>AttributeSet</code>.
245      */

246     private AttributeSet[] attrs;
247
248
249     /**
250      * An Enumeration of the Attribute names in a MuxingAttributeSet.
251      * This may return the same name more than once.
252      */

253     private class MuxingAttributeNameEnumeration implements Enumeration {
254
255         MuxingAttributeNameEnumeration() {
256             updateEnum();
257         }
258
259         public boolean hasMoreElements() {
260             if (currentEnum == null) {
261                 return false;
262             }
263             return currentEnum.hasMoreElements();
264         }
265
266         public Object JavaDoc nextElement() {
267             if (currentEnum == null) {
268                 throw new NoSuchElementException("No more names");
269             }
270             Object JavaDoc retObject = currentEnum.nextElement();
271             if (!currentEnum.hasMoreElements()) {
272                 updateEnum();
273             }
274             return retObject;
275         }
276
277         void updateEnum() {
278             AttributeSet[] as = getAttributes();
279             currentEnum = null;
280             while (currentEnum == null && attrIndex < as.length) {
281                 currentEnum = as[attrIndex++].getAttributeNames();
282                 if (!currentEnum.hasMoreElements()) {
283                     currentEnum = null;
284                 }
285             }
286         }
287
288
289         /** Index into attrs the current Enumeration came from. */
290         private int attrIndex;
291         /** Enumeration from attrs. */
292         private Enumeration currentEnum;
293     }
294 }
295
Popular Tags