View Javadoc
1   /*
2    * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  package javax.swing.text;
26  
27  import java.util.Hashtable;
28  import java.util.Enumeration;
29  import java.util.Collections;
30  import java.io.IOException;
31  import java.io.ObjectInputStream;
32  import java.io.Serializable;
33  import java.util.AbstractMap;
34  import java.util.LinkedHashMap;
35  
36  /**
37   * A straightforward implementation of MutableAttributeSet using a
38   * hash table.
39   * <p>
40   * <strong>Warning:</strong>
41   * Serialized objects of this class will not be compatible with
42   * future Swing releases. The current serialization support is
43   * appropriate for short term storage or RMI between applications running
44   * the same version of Swing.  As of 1.4, support for long term storage
45   * of all JavaBeans&trade;
46   * has been added to the <code>java.beans</code> package.
47   * Please see {@link java.beans.XMLEncoder}.
48   *
49   * @author Tim Prinzing
50   */
51  public class SimpleAttributeSet implements MutableAttributeSet, Serializable, Cloneable
52  {
53      private static final long serialVersionUID = -6631553454711782652L;
54  
55      /**
56       * An empty attribute set.
57       */
58      public static final AttributeSet EMPTY = new EmptyAttributeSet();
59  
60      private transient LinkedHashMap<Object, Object> table = new LinkedHashMap<>(3);
61  
62      /**
63       * Creates a new attribute set.
64       */
65      public SimpleAttributeSet() {
66      }
67  
68      /**
69       * Creates a new attribute set based on a supplied set of attributes.
70       *
71       * @param source the set of attributes
72       */
73      public SimpleAttributeSet(AttributeSet source) {
74          addAttributes(source);
75      }
76  
77      /**
78       * Checks whether the set of attributes is empty.
79       *
80       * @return true if the set is empty else false
81       */
82      public boolean isEmpty()
83      {
84          return table.isEmpty();
85      }
86  
87      /**
88       * Gets a count of the number of attributes.
89       *
90       * @return the count
91       */
92      public int getAttributeCount() {
93          return table.size();
94      }
95  
96      /**
97       * Tells whether a given attribute is defined.
98       *
99       * @param attrName the attribute name
100      * @return true if the attribute is defined
101      */
102     public boolean isDefined(Object attrName) {
103         return table.containsKey(attrName);
104     }
105 
106     /**
107      * Compares two attribute sets.
108      *
109      * @param attr the second attribute set
110      * @return true if the sets are equal, false otherwise
111      */
112     public boolean isEqual(AttributeSet attr) {
113         return ((getAttributeCount() == attr.getAttributeCount()) &&
114                 containsAttributes(attr));
115     }
116 
117     /**
118      * Makes a copy of the attributes.
119      *
120      * @return the copy
121      */
122     public AttributeSet copyAttributes() {
123         return (AttributeSet) clone();
124     }
125 
126     /**
127      * Gets the names of the attributes in the set.
128      *
129      * @return the names as an <code>Enumeration</code>
130      */
131     public Enumeration<?> getAttributeNames() {
132         return Collections.enumeration(table.keySet());
133     }
134 
135     /**
136      * Gets the value of an attribute.
137      *
138      * @param name the attribute name
139      * @return the value
140      */
141     public Object getAttribute(Object name) {
142         Object value = table.get(name);
143         if (value == null) {
144             AttributeSet parent = getResolveParent();
145             if (parent != null) {
146                 value = parent.getAttribute(name);
147             }
148         }
149         return value;
150     }
151 
152     /**
153      * Checks whether the attribute list contains a
154      * specified attribute name/value pair.
155      *
156      * @param name the name
157      * @param value the value
158      * @return true if the name/value pair is in the list
159      */
160     public boolean containsAttribute(Object name, Object value) {
161         return value.equals(getAttribute(name));
162     }
163 
164     /**
165      * Checks whether the attribute list contains all the
166      * specified name/value pairs.
167      *
168      * @param attributes the attribute list
169      * @return true if the list contains all the name/value pairs
170      */
171     public boolean containsAttributes(AttributeSet attributes) {
172         boolean result = true;
173 
174         Enumeration names = attributes.getAttributeNames();
175         while (result && names.hasMoreElements()) {
176             Object name = names.nextElement();
177             result = attributes.getAttribute(name).equals(getAttribute(name));
178         }
179 
180         return result;
181     }
182 
183     /**
184      * Adds an attribute to the list.
185      *
186      * @param name the attribute name
187      * @param value the attribute value
188      */
189     public void addAttribute(Object name, Object value) {
190         table.put(name, value);
191     }
192 
193     /**
194      * Adds a set of attributes to the list.
195      *
196      * @param attributes the set of attributes to add
197      */
198     public void addAttributes(AttributeSet attributes) {
199         Enumeration names = attributes.getAttributeNames();
200         while (names.hasMoreElements()) {
201             Object name = names.nextElement();
202             addAttribute(name, attributes.getAttribute(name));
203         }
204     }
205 
206     /**
207      * Removes an attribute from the list.
208      *
209      * @param name the attribute name
210      */
211     public void removeAttribute(Object name) {
212         table.remove(name);
213     }
214 
215     /**
216      * Removes a set of attributes from the list.
217      *
218      * @param names the set of names to remove
219      */
220     public void removeAttributes(Enumeration<?> names) {
221         while (names.hasMoreElements())
222             removeAttribute(names.nextElement());
223     }
224 
225     /**
226      * Removes a set of attributes from the list.
227      *
228      * @param attributes the set of attributes to remove
229      */
230     public void removeAttributes(AttributeSet attributes) {
231         if (attributes == this) {
232             table.clear();
233         }
234         else {
235             Enumeration names = attributes.getAttributeNames();
236             while (names.hasMoreElements()) {
237                 Object name = names.nextElement();
238                 Object value = attributes.getAttribute(name);
239                 if (value.equals(getAttribute(name)))
240                     removeAttribute(name);
241             }
242         }
243     }
244 
245     /**
246      * Gets the resolving parent.  This is the set
247      * of attributes to resolve through if an attribute
248      * isn't defined locally.  This is null if there
249      * are no other sets of attributes to resolve
250      * through.
251      *
252      * @return the parent
253      */
254     public AttributeSet getResolveParent() {
255         return (AttributeSet) table.get(StyleConstants.ResolveAttribute);
256     }
257 
258     /**
259      * Sets the resolving parent.
260      *
261      * @param parent the parent
262      */
263     public void setResolveParent(AttributeSet parent) {
264         addAttribute(StyleConstants.ResolveAttribute, parent);
265     }
266 
267     // --- Object methods ---------------------------------
268 
269     /**
270      * Clones a set of attributes.
271      *
272      * @return the new set of attributes
273      */
274     public Object clone() {
275         SimpleAttributeSet attr;
276         try {
277             attr = (SimpleAttributeSet) super.clone();
278             attr.table = (LinkedHashMap) table.clone();
279         } catch (CloneNotSupportedException cnse) {
280             attr = null;
281         }
282         return attr;
283     }
284 
285     /**
286      * Returns a hashcode for this set of attributes.
287      * @return     a hashcode value for this set of attributes.
288      */
289     public int hashCode() {
290         return table.hashCode();
291     }
292 
293     /**
294      * Compares this object to the specified object.
295      * The result is <code>true</code> if the object is an equivalent
296      * set of attributes.
297      * @param     obj   the object to compare this attribute set with
298      * @return    <code>true</code> if the objects are equal;
299      *            <code>false</code> otherwise
300      */
301     public boolean equals(Object obj) {
302         if (this == obj) {
303             return true;
304         }
305         if (obj instanceof AttributeSet) {
306             AttributeSet attrs = (AttributeSet) obj;
307             return isEqual(attrs);
308         }
309         return false;
310     }
311 
312     /**
313      * Converts the attribute set to a String.
314      *
315      * @return the string
316      */
317     public String toString() {
318         String s = "";
319         Enumeration names = getAttributeNames();
320         while (names.hasMoreElements()) {
321             Object key = names.nextElement();
322             Object value = getAttribute(key);
323             if (value instanceof AttributeSet) {
324                 // don't go recursive
325                 s = s + key + "=**AttributeSet** ";
326             } else {
327                 s = s + key + "=" + value + " ";
328             }
329         }
330         return s;
331     }
332 
333     private void writeObject(java.io.ObjectOutputStream s) throws IOException {
334         s.defaultWriteObject();
335         StyleContext.writeAttributeSet(s, this);
336     }
337 
338     private void readObject(ObjectInputStream s)
339       throws ClassNotFoundException, IOException {
340         s.defaultReadObject();
341         table = new LinkedHashMap<>(3);
342         StyleContext.readAttributeSet(s, this);
343     }
344 
345     /**
346      * An AttributeSet that is always empty.
347      */
348     static class EmptyAttributeSet implements AttributeSet, Serializable {
349         static final long serialVersionUID = -8714803568785904228L;
350 
351         public int getAttributeCount() {
352             return 0;
353         }
354         public boolean isDefined(Object attrName) {
355             return false;
356         }
357         public boolean isEqual(AttributeSet attr) {
358             return (attr.getAttributeCount() == 0);
359         }
360         public AttributeSet copyAttributes() {
361             return this;
362         }
363         public Object getAttribute(Object key) {
364             return null;
365         }
366         public Enumeration getAttributeNames() {
367             return Collections.emptyEnumeration();
368         }
369         public boolean containsAttribute(Object name, Object value) {
370             return false;
371         }
372         public boolean containsAttributes(AttributeSet attributes) {
373             return (attributes.getAttributeCount() == 0);
374         }
375         public AttributeSet getResolveParent() {
376             return null;
377         }
378         public boolean equals(Object obj) {
379             if (this == obj) {
380                 return true;
381             }
382             return ((obj instanceof AttributeSet) &&
383                     (((AttributeSet)obj).getAttributeCount() == 0));
384         }
385         public int hashCode() {
386             return 0;
387         }
388     }
389 }