samer@0: package samer.core; samer@0: import java.util.Hashtable; samer@0: samer@0: /** samer@0: The Registry stores Object class - Viewer class pairs. samer@0: Associations can be added as Class objects or Class names (Strings). samer@0: When creating a Viewer for a given object, matching is done on the samer@0: basis that an exact Class matches is preferred, but failing that, parent samer@0: Class matches are attempted up the Class heirarchy until a match is samer@0: found. samer@0: */ samer@0: samer@0: public class Registry samer@0: { samer@0: static { Shell.trace("initialising Registry"); } samer@0: private static Hashtable map=new Hashtable(); samer@0: samer@0: /** register viewer as a Viewer class for objects of class obj */ samer@0: public static void put(Class obj, Class viewer) { map.put(obj.getName(),viewer); } samer@0: samer@0: /** Registering by class name saves loading the class itself samer@0: until it is actually needed. */ samer@0: public static void put(String obj, String viewer) { map.put(obj,viewer); } samer@0: samer@0: /** Create and return a Viewer for the supplied object */ samer@0: public static Viewer createViewer(Object obj) samer@0: { samer@0: Class cl=obj.getClass(); samer@0: samer@0: // lookup by class NAME samer@0: Object vwr=map.get(cl.getName()); samer@0: while (vwr==null) { samer@0: cl=cl.getSuperclass(); samer@0: if (cl==null) return null; samer@0: vwr=map.get(cl.getName()); samer@0: } samer@0: samer@0: Class [] types = { cl }; samer@0: Object [] parms = { obj }; samer@0: samer@0: try { samer@0: if (vwr instanceof String) vwr=Class.forName((String)vwr); samer@0: return (Viewer)((Class)vwr).getConstructor(types).newInstance(parms); samer@0: } catch (Exception ex) { samer@0: Shell.trace(ex.toString()); samer@0: return null; samer@0: } samer@0: } samer@0: }