001: /*
002: * @(#)unbind.java 1.2 04/12/06
003: *
004: * Copyright (c) 2002-2004 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package org.pnuts.beans;
010:
011: import pnuts.lang.*;
012: import java.beans.*;
013: import java.util.*;
014: import java.lang.reflect.Method;
015: import java.lang.reflect.InvocationTargetException;
016:
017: /*
018: * function unbind(bean, action {, func })
019: */
020: public class unbind extends PnutsFunction {
021:
022: public unbind() {
023: super ("unbind");
024: }
025:
026: public boolean defined(int nargs) {
027: return nargs == 2 || nargs == 3;
028: }
029:
030: protected Object exec(Object[] args, Context context) {
031: int nargs = args.length;
032: if (nargs != 2 && nargs != 3) {
033: undefined(args, context);
034: return null;
035: }
036: Object bean = args[0];
037: PnutsFunction func;
038: if (nargs == 2) {
039: func = null;
040: } else {
041: func = (PnutsFunction) args[2];
042: if (!func.defined(1)) {
043: throw new PnutsException("");
044: }
045: }
046: String action = (String) args[1];
047: int idx = action.lastIndexOf('.');
048: String listenerType;
049: if (idx > 0) {
050: listenerType = action.substring(0, idx - 1);
051: action = action.substring(idx + 1);
052: } else {
053: listenerType = null;
054: }
055:
056: try {
057: EventSetDescriptor[] eventset = Introspector.getBeanInfo(
058: bean.getClass()).getEventSetDescriptors();
059: for (int i = 0; i < eventset.length; i++) {
060: EventSetDescriptor event = eventset[i];
061: if (listenerType != null
062: && listenerType != event.getListenerType()
063: .getName()) {
064: continue;
065: }
066: Method[] methods = event.getListenerMethods();
067: Method removeMethod = event.getRemoveListenerMethod();
068: for (int j = 0; j < methods.length; j++) {
069: Method m = methods[j];
070: if (m.getName().equals(action)) {
071: unregister(context, bean, action, func,
072: removeMethod);
073: break;
074: }
075: }
076: }
077: } catch (IntrospectionException e1) {
078: throw new PnutsException(e1, context);
079: } catch (IllegalAccessException e2) {
080: throw new PnutsException(e2, context);
081: } catch (InvocationTargetException e3) {
082: throw new PnutsException(e3, context);
083: }
084: return null;
085: }
086:
087: static void unregister(Context context, Object bean, String action,
088: PnutsFunction func, Method removeMethod)
089: throws IllegalAccessException, InvocationTargetException {
090: Hashtable t = (Hashtable) bind.eventAdapterTable.get(bean);
091: if (t != null) {
092: Hashtable u = (Hashtable) t.get(action);
093: if (u != null) {
094: if (func != null) {
095: Object adapter = u.get(func);
096: u.remove(func);
097: if (adapter != null) {
098: removeMethod.invoke(bean,
099: new Object[] { adapter });
100: }
101: } else {
102: for (Enumeration e = u.elements(); e
103: .hasMoreElements();) {
104: removeMethod.invoke(bean, new Object[] { e
105: .nextElement() });
106: }
107: u.clear();
108: }
109: }
110: }
111: }
112:
113: public String toString() {
114: return "function unbind(bean, action { , func } )";
115: }
116: }
|