1 19 20 package org.openide.util; 21 22 import java.util.Collection ; 23 import java.util.Collections ; 24 import java.util.Iterator ; 25 import java.util.Set ; 26 import org.openide.util.lookup.Lookups; 27 import org.openide.util.lookup.ProxyLookup; 28 29 56 public abstract class Lookup { 57 59 public static final Lookup EMPTY = new Empty(); 60 61 62 private static Lookup defaultLookup; 63 64 65 public Lookup() { 66 } 67 68 79 public static synchronized Lookup getDefault() { 80 if (defaultLookup != null) { 81 return defaultLookup; 82 } 83 84 String className = System.getProperty("org.openide.util.Lookup" ); 87 88 if ("-".equals(className)) { 90 return EMPTY; 92 } 93 94 ClassLoader l = Thread.currentThread().getContextClassLoader(); 95 96 try { 97 if (className != null) { 98 defaultLookup = (Lookup) Class.forName(className, true, l).newInstance(); 99 100 return defaultLookup; 101 } 102 } catch (Exception e) { 103 e.printStackTrace(); 106 } 107 108 Lookup misl = Lookups.metaInfServices(l); 112 defaultLookup = misl.lookup(Lookup.class); 113 114 if (defaultLookup != null) { 115 return defaultLookup; 116 } 117 118 Lookup.Provider prov = misl.lookup(Lookup.Provider.class); 120 121 if (prov != null) { 122 defaultLookup = Lookups.proxy(prov); 123 124 return defaultLookup; 125 } 126 127 DefLookup def = new DefLookup(); 128 def.init(l, misl); 129 return defaultLookup = def; 130 } 131 132 private static final class DefLookup extends ProxyLookup { 133 public DefLookup() { 134 super(new Lookup[0]); 135 } 136 137 public void init(ClassLoader loader, Lookup metaInfLookup) { 138 Lookup clLookup = Lookups.singleton(loader); 142 setLookups(new Lookup[] { metaInfLookup, clLookup }); 143 } 144 } 145 146 148 private static void resetDefaultLookup() { 149 if (defaultLookup instanceof DefLookup) { 150 DefLookup def = (DefLookup)defaultLookup; 151 ClassLoader l = Thread.currentThread().getContextClassLoader(); 152 def.init(l, Lookups.metaInfServices(l)); 153 } 154 } 155 156 166 public abstract <T> T lookup(Class <T> clazz); 167 168 179 public abstract <T> Result<T> lookup(Template<T> template); 180 181 188 public <T> Item<T> lookupItem(Template<T> template) { 189 Result<T> res = lookup(template); 190 Iterator <? extends Item<T>> it = res.allItems().iterator(); 191 return it.hasNext() ? it.next() : null; 192 } 193 194 202 public <T> Lookup.Result<T> lookupResult(Class <T> clazz) { 203 return lookup(new Lookup.Template<T>(clazz)); 204 } 205 206 214 public <T> Collection <? extends T> lookupAll(Class <T> clazz) { 215 return lookupResult(clazz).allInstances(); 216 } 217 218 223 public interface Provider { 224 228 Lookup getLookup(); 229 } 230 231 235 236 238 public static final class Template<T> extends Object { 239 240 private int hashCode; 241 242 243 private Class <T> type; 244 245 246 private String id; 247 248 249 private T instance; 250 251 256 @Deprecated 257 public Template() { 258 this(null); 259 } 260 261 264 public Template(Class <T> type) { 265 this(type, null, null); 266 } 267 268 273 public Template(Class <T> type, String id, T instance) { 274 this.type = extractType(type); 275 this.id = id; 276 this.instance = instance; 277 } 278 279 @SuppressWarnings ("unchecked") 280 private Class <T> extractType(Class <T> type) { 281 return (type == null) ? (Class <T>)Object .class : type; 282 } 283 284 289 public Class <T> getType() { 290 return type; 291 } 292 293 299 public String getId() { 300 return id; 301 } 302 303 311 public T getInstance() { 312 return instance; 313 } 314 315 318 public int hashCode() { 319 if (hashCode != 0) { 320 return hashCode; 321 } 322 323 hashCode = ((type == null) ? 1 : type.hashCode()) + ((id == null) ? 2 : id.hashCode()) + 324 ((instance == null) ? 3 : 0); 325 326 return hashCode; 327 } 328 329 333 public boolean equals(Object obj) { 334 if (!(obj instanceof Template)) { 335 return false; 336 } 337 338 Template t = (Template) obj; 339 340 if (hashCode() != t.hashCode()) { 341 return false; 344 } 345 346 if (type != t.type) { 347 return false; 348 } 349 350 if (id == null) { 351 if (t.id != null) { 352 return false; 353 } 354 } else { 355 if (!id.equals(t.id)) { 356 return false; 357 } 358 } 359 360 if (instance == null) { 361 return (t.instance == null); 362 } else { 363 return instance.equals(t.instance); 364 } 365 } 366 367 368 public String toString() { 369 return "Lookup.Template[type=" + type + ",id=" + id + ",instance=" + instance + "]"; } 371 } 372 373 378 public static abstract class Result<T> extends Object { 379 384 public abstract void addLookupListener(LookupListener l); 385 386 389 public abstract void removeLookupListener(LookupListener l); 390 391 395 public abstract Collection <? extends T> allInstances(); 396 397 405 public Set <Class <? extends T>> allClasses() { 406 return Collections.emptySet(); 407 } 408 409 417 public Collection <? extends Item<T>> allItems() { 418 return Collections.emptyList(); 419 } 420 } 421 422 428 public static abstract class Item<T> extends Object { 429 432 public abstract T getInstance(); 433 434 437 public abstract Class <? extends T> getType(); 438 439 441 452 public abstract String getId(); 453 454 459 public abstract String getDisplayName(); 460 461 462 public String toString() { 463 return getId(); 464 } 465 } 466 467 private static final class Empty extends Lookup { 471 private static final Result NO_RESULT = new Result() { 472 public void addLookupListener(LookupListener l) { 473 } 474 475 public void removeLookupListener(LookupListener l) { 476 } 477 478 public Collection allInstances() { 479 return Collections.EMPTY_SET; 480 } 481 }; 482 483 Empty() { 484 } 485 486 public <T> T lookup(Class <T> clazz) { 487 return null; 488 } 489 490 @SuppressWarnings ("unchecked") 491 public <T> Result<T> lookup(Template<T> template) { 492 return NO_RESULT; 493 } 494 } 495 } 496 | Popular Tags |