View Javadoc

1   package org.catacomb.druid.gui.base;
2   
3   
4   import org.catacomb.interlish.structure.ActionRelay;
5   import org.catacomb.report.E;
6   
7   
8   import java.lang.reflect.Method;
9   import java.util.Hashtable;
10  
11  
12  public class DruActionRelay implements ActionRelay {
13  
14  
15      Object actor;
16  
17      Hashtable<String, Method> methodHT;
18  
19  
20      public DruActionRelay(Object ao) {
21          actor = ao;
22  
23  
24          methodHT = new Hashtable<String, Method>();
25          try {
26              Method[] methods = actor.getClass().getDeclaredMethods();
27              for (int i = 0; i < methods.length; i++) {
28                  Method m = methods[i];
29                  methodHT.put(m.getName(), m);
30              }
31  
32  
33          } catch (Exception ex) {
34  
35              E.error(" making action connector " + ex);
36          }
37      }
38  
39  
40      public Object getActor() {
41          return actor;
42      }
43  
44  
45  
46      public void action(String methodName) {
47          invokeMethod(methodName, new Object[0]);
48      }
49  
50  
51      public void actionB(String methodName, boolean b) {
52          Boolean[] args = { new Boolean(b) };
53          invokeMethod(methodName, args);
54      }
55  
56      public void actionI(String methodName, int i) {
57          Integer[] args = { new Integer(i) };
58          invokeMethod(methodName, args);
59      }
60  
61      public void actionD(String methodName, double d) {
62          Double[] args = { new Double(d) };
63          invokeMethod(methodName, args);
64      }
65  
66  
67  
68      public void actionO(String methodName, Object obj) {
69          Object[] args = {obj};
70          invokeMethod(methodName, args);
71      }
72  
73  
74      private Boolean getBoolean(String sarg) {
75          Boolean ret = null;
76          if (sarg != null) {
77              if (sarg.equals("true") || sarg.equals("on")) {
78                  ret = new Boolean(true);
79  
80              } else if (sarg.equals("false") || sarg.equals("off")) {
81                  ret = new Boolean(false);
82              }
83  
84          }
85          return ret;
86      }
87  
88  
89  
90      public void actionS(String methodName, String sarg) {
91  
92          Boolean bl = getBoolean(sarg);
93  
94          if (bl == null)
95              if (sarg == null) {
96                  invokeMethod(methodName, null);
97              } else {
98                  String[] args = { sarg };
99                  invokeMethod(methodName, args);
100             }
101 
102         else {
103             Boolean[] args = { bl };
104             invokeMethod(methodName, args);
105 
106         }
107     }
108 
109 
110 
111     public void invokeMethod(String methodName, Object[] args) {
112 
113 
114         if (methodName != null && methodName.length() > 0) {
115 
116             Class[] ca;
117 
118             if (args == null) {
119                 ca = new Class[0];
120             } else {
121                 ca = new Class[args.length];
122                 for (int i = 0; i < args.length; i++) {
123                     Object oa = args[i];
124                     if (oa == null) {
125 
126                     } else if (oa instanceof Boolean) {
127                         ca[i] = boolean.class;
128 
129                     } else if (oa instanceof Double) {
130                         ca[i] = double.class;
131 
132                     } else if (oa instanceof Integer) {
133                         ca[i] = int.class;
134 
135                     } else {
136                         ca[i] = args[i].getClass();
137                     }
138                 }
139             }
140 
141 
142             try {
143 
144                 // EFF - used to hash these ?;
145                 Method meth = actor.getClass().getDeclaredMethod(methodName, ca);
146                 meth.invoke(actor, args);
147 
148             } catch (NoSuchMethodException ex) {
149                 String sarg = "";
150                 int narg = 0;
151                 if (ca != null && ca.length > 0) {
152                     narg = ca.length;
153                     sarg += "(";
154                     for (int i = 0; i < ca.length; i++) {
155                         if (i > 0) {
156                             sarg += ",";
157                         }
158                         sarg += ca[i];
159                     }
160                     sarg += ")";
161                 } else {
162                     sarg = "()";
163                 }
164                 E.linkToWarning("AC no method " + methodName + " on " + actor +
165                                 " with arg list (n=" + narg + ") " + sarg, actor);
166                 E.warning("call sequence: ");
167 
168             } catch (Exception ex) {
169                 E.error("AC exception while invoking " + methodName +
170                         " on " + actor + " ex=" + ex);
171                 E.info("args were " + args);
172                 ex.printStackTrace();
173             }
174 
175         } else {
176             E.warning("No method set in action relay");
177             // E.printStackTrace();
178         }
179     }
180 
181 
182 
183 
184 }