1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
9 * This class encapsulates a single trap placed on a given node. The
10 * traps for a given property name on a given box are maintained as a
11 * linked list stack, with the most recently placed trap at the head
16 // Static Data //////////////////////////////////////////////////////////////
18 /** List of properties that cannot be trapped */
19 private static final Hash PROHIBITED = new Hash(120, 3);
23 String[] p = new String[] {
24 "sizetoimage", "shrink", "hshrink", "vshrink", "x", "y",
25 "width", "height", "flex", "hflex", "vflex", "cols",
26 "rows", "align", "invisible", "absolute", "globalx",
27 "globaly", "minwidth", "minheight", "height", "width",
28 "maxwidth", "maxheight", "numchildren", "hpad", "vpad",
29 "doublebuffered", "cursor", "mousex", "mousey", "xwt",
30 "static", "mouseinside", "root", "thisbox", "indexof",
33 for(int i=0; i<p.length; i++) PROHIBITED.put(p[i], Boolean.TRUE);
37 // Instance Members ////////////////////////////////////////////////////////
39 /** the box on which this trap was placed */
40 private Box trapee = null;
42 /** the function for this trap */
43 JS.CompiledFunction f = null;
45 /** the next trap down the trap stack */
46 private Trap next = null;
48 /** the property that the trap was placed on */
49 private Object name = null;
52 // Static Methods //////////////////////////////////////////////////////////////////////////
56 * @param trapee the box to place the trap on
57 * @param name the name of the property to trap on
58 * @param f the function to place as a trap
60 static void addTrap(Box trapee, Object name, JS.CompiledFunction f) {
62 // check if this script has already placed a trap on this property
63 for(Trap t = (Trap)trapee.get(name, Trap.class); t != null; t = t.next)
66 // actually place the trap
68 t.next = (Trap)trapee.get(name, Trap.class);
69 trapee.put(name, Trap.class, t);
78 * @param trapee the box to remove the trap from
79 * @param name the name of the property to trap on
80 * @param f the function to remove
82 static void delTrap(Box trapee, Object name, JS.CompiledFunction f) {
83 Trap t = (Trap)trapee.get(name, Trap.class);
85 trapee.put(name, Trap.class, t.next);
88 for(; t.next != null; t = t.next)
93 Log.logJS("warning: tried to remove a trap that had not been placed");
97 // Instance Methods //////////////////////////////////////////////////////////////////////////
101 public Object perform() throws JS.Exn {
102 if (f.getNumFormalArgs() > 0) return cascade();
103 return f.call(new TrapArgs(this));
106 public void perform(Object val) throws JS.Exn {
107 if (f.getNumFormalArgs()== 0) cascade(val);
108 f.call(new TrapArgs(this, val));
111 public Object cascade() {
112 if (next != null) return next.perform();
113 return trapee.get(name, true);
116 public void cascade(Object val) {
117 if (next != null) next.perform(val);
118 trapee.put(name, val, true);
121 private static class TrapArgs extends JS.Array {
123 public TrapArgs(Trap t) { this.t = t; }
124 public TrapArgs(Trap t, Object value) { this.t = t; addElement(value); }
126 public void put(Object key, Object val) {
127 if (key.equals("cascade")) t.cascade(val);
128 else super.put(key, val);
131 public Object get(Object key) {
133 if(!(key instanceof String)) return super.get(key);
134 if (key.equals("trapee")) return t.trapee;
135 if (key.equals("trapname")) return t.name;
136 if (key.equals("cascade")) return t.cascade();
137 return super.get(key);