1 7 package javax.swing.text; 8 9 import java.util.Hashtable ; 10 import java.util.Enumeration ; 11 import java.util.NoSuchElementException ; 12 import java.io.IOException ; 13 import java.io.ObjectInputStream ; 14 import java.io.ObjectOutputStream ; 15 import java.io.Serializable ; 16 17 33 public class SimpleAttributeSet implements MutableAttributeSet , Serializable , Cloneable 34 { 35 38 public static final AttributeSet EMPTY = new EmptyAttributeSet(); 39 40 private transient Hashtable table = new Hashtable (3); 41 42 private static Enumeration emptyEnumeration; 43 44 47 public SimpleAttributeSet() { 48 } 49 50 55 public SimpleAttributeSet(AttributeSet source) { 56 addAttributes(source); 57 } 58 59 private SimpleAttributeSet(Hashtable table) { 60 this.table = table; 61 } 62 63 68 public boolean isEmpty() 69 { 70 return table.isEmpty(); 71 } 72 73 78 public int getAttributeCount() { 79 return table.size(); 80 } 81 82 88 public boolean isDefined(Object attrName) { 89 return table.containsKey(attrName); 90 } 91 92 98 public boolean isEqual(AttributeSet attr) { 99 return ((getAttributeCount() == attr.getAttributeCount()) && 100 containsAttributes(attr)); 101 } 102 103 108 public AttributeSet copyAttributes() { 109 return (AttributeSet ) clone(); 110 } 111 112 117 public Enumeration <?> getAttributeNames() { 118 return table.keys(); 119 } 120 121 127 public Object getAttribute(Object name) { 128 Object value = table.get(name); 129 if (value == null) { 130 AttributeSet parent = getResolveParent(); 131 if (parent != null) { 132 value = parent.getAttribute(name); 133 } 134 } 135 return value; 136 } 137 138 146 public boolean containsAttribute(Object name, Object value) { 147 return value.equals(getAttribute(name)); 148 } 149 150 157 public boolean containsAttributes(AttributeSet attributes) { 158 boolean result = true; 159 160 Enumeration names = attributes.getAttributeNames(); 161 while (result && names.hasMoreElements()) { 162 Object name = names.nextElement(); 163 result = attributes.getAttribute(name).equals(getAttribute(name)); 164 } 165 166 return result; 167 } 168 169 175 public void addAttribute(Object name, Object value) { 176 table.put(name, value); 177 } 178 179 184 public void addAttributes(AttributeSet attributes) { 185 Enumeration names = attributes.getAttributeNames(); 186 while (names.hasMoreElements()) { 187 Object name = names.nextElement(); 188 addAttribute(name, attributes.getAttribute(name)); 189 } 190 } 191 192 197 public void removeAttribute(Object name) { 198 table.remove(name); 199 } 200 201 206 public void removeAttributes(Enumeration <?> names) { 207 while (names.hasMoreElements()) 208 removeAttribute(names.nextElement()); 209 } 210 211 216 public void removeAttributes(AttributeSet attributes) { 217 if (attributes == this) { 218 table.clear(); 219 } 220 else { 221 Enumeration names = attributes.getAttributeNames(); 222 while (names.hasMoreElements()) { 223 Object name = names.nextElement(); 224 Object value = attributes.getAttribute(name); 225 if (value.equals(getAttribute(name))) 226 removeAttribute(name); 227 } 228 } 229 } 230 231 240 public AttributeSet getResolveParent() { 241 return (AttributeSet ) table.get(StyleConstants.ResolveAttribute); 242 } 243 244 249 public void setResolveParent(AttributeSet parent) { 250 addAttribute(StyleConstants.ResolveAttribute, parent); 251 } 252 253 255 260 public Object clone() { 261 SimpleAttributeSet attr; 262 try { 263 attr = (SimpleAttributeSet ) super.clone(); 264 attr.table = (Hashtable ) table.clone(); 265 } catch (CloneNotSupportedException cnse) { 266 attr = null; 267 } 268 return attr; 269 } 270 271 275 public int hashCode() { 276 return table.hashCode(); 277 } 278 279 287 public boolean equals(Object obj) { 288 if (this == obj) { 289 return true; 290 } 291 if (obj instanceof AttributeSet ) { 292 AttributeSet attrs = (AttributeSet ) obj; 293 return isEqual(attrs); 294 } 295 return false; 296 } 297 298 303 public String toString() { 304 String s = ""; 305 Enumeration names = getAttributeNames(); 306 while (names.hasMoreElements()) { 307 Object key = names.nextElement(); 308 Object value = getAttribute(key); 309 if (value instanceof AttributeSet ) { 310 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 s) throws IOException { 320 s.defaultWriteObject(); 321 StyleContext.writeAttributeSet(s, this); 322 } 323 324 private void readObject(ObjectInputStream s) 325 throws ClassNotFoundException , IOException { 326 s.defaultReadObject(); 327 table = new Hashtable (3); 328 StyleContext.readAttributeSet(s, this); 329 } 330 331 334 static class EmptyAttributeSet implements AttributeSet , Serializable { 335 public int getAttributeCount() { 336 return 0; 337 } 338 public boolean isDefined(Object attrName) { 339 return false; 340 } 341 public boolean isEqual(AttributeSet attr) { 342 return (attr.getAttributeCount() == 0); 343 } 344 public AttributeSet copyAttributes() { 345 return this; 346 } 347 public Object getAttribute(Object key) { 348 return null; 349 } 350 public Enumeration getAttributeNames() { 351 return getEmptyEnumeration(); 352 } 353 public boolean containsAttribute(Object name, Object value) { 354 return false; 355 } 356 public boolean containsAttributes(AttributeSet attributes) { 357 return (attributes.getAttributeCount() == 0); 358 } 359 public AttributeSet getResolveParent() { 360 return null; 361 } 362 public boolean equals(Object obj) { 363 if (this == obj) { 364 return true; 365 } 366 return ((obj instanceof AttributeSet ) && 367 (((AttributeSet )obj).getAttributeCount() == 0)); 368 } 369 public int hashCode() { 370 return 0; 371 } 372 }; 373 374 private static Enumeration getEmptyEnumeration() { 375 if (emptyEnumeration == null) { 376 emptyEnumeration = new Enumeration () { 377 public boolean hasMoreElements() { 378 return false; 379 } 380 public Object nextElement() { 381 throw new NoSuchElementException ("No more elements"); 382 } 383 }; 384 } 385 return emptyEnumeration; 386 } 387 } 388 389 | Popular Tags |