KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > text > SimpleAttributeSet


1 /*
2  * @(#)SimpleAttributeSet.java 1.41 04/05/05
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;
8
9 import java.util.Hashtable JavaDoc;
10 import java.util.Enumeration JavaDoc;
11 import java.util.NoSuchElementException JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.io.ObjectInputStream JavaDoc;
14 import java.io.ObjectOutputStream JavaDoc;
15 import java.io.Serializable JavaDoc;
16
17 /**
18  * A straightforward implementation of MutableAttributeSet using a
19  * hash table.
20  * <p>
21  * <strong>Warning:</strong>
22  * Serialized objects of this class will not be compatible with
23  * future Swing releases. The current serialization support is
24  * appropriate for short term storage or RMI between applications running
25  * the same version of Swing. As of 1.4, support for long term storage
26  * of all JavaBeans<sup><font size="-2">TM</font></sup>
27  * has been added to the <code>java.beans</code> package.
28  * Please see {@link java.beans.XMLEncoder}.
29  *
30  * @version 1.41 05/05/04
31  * @author Tim Prinzing
32  */

33 public class SimpleAttributeSet implements MutableAttributeSet JavaDoc, Serializable JavaDoc, Cloneable JavaDoc
34 {
35     /**
36      * An empty attribute set.
37      */

38     public static final AttributeSet JavaDoc EMPTY = new EmptyAttributeSet();
39
40     private transient Hashtable JavaDoc table = new Hashtable JavaDoc(3);
41
42     private static Enumeration JavaDoc emptyEnumeration;
43
44     /**
45      * Creates a new attribute set.
46      */

47     public SimpleAttributeSet() {
48     }
49
50     /**
51      * Creates a new attribute set based on a supplied set of attributes.
52      *
53      * @param source the set of attributes
54      */

55     public SimpleAttributeSet(AttributeSet JavaDoc source) {
56         addAttributes(source);
57     }
58
59     private SimpleAttributeSet(Hashtable JavaDoc table) {
60         this.table = table;
61     }
62
63     /**
64      * Checks whether the set of attributes is empty.
65      *
66      * @return true if the set is empty else false
67      */

68     public boolean isEmpty()
69     {
70         return table.isEmpty();
71     }
72
73     /**
74      * Gets a count of the number of attributes.
75      *
76      * @return the count
77      */

78     public int getAttributeCount() {
79         return table.size();
80     }
81
82     /**
83      * Tells whether a given attribute is defined.
84      *
85      * @param attrName the attribute name
86      * @return true if the attribute is defined
87      */

88     public boolean isDefined(Object JavaDoc attrName) {
89     return table.containsKey(attrName);
90     }
91
92     /**
93      * Compares two attribute sets.
94      *
95      * @param attr the second attribute set
96      * @return true if the sets are equal, false otherwise
97      */

98     public boolean isEqual(AttributeSet JavaDoc attr) {
99     return ((getAttributeCount() == attr.getAttributeCount()) &&
100         containsAttributes(attr));
101     }
102
103     /**
104      * Makes a copy of the attributes.
105      *
106      * @return the copy
107      */

108     public AttributeSet JavaDoc copyAttributes() {
109     return (AttributeSet JavaDoc) clone();
110     }
111
112     /**
113      * Gets the names of the attributes in the set.
114      *
115      * @return the names as an <code>Enumeration</code>
116      */

117     public Enumeration JavaDoc<?> getAttributeNames() {
118         return table.keys();
119     }
120
121     /**
122      * Gets the value of an attribute.
123      *
124      * @param name the attribute name
125      * @return the value
126      */

127     public Object JavaDoc getAttribute(Object JavaDoc name) {
128         Object JavaDoc value = table.get(name);
129     if (value == null) {
130         AttributeSet JavaDoc parent = getResolveParent();
131         if (parent != null) {
132         value = parent.getAttribute(name);
133         }
134     }
135     return value;
136     }
137
138     /**
139      * Checks whether the attribute list contains a
140      * specified attribute name/value pair.
141      *
142      * @param name the name
143      * @param value the value
144      * @return true if the name/value pair is in the list
145      */

146     public boolean containsAttribute(Object JavaDoc name, Object JavaDoc value) {
147         return value.equals(getAttribute(name));
148     }
149
150     /**
151      * Checks whether the attribute list contains all the
152      * specified name/value pairs.
153      *
154      * @param attributes the attribute list
155      * @return true if the list contains all the name/value pairs
156      */

157     public boolean containsAttributes(AttributeSet JavaDoc attributes) {
158         boolean result = true;
159
160         Enumeration JavaDoc names = attributes.getAttributeNames();
161         while (result && names.hasMoreElements()) {
162             Object JavaDoc name = names.nextElement();
163             result = attributes.getAttribute(name).equals(getAttribute(name));
164         }
165
166         return result;
167     }
168
169     /**
170      * Adds an attribute to the list.
171      *
172      * @param name the attribute name
173      * @param value the attribute value
174      */

175     public void addAttribute(Object JavaDoc name, Object JavaDoc value) {
176         table.put(name, value);
177     }
178
179     /**
180      * Adds a set of attributes to the list.
181      *
182      * @param attributes the set of attributes to add
183      */

184     public void addAttributes(AttributeSet JavaDoc attributes) {
185         Enumeration JavaDoc names = attributes.getAttributeNames();
186         while (names.hasMoreElements()) {
187             Object JavaDoc name = names.nextElement();
188             addAttribute(name, attributes.getAttribute(name));
189         }
190     }
191
192     /**
193      * Removes an attribute from the list.
194      *
195      * @param name the attribute name
196      */

197     public void removeAttribute(Object JavaDoc name) {
198         table.remove(name);
199     }
200
201     /**
202      * Removes a set of attributes from the list.
203      *
204      * @param names the set of names to remove
205      */

206     public void removeAttributes(Enumeration JavaDoc<?> names) {
207         while (names.hasMoreElements())
208             removeAttribute(names.nextElement());
209     }
210
211     /**
212      * Removes a set of attributes from the list.
213      *
214      * @param attributes the set of attributes to remove
215      */

216     public void removeAttributes(AttributeSet JavaDoc attributes) {
217     if (attributes == this) {
218         table.clear();
219     }
220     else {
221         Enumeration JavaDoc names = attributes.getAttributeNames();
222         while (names.hasMoreElements()) {
223         Object JavaDoc name = names.nextElement();
224         Object JavaDoc value = attributes.getAttribute(name);
225         if (value.equals(getAttribute(name)))
226             removeAttribute(name);
227         }
228     }
229     }
230
231     /**
232      * Gets the resolving parent. This is the set
233      * of attributes to resolve through if an attribute
234      * isn't defined locally. This is null if there
235      * are no other sets of attributes to resolve
236      * through.
237      *
238      * @return the parent
239      */

240     public AttributeSet JavaDoc getResolveParent() {
241     return (AttributeSet JavaDoc) table.get(StyleConstants.ResolveAttribute);
242     }
243
244     /**
245      * Sets the resolving parent.
246      *
247      * @param parent the parent
248      */

249     public void setResolveParent(AttributeSet JavaDoc parent) {
250     addAttribute(StyleConstants.ResolveAttribute, parent);
251     }
252
253     // --- Object methods ---------------------------------
254

255     /**
256      * Clones a set of attributes.
257      *
258      * @return the new set of attributes
259      */

260     public Object JavaDoc clone() {
261     SimpleAttributeSet JavaDoc attr;
262     try {
263         attr = (SimpleAttributeSet JavaDoc) super.clone();
264         attr.table = (Hashtable JavaDoc) table.clone();
265     } catch (CloneNotSupportedException JavaDoc cnse) {
266         attr = null;
267     }
268         return attr;
269     }
270
271     /**
272      * Returns a hashcode for this set of attributes.
273      * @return a hashcode value for this set of attributes.
274      */

275     public int hashCode() {
276     return table.hashCode();
277     }
278
279     /**
280      * Compares this object to the specified object.
281      * The result is <code>true</code> if the object is an equivalent
282      * set of attributes.
283      * @param obj the object to compare this attribute set with
284      * @return <code>true</code> if the objects are equal;
285      * <code>false</code> otherwise
286      */

287     public boolean equals(Object JavaDoc obj) {
288     if (this == obj) {
289         return true;
290     }
291     if (obj instanceof AttributeSet JavaDoc) {
292         AttributeSet JavaDoc attrs = (AttributeSet JavaDoc) obj;
293         return isEqual(attrs);
294     }
295     return false;
296     }
297
298     /**
299      * Converts the attribute set to a String.
300      *
301      * @return the string
302      */

303     public String JavaDoc toString() {
304     String JavaDoc s = "";
305         Enumeration JavaDoc names = getAttributeNames();
306         while (names.hasMoreElements()) {
307             Object JavaDoc key = names.nextElement();
308             Object JavaDoc value = getAttribute(key);
309         if (value instanceof AttributeSet JavaDoc) {
310         // don't go recursive
311
s = s + key + "=**AttributeSet** ";
312         } else {
313         s = s + key + "=" + value + " ";
314         }
315     }
316     return s;
317     }
318
319     private void writeObject(java.io.ObjectOutputStream JavaDoc s) throws IOException JavaDoc {
320         s.defaultWriteObject();
321     StyleContext.writeAttributeSet(s, this);
322     }
323
324     private void readObject(ObjectInputStream JavaDoc s)
325       throws ClassNotFoundException JavaDoc, IOException JavaDoc {
326         s.defaultReadObject();
327     table = new Hashtable JavaDoc(3);
328     StyleContext.readAttributeSet(s, this);
329     }
330
331     /**
332      * An AttributeSet this is always empty.
333      */

334     static class EmptyAttributeSet implements AttributeSet JavaDoc, Serializable JavaDoc {
335     public int getAttributeCount() {
336         return 0;
337     }
338     public boolean isDefined(Object JavaDoc attrName) {
339         return false;
340     }
341     public boolean isEqual(AttributeSet JavaDoc attr) {
342         return (attr.getAttributeCount() == 0);
343     }
344     public AttributeSet JavaDoc copyAttributes() {
345         return this;
346     }
347     public Object JavaDoc getAttribute(Object JavaDoc key) {
348         return null;
349     }
350     public Enumeration JavaDoc getAttributeNames() {
351         return getEmptyEnumeration();
352     }
353     public boolean containsAttribute(Object JavaDoc name, Object JavaDoc value) {
354         return false;
355     }
356     public boolean containsAttributes(AttributeSet JavaDoc attributes) {
357         return (attributes.getAttributeCount() == 0);
358     }
359     public AttributeSet JavaDoc getResolveParent() {
360         return null;
361     }
362     public boolean equals(Object JavaDoc obj) {
363         if (this == obj) {
364         return true;
365         }
366         return ((obj instanceof AttributeSet JavaDoc) &&
367             (((AttributeSet JavaDoc)obj).getAttributeCount() == 0));
368     }
369     public int hashCode() {
370         return 0;
371     }
372     };
373
374     private static Enumeration JavaDoc getEmptyEnumeration() {
375         if (emptyEnumeration == null) {
376             emptyEnumeration = new Enumeration JavaDoc() {
377                 public boolean hasMoreElements() {
378                     return false;
379                 }
380                 public Object JavaDoc nextElement() {
381                     throw new NoSuchElementException JavaDoc("No more elements");
382                 }
383             };
384         }
385         return emptyEnumeration;
386     }
387 }
388
389
Popular Tags