1 14 package org.wings.style; 15 16 import org.wings.Renderable; 17 import org.wings.io.Device; 18 19 import java.io.IOException ; 20 import java.io.Serializable ; 21 import java.util.*; 22 23 29 public class CSSAttributeSet 30 implements Renderable, Serializable , Cloneable { 31 public static final CSSAttributeSet EMPTY_ATTRIBUTESET = 32 new CSSAttributeSet() { 33 private UnsupportedOperationException doThrow() { 34 return new UnsupportedOperationException ("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 put(String name, String value) { 38 throw doThrow(); 39 } 40 41 public boolean putAll(CSSAttributeSet attributes) { 42 throw doThrow(); 43 } 44 }; 45 46 private Map map; 47 48 51 private CSSAttributeSet(HashMap map) { 52 this.map = map; 53 } 54 55 58 public CSSAttributeSet() { 59 } 60 61 public CSSAttributeSet(CSSProperty cssProperty, String cssPropertyValue) { 62 put(cssProperty, cssPropertyValue); 63 } 64 65 70 public CSSAttributeSet(CSSAttributeSet source) { 71 putAll(source); 72 } 73 74 79 public final boolean isEmpty() { 80 return map == null || map.isEmpty(); 81 } 82 83 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 104 public final boolean contains(CSSProperty name) { 105 return map == null ? false : map.containsKey(name); 106 } 107 108 113 public final Set properties() { 114 return map == null ? Collections.EMPTY_SET : map.keySet(); 115 } 116 117 123 public final String get(CSSProperty property) { 124 return map == null ? null : (String ) map.get(property); 125 } 126 127 133 public String put(CSSProperty name, String value) { 134 if (map == null) { 135 map = new HashMap(8); 136 } 137 138 if (value == null) 139 return remove(name); 140 return (String ) map.put(name, value); 141 } 142 143 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 167 public String remove(CSSProperty name) { 168 return map == null ? null : (String ) map.remove(name); 169 } 170 171 173 178 public Object clone() { 179 187 188 if (isEmpty()) { 189 return new CSSAttributeSet(); 190 } else { 191 return new CSSAttributeSet((HashMap) ((HashMap) map).clone()); 192 } 193 } 194 195 200 public int hashCode() { 201 return map == null ? 0 : map.hashCode(); 202 } 203 204 210 public boolean equals(Object 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 234 public void writeFiltered(Device d, List l, boolean include) throws IOException { 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 253 public void writeExcluding(Device d, List l) throws IOException { 254 writeFiltered(d, l, false); 255 } 256 257 261 public void writeIncluding(Device d, List l) throws IOException { 262 writeFiltered(d, l, true); 263 } 264 265 268 public void write(Device d) 269 throws IOException { 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 286 public String toString() { 287 StringBuffer b = new StringBuffer (); 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 |