KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > style > CSSAttributeSet


1 /*
2  * $Id: CSSAttributeSet.java,v 1.1 2005/05/27 09:17:35 blueshift Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings.style;
15
16 import org.wings.Renderable;
17 import org.wings.io.Device;
18
19 import java.io.IOException JavaDoc;
20 import java.io.Serializable JavaDoc;
21 import java.util.*;
22
23 /**
24  * A straightforward implementation of CSSPropertySet using a hash map.
25  *
26  * @author <a HREF="mailto:engels@mercatis.de">Holger Engels</a>
27  * @version $Revision: 1.1 $
28  */

29 public class CSSAttributeSet
30         implements Renderable, Serializable JavaDoc, Cloneable JavaDoc {
31     public static final CSSAttributeSet EMPTY_ATTRIBUTESET =
32             new CSSAttributeSet() {
33                 private UnsupportedOperationException JavaDoc doThrow() {
34                     return new UnsupportedOperationException JavaDoc("cannot change values for the global EMPTY_ATTRIBUTESET. You attempted to modify this unmodifiable CSSPropertySet: create your own instance of a CSSPropertySet first!");
35                 }
36
37                 public String JavaDoc put(String JavaDoc name, String JavaDoc value) {
38                     throw doThrow();
39                 }
40
41                 public boolean putAll(CSSAttributeSet attributes) {
42                     throw doThrow();
43                 }
44             };
45
46     private Map map;
47
48     /**
49      * create a CSSPropertySet from the given HashMap.
50      */

51     private CSSAttributeSet(HashMap map) {
52         this.map = map;
53     }
54
55     /**
56      * Creates a new, empty atribute set.
57      */

58     public CSSAttributeSet() {
59     }
60
61     public CSSAttributeSet(CSSProperty cssProperty, String JavaDoc cssPropertyValue) {
62         put(cssProperty, cssPropertyValue);
63     }
64
65     /**
66      * Creates a new attribute set based on a supplied set of attributes.
67      *
68      * @param source the set of attributes
69      */

70     public CSSAttributeSet(CSSAttributeSet source) {
71         putAll(source);
72     }
73
74     /**
75      * Checks whether the set of attributes is empty.
76      *
77      * @return true if the set is empty else false
78      */

79     public final boolean isEmpty() {
80         return map == null || map.isEmpty();
81     }
82
83     /**
84      * Gets a count of the number of attributes.
85      *
86      * @return the count
87      */

88     public final int size() {
89         return map == null ? 0 : map.size();
90     }
91
92     public final void clear() {
93         if (map != null) {
94             map.clear();
95         }
96     }
97
98     /**
99      * Tells whether a given attribute is defined.
100      *
101      * @param name the attribute name
102      * @return true if the attribute is defined
103      */

104     public final boolean contains(CSSProperty name) {
105         return map == null ? false : map.containsKey(name);
106     }
107
108     /**
109      * Gets the defned CSS properties in the set.
110      *
111      * @return A set of {@link CSSProperty}
112      */

113     public final Set properties() {
114         return map == null ? Collections.EMPTY_SET : map.keySet();
115     }
116
117     /**
118      * Gets the value of an css property.
119      *
120      * @param property the attribute property
121      * @return the value
122      */

123     public final String JavaDoc get(CSSProperty property) {
124         return map == null ? null : (String JavaDoc) map.get(property);
125     }
126
127     /**
128      * Adds an attribute to the list.
129      *
130      * @param name the attribute name
131      * @param value the attribute value
132      */

133     public String JavaDoc put(CSSProperty name, String JavaDoc value) {
134         if (map == null) {
135             map = new HashMap(8);
136         }
137
138         if (value == null)
139             return remove(name);
140         return (String JavaDoc) map.put(name, value);
141     }
142
143     /**
144      * Adds a set of attributes to the list.
145      *
146      * @param attributes the set of attributes to add
147      */

148     public boolean putAll(CSSAttributeSet attributes) {
149         if (map == null) {
150             map = new HashMap(8);
151         }
152
153         boolean changed = false;
154         Iterator names = attributes.properties().iterator();
155         while (names.hasNext()) {
156             CSSProperty property = (CSSProperty) names.next();
157             changed = changed || (put(property, attributes.get(property)) != null);
158         }
159         return changed;
160     }
161
162     /**
163      * Removes an attribute from the list.
164      *
165      * @param name the attribute name
166      */

167     public String JavaDoc remove(CSSProperty name) {
168         return map == null ? null : (String JavaDoc) map.remove(name);
169     }
170
171     // --- Object methods ---------------------------------
172

173     /**
174      * Clones a set of attributes.
175      *
176      * @return the new set of attributes
177      */

178     public Object JavaDoc clone() {
179         /*
180         try {
181             attr = (CSSPropertySet)super.clone();
182             attr.putAll(this);
183         } catch (CloneNotSupportedException cnse) {
184             attr = null;
185         }
186         */

187
188         if (isEmpty()) {
189             return new CSSAttributeSet();
190         } else {
191             return new CSSAttributeSet((HashMap) ((HashMap) map).clone());
192         }
193     }
194
195     /**
196      * Returns a hashcode for this set of attributes.
197      *
198      * @return a hashcode value for this set of attributes.
199      */

200     public int hashCode() {
201         return map == null ? 0 : map.hashCode();
202     }
203
204     /**
205      * Compares two attribute sets.
206      *
207      * @param object the second attribute set
208      * @return true if the sets are equal, false otherwise
209      */

210     public boolean equals(Object JavaDoc object) {
211         if (!(object instanceof CSSAttributeSet))
212             return false;
213         CSSAttributeSet other = (CSSAttributeSet) object;
214
215         if (size() != other.size())
216             return false;
217
218         Iterator names = other.properties().iterator();
219         while (names.hasNext()) {
220             CSSProperty property = (CSSProperty) names.next();
221             if (!other.get(property).equals(get(property)))
222                 return false;
223         }
224         return true;
225     }
226
227     /**
228      * Write style definition to the device. If include is true, write those
229      * contained in the {@link java.util.List}. If include is false, write those not contained
230      * in the {@link java.util.List}.
231      * Basically this is a filter on the styles, so we can separate styles for
232      * one logical component onto multiple real html elements.
233      */

234     public void writeFiltered(Device d, List l, boolean include) throws IOException JavaDoc {
235         if (l == null) l = Collections.EMPTY_LIST;
236         if (map != null) {
237             Iterator names = map.entrySet().iterator();
238             while (names.hasNext()) {
239                 Map.Entry next = (Map.Entry) names.next();
240                 if ( !(l.contains(next.getKey()) ^ include) ) {
241                     d.print(next.getKey()).print(":")
242                             .print(next.getValue())
243                             .print("; ");
244                 }
245             }
246         }
247     }
248     
249     /**
250      * Write style definition to the device. Write only those not contained
251      * in the set.
252      */

253     public void writeExcluding(Device d, List l) throws IOException JavaDoc {
254         writeFiltered(d, l, false);
255     }
256
257     /**
258      * Write style definition to the device. Write only those contained
259      * in the set.
260      */

261     public void writeIncluding(Device d, List l) throws IOException JavaDoc {
262         writeFiltered(d, l, true);
263     }
264
265     /**
266      * Write style definition to the device
267      */

268     public void write(Device d)
269             throws IOException JavaDoc {
270         if (map != null) {
271             Iterator names = map.entrySet().iterator();
272             while (names.hasNext()) {
273                 Map.Entry next = (Map.Entry) names.next();
274                 d.print(next.getKey()).print(":")
275                         .print(next.getValue())
276                         .print("; ");
277             }
278         }
279     }
280
281     /**
282      * Converts the attribute set to a String.
283      *
284      * @return the string
285      */

286     public String JavaDoc toString() {
287         StringBuffer JavaDoc b = new StringBuffer JavaDoc();
288
289         if (map != null) {
290             Iterator names = map.entrySet().iterator();
291             while (names.hasNext()) {
292                 Map.Entry next = (Map.Entry) names.next();
293                 b.append(next.getKey());
294                 b.append(":");
295                 b.append(next.getValue());
296                 b.append("; ");
297             }
298         }
299         return b.toString();
300     }
301 }
302
303
304
Popular Tags