samer@0
|
1 package samer.core;
|
samer@0
|
2 import java.util.Hashtable;
|
samer@0
|
3
|
samer@0
|
4 /**
|
samer@0
|
5 The Registry stores Object class - Viewer class pairs.
|
samer@0
|
6 Associations can be added as Class objects or Class names (Strings).
|
samer@0
|
7 When creating a Viewer for a given object, matching is done on the
|
samer@0
|
8 basis that an exact Class matches is preferred, but failing that, parent
|
samer@0
|
9 Class matches are attempted up the Class heirarchy until a match is
|
samer@0
|
10 found.
|
samer@0
|
11 */
|
samer@0
|
12
|
samer@0
|
13 public class Registry
|
samer@0
|
14 {
|
samer@0
|
15 static { Shell.trace("initialising Registry"); }
|
samer@0
|
16 private static Hashtable map=new Hashtable();
|
samer@0
|
17
|
samer@0
|
18 /** register viewer as a Viewer class for objects of class obj */
|
samer@0
|
19 public static void put(Class obj, Class viewer) { map.put(obj.getName(),viewer); }
|
samer@0
|
20
|
samer@0
|
21 /** Registering by class name saves loading the class itself
|
samer@0
|
22 until it is actually needed. */
|
samer@0
|
23 public static void put(String obj, String viewer) { map.put(obj,viewer); }
|
samer@0
|
24
|
samer@0
|
25 /** Create and return a Viewer for the supplied object */
|
samer@0
|
26 public static Viewer createViewer(Object obj)
|
samer@0
|
27 {
|
samer@0
|
28 Class cl=obj.getClass();
|
samer@0
|
29
|
samer@0
|
30 // lookup by class NAME
|
samer@0
|
31 Object vwr=map.get(cl.getName());
|
samer@0
|
32 while (vwr==null) {
|
samer@0
|
33 cl=cl.getSuperclass();
|
samer@0
|
34 if (cl==null) return null;
|
samer@0
|
35 vwr=map.get(cl.getName());
|
samer@0
|
36 }
|
samer@0
|
37
|
samer@0
|
38 Class [] types = { cl };
|
samer@0
|
39 Object [] parms = { obj };
|
samer@0
|
40
|
samer@0
|
41 try {
|
samer@0
|
42 if (vwr instanceof String) vwr=Class.forName((String)vwr);
|
samer@0
|
43 return (Viewer)((Class)vwr).getConstructor(types).newInstance(parms);
|
samer@0
|
44 } catch (Exception ex) {
|
samer@0
|
45 Shell.trace(ex.toString());
|
samer@0
|
46 return null;
|
samer@0
|
47 }
|
samer@0
|
48 }
|
samer@0
|
49 }
|