KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > python > core > PyBeanEventProperty


1 // Copyright (c) Corporation for National Research Initiatives
2
package org.python.core;
3
4 import java.lang.reflect.*;
5 import java.util.Hashtable JavaDoc;
6 import java.io.*;
7
8 public class PyBeanEventProperty extends PyReflectedField
9 {
10     public Method addMethod;
11     public String JavaDoc eventName;
12     public Class JavaDoc eventClass;
13     public String JavaDoc __name__;
14
15     public PyBeanEventProperty(String JavaDoc eventName, Class JavaDoc eventClass,
16                                Method addMethod, Method eventMethod)
17     {
18         __name__ = eventMethod.getName().intern();
19         this.addMethod = addMethod;
20         this.eventName = eventName;
21         this.eventClass = eventClass;
22     }
23
24     public PyObject _doget(PyObject self) {
25         if (self == null)
26             return this;
27
28         initAdapter();
29
30         Object JavaDoc jself = Py.tojava(self, addMethod.getDeclaringClass());
31
32         Object JavaDoc field;
33         try {
34             field = adapterField.get(getAdapter(jself));
35         } catch (Exception JavaDoc exc) {
36             throw Py.JavaError(exc);
37         }
38
39         PyCompoundCallable func;
40         if (field == null) {
41             func = new PyCompoundCallable();
42             setFunction(jself, func);
43             return func;
44         }
45         if (field instanceof PyCompoundCallable)
46             return (PyCompoundCallable)field;
47
48         func = new PyCompoundCallable();
49         setFunction(jself, func);
50         func.append((PyObject)field);
51         return func;
52     }
53
54     private synchronized static Class JavaDoc getAdapterClass(Class JavaDoc c) {
55         // System.err.println("getting adapter for: "+c+", "+c.getName());
56
InternalTables tbl=PyJavaClass.getInternalTables();
57         Object JavaDoc o = tbl.getAdapterClass(c);
58         if (o != null)
59             return (Class JavaDoc)o;
60         Class JavaDoc pc = Py.findClass("org.python.proxies."+c.getName()+"$Adapter");
61         if (pc == null) {
62             //System.err.println("adapter not found for: "+
63
// "org.python.proxies."+
64
// c.getName()+"$Adapter");
65
pc = MakeProxies.makeAdapter(c);
66         }
67         tbl.putAdapterClass(c, pc);
68         return pc;
69     }
70
71     private synchronized Object JavaDoc getAdapter(Object JavaDoc self) {
72         InternalTables tbl=PyJavaClass.getInternalTables();
73         String JavaDoc eventClassName = eventClass.getName();
74
75         Object JavaDoc adapter = tbl.getAdapter(self, eventClassName);
76         if (adapter != null)
77             return adapter;
78
79         try {
80             adapter = adapterClass.newInstance();
81             addMethod.invoke(self, new Object JavaDoc[] {adapter});
82         } catch (Exception JavaDoc e) {
83             throw Py.JavaError(e);
84         }
85         tbl.putAdapter(self, eventClassName, adapter);
86         return adapter;
87     }
88
89     private Field adapterField;
90     private Class JavaDoc adapterClass;
91
92     private void initAdapter() {
93         if (adapterClass == null) {
94             adapterClass = getAdapterClass(eventClass);
95         }
96         if (adapterField == null) {
97             try {
98                 adapterField = adapterClass.getField(__name__);
99             } catch (NoSuchFieldException JavaDoc exc) {
100                 throw Py.AttributeError("Internal bean event error: "+
101                                         __name__);
102             }
103         }
104     }
105
106     private void setFunction(Object JavaDoc self, PyObject callable) {
107         initAdapter();
108         try {
109             adapterField.set(getAdapter(self), callable);
110         } catch (Exception JavaDoc exc) {
111             throw Py.JavaError(exc);
112         }
113     }
114
115     public boolean _doset(PyObject self, PyObject value) {
116         Object JavaDoc jself = Py.tojava(self, addMethod.getDeclaringClass());
117         if (!(value instanceof PyCompoundCallable)) {
118             PyCompoundCallable func = new PyCompoundCallable();
119             setFunction(jself, func);
120             func.append(value);
121         } else {
122             setFunction(jself, value);
123         }
124         return true;
125     }
126
127     public String JavaDoc toString() {
128         return "<beanEventProperty "+__name__+" for event "+
129             eventClass.toString()+" "+Py.idstr(this)+">";
130     }
131 }
132
Popular Tags