1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
10 * This class encapsulates a single trap placed on a given node. The
11 * traps for a given property name on a given box are maintained as a
12 * linked list stack, with the most recently placed trap at the head
17 // Static Data //////////////////////////////////////////////////////////////
19 /** List of properties that cannot be trapped */
20 private static final Hash PROHIBITED = new Hash(120, 3);
23 String[] p = new String[] {
24 "shrink", "hshrink", "vshrink", "x", "y",
25 "width", "height", "flex", "colspan", "rowspan", "cols",
26 "rows", "align", "invisible", "absolute", "globalx",
27 "globaly", "minwidth", "minheight", "height", "width",
28 "maxwidth", "maxheight", "numchildren", "hpad", "vpad",
29 "buffered", "cursor", "mousex", "mousey",
30 "mouseinside", "thisbox", "indexof", "path", "font", "fontsize"
32 for(int i=0; i<p.length; i++) PROHIBITED.put(p[i], Boolean.TRUE);
36 // Instance Members ////////////////////////////////////////////////////////
38 /** the box on which this trap was placed */
39 private Box trapee = null;
41 /** the function for this trap */
42 JS.CompiledFunction f = null;
44 /** the next trap down the trap stack */
45 private Trap next = null;
47 /** the property that the trap was placed on */
48 private Object name = null;
51 // Static Methods //////////////////////////////////////////////////////////////////////////
55 * @param trapee the box to place the trap on
56 * @param name the name of the property to trap on
57 * @param f the function to place as a trap
59 static void addTrap(Box trapee, Object name, JS.CompiledFunction f) {
61 // check if this script has already placed a trap on this property
62 for(Trap t = (Trap)trapee.get(name, Trap.class); t != null; t = t.next)
65 // actually place the trap
67 t.next = (Trap)trapee.get(name, Trap.class);
68 trapee.put(name, Trap.class, t);
77 * @param trapee the box to remove the trap from
78 * @param name the name of the property to trap on
79 * @param f the function to remove
81 static void delTrap(Box trapee, Object name, JS.CompiledFunction f) {
82 Trap t = (Trap)trapee.get(name, Trap.class);
84 trapee.put(name, Trap.class, t.next);
87 for(; t.next != null; t = t.next)
92 Log.logJS("warning: tried to remove a trap that had not been placed");
96 // Instance Methods //////////////////////////////////////////////////////////////////////////
100 public Object perform() {
102 if (f.getNumFormalArgs() > 0) return cascade();
103 JS.Thread.current().setTailCall(f, new TrapArgs(this));
105 } catch (Exception e) {
106 Log.log(this, "Exception thrown from within trap: " + e);
111 private static JS.CompiledFunction cascadeHelper = null;
112 private static String cascadeHelperText =
113 "return function(q) { var ret = arguments.doTrap;" +
114 "if (ret != false && !arguments.didCascade) arguments.cascade = q; };";
117 cascadeHelper = JS.parse("cascadeHelper", 1, new StringReader(cascadeHelperText));
118 cascadeHelper = (JS.CompiledFunction)new JS.Thread(cascadeHelper).resume();
119 } catch (Exception e) {
120 Log.log(Trap.class, e);
124 public void perform(Object val) {
126 if (f.getNumFormalArgs() == 0) cascade(val);
127 else JS.Thread.current().setTailCall(cascadeHelper, new TrapArgs(this, val));
128 } catch (Exception e) {
129 Log.log(this, "Exception thrown from within trap: " + e);
134 public Object cascade() {
135 if (next != null) { next.perform(); return null; }
136 return trapee.get(name, true);
139 public void cascade(Object val) {
140 if (next != null) next.perform(val);
141 trapee.put(name, val, true);
144 private static class TrapArgs extends JS.Array {
146 public boolean cascadeHappened = false;
147 public TrapArgs(Trap t) { this.t = t; }
148 public TrapArgs(Trap t, Object value) { this.t = t; addElement(value); }
150 public void put(Object key, Object val) {
151 if (key.equals("cascade")) { cascadeHappened = true; t.cascade(val); }
152 else super.put(key, val);
155 public Object get(Object key) {
157 if(!(key instanceof String)) return super.get(key);
158 if (key.equals("trapee")) return t.trapee;
159 if (key.equals("doTrap")) { JS.Thread.current().setTailCall(t.f, this); return null; }
160 if (key.equals("didCascade")) return cascadeHappened ? Boolean.TRUE : Boolean.FALSE;
161 if (key.equals("trapname")) return t.name;
162 if (key.equals("cascade")) return t.cascade();
163 if (key.equals("callee")) return t.f;
164 return super.get(key);