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);
22 String[] p = new String[] {
23 "shrink", "hshrink", "vshrink", "x", "y",
24 "width", "height", "flex", "colspan", "rowspan", "cols",
25 "rows", "align", "invisible", "absolute", "globalx",
26 "globaly", "minwidth", "minheight", "height", "width",
27 "maxwidth", "maxheight", "numchildren", "hpad", "vpad",
28 "buffered", "cursor", "mousex", "mousey",
29 "mouseinside", "thisbox", "indexof", "path", "font", "fontsize"
31 for(int i=0; i<p.length; i++) PROHIBITED.put(p[i], Boolean.TRUE);
35 // Instance Members ////////////////////////////////////////////////////////
37 /** the box on which this trap was placed */
38 private Box trapee = null;
40 /** the function for this trap */
41 JS.CompiledFunction f = null;
43 /** the next trap down the trap stack */
44 private Trap next = null;
46 /** the property that the trap was placed on */
47 private Object name = null;
50 // Static Methods //////////////////////////////////////////////////////////////////////////
54 * @param trapee the box to place the trap on
55 * @param name the name of the property to trap on
56 * @param f the function to place as a trap
58 static void addTrap(Box trapee, Object name, JS.CompiledFunction f) {
60 // check if this script has already placed a trap on this property
61 for(Trap t = (Trap)trapee.get(name, Trap.class); t != null; t = t.next)
64 // actually place the trap
66 t.next = (Trap)trapee.get(name, Trap.class);
67 trapee.put(name, Trap.class, t);
76 * @param trapee the box to remove the trap from
77 * @param name the name of the property to trap on
78 * @param f the function to remove
80 static void delTrap(Box trapee, Object name, JS.CompiledFunction f) {
81 Trap t = (Trap)trapee.get(name, Trap.class);
83 trapee.put(name, Trap.class, t.next);
86 for(; t.next != null; t = t.next)
91 Log.logJS("warning: tried to remove a trap that had not been placed");
95 // Instance Methods //////////////////////////////////////////////////////////////////////////
99 public Object perform() throws JS.Exn {
101 if (f.getNumFormalArgs() > 0) return cascade();
102 return f.call(new TrapArgs(this));
103 } catch (Exception e) {
104 Log.log(this, "Exception thrown from within trap: " + e);
109 public void perform(Object val) throws JS.Exn {
111 if (f.getNumFormalArgs() == 0) cascade(val);
112 TrapArgs ta = new TrapArgs(this, val);
113 Object ret = f.call(ta);
114 if (ret != Boolean.FALSE && !ta.cascadeHappened) cascade(val);
115 } catch (Exception e) {
116 Log.log(this, "Exception thrown from within trap: " + e);
120 public Object cascade() {
121 if (next != null) return next.perform();
122 return trapee.get(name, true);
125 public void cascade(Object val) {
126 if (next != null) next.perform(val);
127 trapee.put(name, val, true);
130 private static class TrapArgs extends JS.Array {
132 public boolean cascadeHappened = false;
133 public TrapArgs(Trap t) { this.t = t; }
134 public TrapArgs(Trap t, Object value) { this.t = t; addElement(value); }
136 public void put(Object key, Object val) {
137 if (key.equals("cascade")) { cascadeHappened = true; t.cascade(val); }
138 else super.put(key, val);
141 public Object get(Object key) {
143 if(!(key instanceof String)) return super.get(key);
144 if (key.equals("trapee")) return t.trapee;
145 if (key.equals("trapname")) return t.name;
146 if (key.equals("cascade")) return t.cascade();
147 if (key.equals("callee")) return t.f;
148 return super.get(key);