View Javadoc

1   // Copyright 2004, FreeHEP.
2   package hep.wired.util;
3   
4   import java.util.*;
5   
6   import org.openide.util.Lookup;
7   import org.openide.util.Lookup.Result;
8   
9   import org.freehep.application.Application;
10  import org.freehep.application.studio.Studio;
11  import org.freehep.util.FreeHEPLookup;
12  
13  /***
14   * Utility class to lookup values in the registry.
15   * 
16   * @author Mark Donszelmann
17   * @version $Id: WiredRegistry.java 2095 2005-07-22 23:01:35Z duns $
18   */
19  
20  public class WiredRegistry {
21  
22      private static transient FreeHEPLookup registry;
23  
24      private WiredRegistry() {
25      }
26  
27      public static void add(Object obj) {
28          if (registry == null) registry = ((Studio)Application.getApplication()).getLookup();
29          registry.add(obj);        
30      }
31  
32      public static Object lookup(Class cls) {
33          if (registry == null) registry = ((Studio)Application.getApplication()).getLookup();
34          Collection instances = registry.lookup(new Lookup.Template(cls)).allInstances();
35          return instances.isEmpty() ? null : instances.iterator().next();
36      }
37   
38      public static Object lookup(Class cls, String name) {
39          Collection instances = registry.lookup(new Lookup.Template(cls)).allInstances();
40          for (Iterator i=instances.iterator(); i.hasNext(); ) {
41              Object obj = i.next();
42              if (obj instanceof ID) {
43                  if (((ID)obj).getID().equals(name)) return obj;
44              }
45          }
46          return null;
47      }
48      
49      public static Collection allInstances(Class cls) {
50          if (registry == null) registry = ((Studio)Application.getApplication()).getLookup();
51          return registry.lookup(new Lookup.Template(cls)).allInstances();
52      }
53      
54      public static Collection allInstances(Class cls, String name) {
55          Collection instancesWithID = new ArrayList();
56          Collection instances = registry.lookup(new Lookup.Template(cls)).allClasses();
57          for (Iterator i=instances.iterator(); i.hasNext(); ) {
58              Object obj = i.next();
59              if (obj instanceof ID) {
60                  if (((ID)obj).getID().equals(name)) instancesWithID.add(obj);
61              }
62          }
63          return instancesWithID;   
64      }
65      
66      public static Set allClasses(Class cls) {
67          return registry.lookup(new Lookup.Template(cls)).allClasses();
68      } 
69      
70      public static interface ID {
71          public String getID();
72      }
73  }