You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-dev@xmlgraphics.apache.org by cj...@apache.org on 2002/06/27 10:12:06 UTC
cvs commit: xml-batik/sources/org/apache/batik/script/rhino EventTargetWrapHandler.java EventTargetWrapper.java
cjolif 2002/06/27 01:12:06
Modified: sources/org/apache/batik/script/rhino
EventTargetWrapHandler.java EventTargetWrapper.java
Log:
implements RFE 9149
Revision Changes Path
1.5 +1 -3 xml-batik/sources/org/apache/batik/script/rhino/EventTargetWrapHandler.java
Index: EventTargetWrapHandler.java
===================================================================
RCS file: /home/cvs/xml-batik/sources/org/apache/batik/script/rhino/EventTargetWrapHandler.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- EventTargetWrapHandler.java 25 Feb 2002 15:05:33 -0000 1.4
+++ EventTargetWrapHandler.java 27 Jun 2002 08:12:06 -0000 1.5
@@ -13,8 +13,6 @@
import org.w3c.dom.events.EventTarget;
-import org.apache.batik.script.Window;
-
/**
* This is an utility class allowing to pass an ECMAScript function
* as a parameter of the <code>addEventListener</code> method of
1.6 +71 -6 xml-batik/sources/org/apache/batik/script/rhino/EventTargetWrapper.java
Index: EventTargetWrapper.java
===================================================================
RCS file: /home/cvs/xml-batik/sources/org/apache/batik/script/rhino/EventTargetWrapper.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- EventTargetWrapper.java 8 Oct 2001 15:04:39 -0000 1.5
+++ EventTargetWrapper.java 27 Jun 2002 08:12:06 -0000 1.6
@@ -14,6 +14,7 @@
import org.mozilla.javascript.NativeJavaObject;
import org.mozilla.javascript.NativeJavaMethod;
import org.mozilla.javascript.Scriptable;
+import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.Undefined;
import org.mozilla.javascript.WrappedException;
@@ -33,6 +34,9 @@
* with a Rhino function as parameter should redirect the call to
* <code>addEventListener</code> with a Java function object calling
* the Rhino function.
+ * This class also allows to pass an ECMAScript (Rhino) object as
+ * a parameter instead of a function provided the fact that this object
+ * has a <code>handleEvent</code> method.
* @author <a href="mailto:cjolif@ilog.fr">Christophe Jolif</a>
* @version $Id$
*/
@@ -41,10 +45,10 @@
/**
* The Java function object calling the Rhino function.
*/
- class RhinoEventListener implements EventListener {
+ class FunctionEventListener implements EventListener {
private Function function;
- RhinoEventListener(Function f) {
+ FunctionEventListener(Function f) {
function = f;
}
public void handleEvent(Event evt) {
@@ -52,8 +56,31 @@
interpreter.callHandler(function, evt);
} catch (JavaScriptException e) {
// the only simple solution is to forward it as a
- // RuntimeException to be catch by event dispatching
- // in BridgetEventSupport.java
+ // RuntimeException to be catched by event dispatching
+ // in BridgeEventSupport.java
+ // another solution will to give UserAgent to interpreters
+ throw new WrappedException(e);
+ }
+ }
+ }
+
+ class HandleEventListener implements EventListener {
+ private final static String HANDLE_EVENT = "handleEvent";
+
+ private Scriptable scriptable;
+ private Object[] array = new Object[1];
+
+ HandleEventListener(Scriptable s) {
+ scriptable = s;
+ }
+ public void handleEvent(Event evt) {
+ try {
+ array[0] = evt;
+ ScriptableObject.callMethod(scriptable, HANDLE_EVENT, array);
+ } catch (JavaScriptException e) {
+ // the only simple solution is to forward it as a
+ // RuntimeException to be catched by event dispatching
+ // in BridgeEventSupport.java
// another solution will to give UserAgent to interpreters
throw new WrappedException(e);
}
@@ -70,7 +97,7 @@
if (args[1] instanceof Function) {
if (this.get(NAME, this).equals(ADD_NAME)) {
EventListener evtListener =
- new RhinoEventListener((Function)args[1]);
+ new FunctionEventListener((Function)args[1]);
if (listenerMap == null)
listenerMap = new HashMap(2);
listenerMap.put(args[1], evtListener);
@@ -102,6 +129,44 @@
booleanValue());
}
return Undefined.instance;
+ }
+ } else {
+ if (args[1] instanceof org.mozilla.javascript.NativeObject) {
+ if (this.get(NAME, this).equals(ADD_NAME)) {
+ EventListener evtListener =
+ new HandleEventListener((Scriptable)args[1]);
+ if (listenerMap == null)
+ listenerMap = new HashMap(2);
+ listenerMap.put(args[1], evtListener);
+ // we need to marshall args
+ Class[] paramTypes = { String.class, Scriptable.class,
+ Boolean.TYPE };
+ for (int i = 0; i < args.length; i++)
+ args[i] = NativeJavaObject.coerceType(paramTypes[i],
+ args[i]);
+ ((EventTarget)unwrap()).
+ addEventListener((String)args[0],
+ evtListener,
+ ((Boolean)args[2]).booleanValue());
+ return Undefined.instance;
+ } else {
+ if (listenerMap != null) {
+ // we need to marshall args
+ Class[] paramTypes = { String.class, Scriptable.class,
+ Boolean.TYPE };
+ for (int i = 0; i < args.length; i++)
+ args[i] =
+ NativeJavaObject.coerceType(paramTypes[i],
+ args[i]);
+ ((EventTarget)unwrap()).
+ removeEventListener((String)args[0],
+ (EventListener)listenerMap.
+ remove(args[1]),
+ ((Boolean)args[2]).
+ booleanValue());
+ }
+ return Undefined.instance;
+ }
}
}
return super.call(ctx, scope, thisObj, args);
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-dev-help@xml.apache.org