-// Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
+// Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
package org.xwt;
import java.util.*;
import org.xwt.js.*;
import org.xwt.util.*;
+import java.io.*;
/**
* This class encapsulates a single trap placed on a given node. The
// Static Data //////////////////////////////////////////////////////////////
- /** a vector of weak references to all instances of Trap; used for retheming */
- private static Hashtable allTraps = new Hashtable(1000);
-
/** List of properties that cannot be trapped */
private static final Hash PROHIBITED = new Hash(120, 3);
static {
String[] p = new String[] {
- "sizetoimage", "shrink", "hshrink", "vshrink", "x", "y", "width", "height",
- "flex", "hflex", "vflex", "cols", "rows", "align", "invisible", "absolute", "globalx", "globaly",
- "minwidth", "minheight", "height", "width", "maxwidth", "maxheight",
- "numchildren", "hpad", "vpad", "doublebuffered", "cursor",
- "mousex", "mousey", "xwt", "static", "mouseinside", "root", "thisbox", "indexof", "svg"
+ "shrink", "hshrink", "vshrink", "x", "y",
+ "width", "height", "flex", "colspan", "rowspan", "cols",
+ "rows", "align", "invisible", "absolute", "globalx",
+ "globaly", "minwidth", "minheight", "height", "width",
+ "maxwidth", "maxheight", "numchildren", "hpad", "vpad",
+ "buffered", "cursor", "mousex", "mousey",
+ "mouseinside", "thisbox", "indexof", "path", "font", "fontsize"
};
for(int i=0; i<p.length; i++) PROHIBITED.put(p[i], Boolean.TRUE);
};
- private static Object dummy = new Object();
-
// Instance Members ////////////////////////////////////////////////////////
/** the box on which this trap was placed */
private Box trapee = null;
- /** If this trap was actually placed on a root proxy, this is a reference to the proxy */
- private JS rp = null;
-
/** the function for this trap */
JS.CompiledFunction f = null;
- /** the name of the property that this trap was placed on */
- private String name = null;
-
/** the next trap down the trap stack */
private Trap next = null;
- /** true if this is a read-trap */
- boolean isreadtrap = false;
-
- /** The nodeName of the Template whose script placed the trap. See Template.nodeName for more detail. */
- private String placerNodeName = null;
-
- /** our weak reference in the allTraps Hash */
- private Weak myWeak = Platform.getWeak(this);
+ /** the property that the trap was placed on */
+ private Object name = null;
// Static Methods //////////////////////////////////////////////////////////////////////////
* @param trapee the box to place the trap on
* @param name the name of the property to trap on
* @param f the function to place as a trap
- * @param isreadtrap true iff this is a read (double-underscore) trap
- * @param rp if this trap is being placed via a rootproxy, this is that proxy object.
*/
- static void addTrap(Box trapee, String name, JS.CompiledFunction f, boolean isreadtrap, JS rp) {
-
- if (PROHIBITED.get(name) != null || name.startsWith("xwt_")) {
- Log.log(Trap.class, "Error: you cannot place traps on special property \"" + name + "\"");
- return;
- }
-
- // find out what script is currently running
- JS.CompiledFunction placer = JS.Thread.fromJavaThread(java.lang.Thread.currentThread()).getCurrentCompiledFunction();
- if (placer == null) { Log.log(Trap.class, "placer is null"); return; }
- String placerNodeName = placer.getSourceName();
+ static void addTrap(Box trapee, Object name, JS.CompiledFunction f) {
// check if this script has already placed a trap on this property
- if (trapee.traps == null) trapee.traps = new Hash(10, 3);
- for(Trap t = (Trap)trapee.traps.get(name); t != null; t = t.next) if (t.placerNodeName.equals(placerNodeName)) return;
+ for(Trap t = (Trap)trapee.get(name, Trap.class); t != null; t = t.next)
+ if (t.f == f) return;
// actually place the trap
Trap t = new Trap();
- t.next = (Trap)trapee.traps.get(name);
- trapee.traps.put(name, t);
+ t.next = (Trap)trapee.get(name, Trap.class);
+ trapee.put(name, Trap.class, t);
t.trapee = trapee;
- t.f = f;
- t.placerNodeName = placerNodeName;
- t.rp = rp;
t.name = name;
- t.isreadtrap = isreadtrap;
-
- // make sure that the surface knows when we place traps on these two properties (slightly inelegant)
- if (trapee.surface != null && (name.equals("KeyPressed") || name.equals("KeyReleased"))) {
- trapee.surface.keywatchers.removeElement(trapee);
- trapee.surface.keywatchers.addElement(trapee);
- }
+ t.f = f;
}
- /** returns the function placed as a trap on property <code>name</code> of box <code>b</code> by the currently-running script */
- public static Trap getTrap(Box b, String name) {
- if (b.traps == null) return null;
- String currentFunctionNodeName = JS.Thread.fromJavaThread(java.lang.Thread.currentThread()).getCurrentCompiledFunction().getSourceName();
- for(Trap cur = (Trap)b.traps.get(name); cur != null; cur = cur.next)
- if (cur.placerNodeName.equals(currentFunctionNodeName))
- return cur;
-
- return null;
+ /**
+ * deletes a trap.
+ * @param trapee the box to remove the trap from
+ * @param name the name of the property to trap on
+ * @param f the function to remove
+ */
+ static void delTrap(Box trapee, Object name, JS.CompiledFunction f) {
+ Trap t = (Trap)trapee.get(name, Trap.class);
+ if (t.f == f) {
+ trapee.put(name, Trap.class, t.next);
+ return;
+ }
+ for(; t.next != null; t = t.next)
+ if (t.next.f == f) {
+ t.next = t.next.next;
+ return;
+ }
+ Log.logJS("warning: tried to remove a trap that had not been placed");
}
- // Instance Methods //////////////////////////////////////////////////////////////////////////
-
- private Trap() { allTraps.put(myWeak, dummy); }
- /** the empty object, used for get-traps */
- public static JS.Array emptyargs = new JS.Array();
+ // Instance Methods //////////////////////////////////////////////////////////////////////////
- /** perform this trap -- arg.length == 0 if this is a get; otherwise it contains a single element to be put */
- public Object perform(JS.Array jsArrayArgs) throws JS.Exn {
- // TrapContext tc = TrapContext.get();
- if (jsArrayArgs == null) jsArrayArgs = emptyargs;
- TrapArgs args = new TrapArgs(this,jsArrayArgs);
+ private Trap() { }
- // invoke the trap function
+ public Object perform() {
try {
- if (!isreadtrap && args.length() == 0) return cascade(args);
-
- if (f == null) {
- if (Log.verbose) Log.log(this, "debug: reclaimed a dangling trap on property " + name);
- Object ret = cascade(args);
- delete();
- return ret;
- }
-
- Object ret = f.call(args);
-
- // autocascade if required
- if(args.length() > 0 && !isreadtrap && !args.cascadeHappened) cascade(args);
-
- return ret;
-
- } finally {
-
+ if (f.getNumFormalArgs() > 0) return cascade();
+ JS.Thread.current().setTailCall(f, new TrapArgs(this));
+ return null;
+ } catch (Exception e) {
+ Log.log(this, "Exception thrown from within trap: " + e);
+ return null;
}
}
-
- public Object cascade(JS.Array args) {
- // if we've hit the end of the trap stack, just do a put(,,,true)
- if (next == null) {
- if (args.length() == 0) return trapee.get(name, true);
- trapee.put(name, args.elementAt(0), true);
- return null;
+
+ private static JS.CompiledFunction cascadeHelper = null;
+ private static String cascadeHelperText =
+ "return function(q) { var ret = arguments.doTrap;" +
+ "if (ret != false && !arguments.didCascade) arguments.cascade = q; };";
+ static {
+ try {
+ cascadeHelper = JS.parse("cascadeHelper", 1, new StringReader(cascadeHelperText));
+ cascadeHelper = (JS.CompiledFunction)new JS.Thread(cascadeHelper).resume();
+ } catch (Exception e) {
+ Log.log(Trap.class, e);
}
- return next.perform(args);
}
- /** removes this trap */
- public void delete() {
- for(Trap last = null, cur = (Trap)trapee.traps.get(name); cur != null; last = cur, cur = cur.next) {
- if (cur != this) continue;
- else if (last != null) last.next = cur.next;
- else if (cur.next == null) trapee.traps.remove(name);
- else trapee.traps.put(name, cur.next);
+ public void perform(Object val) {
+ try {
+ if (f.getNumFormalArgs() == 0) cascade(val);
+ else JS.Thread.current().setTailCall(cascadeHelper, new TrapArgs(this, val));
+ } catch (Exception e) {
+ Log.log(this, "Exception thrown from within trap: " + e);
+ e.printStackTrace();
}
- /* FIXME
- if (trapee.surface != null && !trapee.is_trapped("KeyPressed") && !trapee.is_trapped("KeyReleased"))
- trapee.surface.keywatchers.removeElement(trapee);
- */
- allTraps.remove(myWeak);
}
+ public Object cascade() {
+ if (next != null) { next.perform(); return null; }
+ return trapee.get(name, true);
+ }
+
+ public void cascade(Object val) {
+ if (next != null) next.perform(val);
+ trapee.put(name, val, true);
+ }
+
private static class TrapArgs extends JS.Array {
- public boolean cascadeHappened;
private Trap t;
- public TrapArgs(Trap t,JS.Array args) {
- int size = args.length();
- setSize(size);
- for(int i=0;i<args.length();i++) setElementAt(args.elementAt(i),i);
- cascadeHappened = false;
- this.t = t;
- }
+ public boolean cascadeHappened = false;
+ public TrapArgs(Trap t) { this.t = t; }
+ public TrapArgs(Trap t, Object value) { this.t = t; addElement(value); }
+ public void put(Object key, Object val) {
+ if (key.equals("cascade")) { cascadeHappened = true; t.cascade(val); }
+ else super.put(key, val);
+ }
+
public Object get(Object key) {
// common case
if(!(key instanceof String)) return super.get(key);
-
- if(key.equals("trapee")) return t.trapee;
- if(key.equals("trapname")) return t.name;
- // FIXME: GETCALL when its available
- if(key.equals("cascade")) return new JS.Callable() {
- public Object call(JS.Array args) {
- if(args.length() != 0) cascadeHappened = true;
- return t.cascade(args);
- }
- };
+ if (key.equals("trapee")) return t.trapee;
+ if (key.equals("doTrap")) { JS.Thread.current().setTailCall(t.f, this); return null; }
+ if (key.equals("didCascade")) return cascadeHappened ? Boolean.TRUE : Boolean.FALSE;
+ if (key.equals("trapname")) return t.name;
+ if (key.equals("cascade")) return t.cascade();
+ if (key.equals("callee")) return t.f;
return super.get(key);
}
}