1 7 package javax.swing.text.html; 8 9 import javax.swing.text.*; 10 import java.io.Serializable ; 11 import java.util.*; 12 13 19 class MuxingAttributeSet implements AttributeSet, Serializable { 20 24 public MuxingAttributeSet(AttributeSet[] attrs) { 25 this.attrs = attrs; 26 } 27 28 34 protected MuxingAttributeSet() { 35 } 36 37 41 protected synchronized void setAttributes(AttributeSet[] attrs) { 42 this.attrs = attrs; 43 } 44 45 49 protected synchronized AttributeSet[] getAttributes() { 50 return attrs; 51 } 52 53 58 protected synchronized void insertAttributeSetAt(AttributeSet as, 59 int index) { 60 int numAttrs = attrs.length; 61 AttributeSet newAttrs[] = new AttributeSet[numAttrs + 1]; 62 if (index < numAttrs) { 63 if (index > 0) { 64 System.arraycopy(attrs, 0, newAttrs, 0, index); 65 System.arraycopy(attrs, index, newAttrs, index + 1, 66 numAttrs - index); 67 } 68 else { 69 System.arraycopy(attrs, 0, newAttrs, 1, numAttrs); 70 } 71 } 72 else { 73 System.arraycopy(attrs, 0, newAttrs, 0, numAttrs); 74 } 75 newAttrs[index] = as; 76 attrs = newAttrs; 77 } 78 79 84 protected synchronized void removeAttributeSetAt(int index) { 85 int numAttrs = attrs.length; 86 AttributeSet[] newAttrs = new AttributeSet[numAttrs - 1]; 87 if (numAttrs > 0) { 88 if (index == 0) { 89 System.arraycopy(attrs, 1, newAttrs, 0, numAttrs - 1); 91 } 92 else if (index < (numAttrs - 1)) { 93 System.arraycopy(attrs, 0, newAttrs, 0, index); 95 System.arraycopy(attrs, index + 1, newAttrs, index, 96 numAttrs - index - 1); 97 } 98 else { 99 System.arraycopy(attrs, 0, newAttrs, 0, numAttrs - 1); 101 } 102 } 103 attrs = newAttrs; 104 } 105 106 108 114 public int getAttributeCount() { 115 AttributeSet[] as = getAttributes(); 116 int n = 0; 117 for (int i = 0; i < as.length; i++) { 118 n += as[i].getAttributeCount(); 119 } 120 return n; 121 } 122 123 133 public boolean isDefined(Object key) { 134 AttributeSet[] as = getAttributes(); 135 for (int i = 0; i < as.length; i++) { 136 if (as[i].isDefined(key)) { 137 return true; 138 } 139 } 140 return false; 141 } 142 143 150 public boolean isEqual(AttributeSet attr) { 151 return ((getAttributeCount() == attr.getAttributeCount()) && 152 containsAttributes(attr)); 153 } 154 155 161 public AttributeSet copyAttributes() { 162 AttributeSet[] as = getAttributes(); 163 MutableAttributeSet a = new SimpleAttributeSet(); 164 int n = 0; 165 for (int i = as.length - 1; i >= 0; i--) { 166 a.addAttributes(as[i]); 167 } 168 return a; 169 } 170 171 180 public Object getAttribute(Object key) { 181 AttributeSet[] as = getAttributes(); 182 int n = as.length; 183 for (int i = 0; i < n; i++) { 184 Object o = as[i].getAttribute(key); 185 if (o != null) { 186 return o; 187 } 188 } 189 return null; 190 } 191 192 198 public Enumeration getAttributeNames() { 199 return new MuxingAttributeNameEnumeration(); 200 } 201 202 210 public boolean containsAttribute(Object name, Object value) { 211 return value.equals(getAttribute(name)); 212 } 213 214 222 public boolean containsAttributes(AttributeSet attrs) { 223 boolean result = true; 224 225 Enumeration names = attrs.getAttributeNames(); 226 while (result && names.hasMoreElements()) { 227 Object name = names.nextElement(); 228 result = attrs.getAttribute(name).equals(getAttribute(name)); 229 } 230 231 return result; 232 } 233 234 238 public AttributeSet getResolveParent() { 239 return null; 240 } 241 242 246 private AttributeSet[] attrs; 247 248 249 253 private class MuxingAttributeNameEnumeration implements Enumeration { 254 255 MuxingAttributeNameEnumeration() { 256 updateEnum(); 257 } 258 259 public boolean hasMoreElements() { 260 if (currentEnum == null) { 261 return false; 262 } 263 return currentEnum.hasMoreElements(); 264 } 265 266 public Object nextElement() { 267 if (currentEnum == null) { 268 throw new NoSuchElementException("No more names"); 269 } 270 Object retObject = currentEnum.nextElement(); 271 if (!currentEnum.hasMoreElements()) { 272 updateEnum(); 273 } 274 return retObject; 275 } 276 277 void updateEnum() { 278 AttributeSet[] as = getAttributes(); 279 currentEnum = null; 280 while (currentEnum == null && attrIndex < as.length) { 281 currentEnum = as[attrIndex++].getAttributeNames(); 282 if (!currentEnum.hasMoreElements()) { 283 currentEnum = null; 284 } 285 } 286 } 287 288 289 290 private int attrIndex; 291 292 private Enumeration currentEnum; 293 } 294 } 295 | Popular Tags |