1 19 20 package org.netbeans.api.editor.settings; 21 22 import java.util.ArrayList ; 23 import java.util.Arrays ; 24 import java.util.Collection ; 25 import java.util.Collections ; 26 import java.util.Enumeration ; 27 import java.util.HashMap ; 28 import java.util.HashSet ; 29 import java.util.List ; 30 import javax.swing.text.AttributeSet ; 31 import javax.swing.text.SimpleAttributeSet ; 32 33 38 public final class AttributesUtilities { 39 40 private static final String ATTR_DISMANTLED_STRUCTURE = "dismantled-structure"; 42 55 public static AttributeSet createImmutable(Object ... keyValuePairs) { 56 assert keyValuePairs.length % 2 == 0 : "There must be even number of prameters. " + 57 "They are key-value pairs of attributes that will be inserted into the set."; 58 59 HashMap <Object , Object > map = new HashMap <Object , Object >(); 60 61 for(int i = keyValuePairs.length / 2 - 1; i >= 0 ; i--) { 62 Object attrKey = keyValuePairs[2 * i]; 63 Object attrValue = keyValuePairs[2 * i + 1]; 64 65 map.put(attrKey, attrValue); 66 } 67 68 return new Immutable(map); 69 } 70 71 83 public static AttributeSet createImmutable(AttributeSet ... sets) { 84 HashMap <Object , Object > map = new HashMap <Object , Object >(); 85 86 for(int i = sets.length - 1; i >= 0; i--) { 87 AttributeSet set = sets[i]; 88 for(Enumeration <?> keys = set.getAttributeNames(); keys.hasMoreElements(); ) { 89 Object attrKey = keys.nextElement(); 90 Object attrValue = set.getAttribute(attrKey); 91 92 map.put(attrKey, attrValue); 93 } 94 } 95 96 return new Immutable(map); 97 } 98 99 111 public static AttributeSet createComposite(AttributeSet ... sets) { 112 if (sets.length == 0) { 113 return SimpleAttributeSet.EMPTY; 114 } else if (sets.length == 1) { 115 return sets[0]; 116 } else { 117 ArrayList <AttributeSet > all = new ArrayList <AttributeSet >(); 118 119 for(AttributeSet s : sets) { 120 if (s instanceof AttributesUtilities.Composite) { 121 all.addAll(((AttributesUtilities.Composite) s).getDelegates()); 122 } else if (s instanceof AttributesUtilities.Proxy) { 123 all.add(((AttributesUtilities.Proxy) s).getDelegate()); 124 } else if (s != null && s != SimpleAttributeSet.EMPTY) { 125 all.add(s); 126 } 127 } 128 129 if (all.size() == 0) { 130 return SimpleAttributeSet.EMPTY; 131 } else { 132 return new Composite(all.toArray(new AttributeSet [all.size()])); 133 } 134 } 135 } 136 137 private static List <AttributeSet > dismantle(AttributeSet set) { 138 ArrayList <AttributeSet > sets = new ArrayList <AttributeSet >(); 139 140 if (set instanceof Proxy) { 141 sets.addAll(dismantle(((Proxy) set).getDelegate())); 142 } else if (set instanceof Composite) { 143 List <AttributeSet > delegates = ((Composite) set).getDelegates(); 144 for(AttributeSet delegate : delegates) { 145 sets.addAll(dismantle(delegate)); 146 } 147 } else { 148 sets.add(set); 149 } 150 151 return sets; 152 } 153 154 158 private AttributesUtilities() { 159 } 161 162 private static class Immutable implements AttributeSet { 163 164 private final HashMap <Object , Object > attribs; 165 private AttributeSet parent = null; 166 167 168 private Immutable(HashMap <Object , Object > attribs) { 169 this.attribs = attribs == null ? new HashMap <Object , Object >() : attribs; 170 } 171 172 public synchronized void setResolveParent(AttributeSet parent) { 173 this.parent = parent; 174 } 175 176 public synchronized boolean containsAttributes(AttributeSet attributes) { 177 for(Enumeration names = attributes.getAttributeNames(); names.hasMoreElements(); ) { 178 Object name = names.nextElement(); 179 Object value = attributes.getAttribute(name); 180 181 if (!containsAttribute(name, value)) { 182 return false; 183 } 184 } 185 186 return true; 187 } 188 189 public synchronized boolean isEqual(AttributeSet attr) { 190 return containsAttributes(attr) && attr.containsAttributes(this); 191 } 192 193 public synchronized Object getAttribute(Object key) { 194 195 if (AttributeSet.ResolveAttribute == key) { 197 return parent; 198 } 199 200 if (attribs.containsKey(key)) { 202 return attribs.get(key); 203 } 204 205 if (parent != null) { 207 return parent.getAttribute(key); 208 } else { 209 return null; 210 } 211 } 212 213 public synchronized boolean isDefined(Object key) { 214 return attribs.containsKey(key); 215 } 216 217 public synchronized boolean containsAttribute(Object key, Object value) { 218 if (attribs.containsKey(key)) { 219 Object attrValue = attribs.get(key); 220 if ((value == null && attrValue == null) || 221 (value != null && attrValue != null && value.equals(attrValue)) 222 ) { 223 return true; 224 } 225 } 226 227 return false; 228 } 229 230 public AttributeSet copyAttributes() { 231 return new Proxy(this); 232 } 233 234 237 public synchronized int getAttributeCount() { 238 return attribs.size(); 239 } 240 241 244 public synchronized Enumeration <?> getAttributeNames() { 245 return Collections.enumeration(attribs.keySet()); 246 } 247 248 public synchronized AttributeSet getResolveParent() { 249 return parent; 250 } 251 252 } 254 private static final class Proxy implements AttributeSet { 255 256 private AttributeSet original; 257 258 public Proxy(AttributeSet original) { 259 this.original = original; 260 } 261 262 public AttributeSet getDelegate() { 263 return original; 264 } 265 266 public boolean isEqual(AttributeSet attr) { 267 return original.isEqual(attr); 268 } 269 270 public boolean containsAttributes(AttributeSet attributes) { 271 return original.containsAttributes(attributes); 272 } 273 274 public boolean isDefined(Object attrName) { 275 return original.isDefined(attrName); 276 } 277 278 public Object getAttribute(Object key) { 279 if (key instanceof String && key.equals(ATTR_DISMANTLED_STRUCTURE)) { 280 return dismantle(this); 281 } else { 282 return original.getAttribute(key); 283 } 284 } 285 286 public AttributeSet getResolveParent() { 287 return original.getResolveParent(); 288 } 289 290 public Enumeration <?> getAttributeNames() { 291 return original.getAttributeNames(); 292 } 293 294 public int getAttributeCount() { 295 return original.getAttributeCount(); 296 } 297 298 public AttributeSet copyAttributes() { 299 return original.copyAttributes(); 300 } 301 302 public boolean containsAttribute(Object name, Object value) { 303 return original.containsAttribute(name, value); 304 } 305 } 307 private static final class Composite implements AttributeSet { 308 309 private final AttributeSet [] delegates; 310 311 public Composite(AttributeSet ... delegates) { 312 this.delegates = delegates; 313 } 314 315 public List <AttributeSet > getDelegates() { 316 return Arrays.asList(delegates); 317 } 318 319 public boolean isEqual(AttributeSet attr) { 320 return containsAttributes(attr) && attr.containsAttributes(this); 321 } 322 323 public boolean containsAttributes(AttributeSet attributes) { 324 for(Enumeration <?> keys = attributes.getAttributeNames(); keys.hasMoreElements(); ) { 325 Object key = keys.nextElement(); 326 Object value = attributes.getAttribute(key); 327 328 if (!containsAttribute(key, value)) { 329 return false; 330 } 331 } 332 333 return true; 334 } 335 336 public boolean isDefined(Object key) { 337 for(AttributeSet delegate : delegates) { 338 if (delegate.isDefined(key)) { 339 return true; 340 } 341 } 342 343 return false; 344 } 345 346 public Object getAttribute(Object key) { 347 if (key instanceof String && key.equals(ATTR_DISMANTLED_STRUCTURE)) { 348 return dismantle(this); 349 } 350 351 for(AttributeSet delegate : delegates) { 352 if (delegate.isDefined(key)) { 353 return delegate.getAttribute(key); 354 } 355 } 356 357 return null; 358 } 359 360 public AttributeSet getResolveParent() { 361 return null; 362 } 363 364 public Enumeration <?> getAttributeNames() { 365 return Collections.enumeration(getAllKeys()); 366 } 367 368 public int getAttributeCount() { 369 return getAllKeys().size(); 370 } 371 372 public AttributeSet copyAttributes() { 373 return createImmutable(delegates); 374 } 375 376 public boolean containsAttribute(Object key, Object value) { 377 for(AttributeSet delegate : delegates) { 378 if (delegate.containsAttribute(key, value)) { 379 return true; 380 } 381 } 382 383 return false; 384 } 385 386 private Collection <?> getAllKeys() { 387 HashSet <Object > allKeys = new HashSet <Object >(); 388 389 for(AttributeSet delegate : delegates) { 390 for(Enumeration <?> keys = delegate.getAttributeNames(); keys.hasMoreElements(); ) { 391 Object key = keys.nextElement(); 392 allKeys.add(key); 393 } 394 } 395 396 return allKeys; 397 } 398 } } 400 | Popular Tags |