1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
6 import org.mozilla.javascript.*;
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 /** a vector of weak references to all instances of Trap; used for retheming */
19 private static Hashtable allTraps = new Hashtable(1000);
21 /** List of properties that cannot be trapped */
22 private static final Hash PROHIBITED = new Hash(120, 3);
25 String[] p = new String[] {
26 "sizetoimage", "shrink", "hshrink", "vshrink", "x", "y", "width", "height",
27 "flex", "align", "invisible", "absolute", "globalx", "globaly",
28 "minwidth", "minheight", "height", "width", "maxwidth", "maxheight",
29 "numchildren", "hpad", "vpad", "doublebuffered", "cursor",
30 "mousex", "mousey", "xwt", "static", "mouseinside", "root", "thisbox", "indexof", "svg"
32 for(int i=0; i<p.length; i++) PROHIBITED.put(p[i], Boolean.TRUE);
35 private static Object dummy = new Object();
38 // Instance Members ////////////////////////////////////////////////////////
40 /** the box on which this trap was placed */
41 private Box trapee = null;
43 /** If this trap was actually placed on a root proxy, this is a reference to the proxy */
44 private Scriptable rp = null;
46 /** the function for this trap */
49 /** the name of the property that this trap was placed on */
50 private String name = null;
52 /** the next trap down the trap stack */
53 private Trap next = null;
55 /** true if this is a read-trap */
56 boolean isreadtrap = false;
58 /** The nodeName of the Template whose script placed the trap. See Template.nodeName for more detail. */
59 private String placerNodeName = null;
61 /** our weak reference in the allTraps Hash */
62 private Weak myWeak = Platform.getWeak(this);
65 // Static Methods //////////////////////////////////////////////////////////////////////////
69 * @param trapee the box to place the trap on
70 * @param name the name of the property to trap on
71 * @param f the function to place as a trap
72 * @param isreadtrap true iff this is a read (double-underscore) trap
73 * @param rp if this trap is being placed via a rootproxy, this is that proxy object.
75 static void addTrap(Box trapee, String name, Function f, boolean isreadtrap, Scriptable rp) {
77 if (PROHIBITED.get(name) != null || name.startsWith("xwt_")) {
78 Log.log(Trap.class, "Error: you cannot place traps on special property \"" + name + "\"");
82 // find out what script is currently running
83 String placerNodeName = JSObject.getCurrentFunctionSourceName();
85 // check if this script has already placed a trap on this property
86 if (trapee.traps == null) trapee.traps = new Hash(10, 3);
87 for(Trap t = (Trap)trapee.traps.get(name); t != null; t = t.next) if (t.placerNodeName.equals(placerNodeName)) return;
89 // actually place the trap
91 t.next = (Trap)trapee.traps.get(name);
92 trapee.traps.put(name, t);
95 t.placerNodeName = placerNodeName;
98 t.isreadtrap = isreadtrap;
100 // make sure that the surface knows when we place traps on these two properties (slightly inelegant)
101 if (trapee.surface != null && (name.equals("KeyPressed") || name.equals("KeyReleased"))) {
102 trapee.surface.keywatchers.removeElement(trapee);
103 trapee.surface.keywatchers.addElement(trapee);
107 /** returns the function placed as a trap on property <code>name</code> of box <code>b</code> by the currently-running script */
108 public static Trap getTrap(Box b, String name) {
109 if (b.traps == null) return null;
111 String currentFunctionNodeName = JSObject.getCurrentFunctionSourceName();
112 for(Trap cur = (Trap)b.traps.get(name); cur != null; cur = cur.next)
113 if (cur.placerNodeName.equals(currentFunctionNodeName))
119 /** Called by Rhino's arguments.cascade. Note: cx will be null if this was invoked from perform() rather than from a script. */
120 public static final Function cascadeFunction = new CascadeFunction();
121 private static class CascadeFunction extends JSObject implements Function {
122 CascadeFunction() { setSeal(true); }
123 public Scriptable construct(Context cx, Scriptable scope, java.lang.Object[] args) { return null; }
124 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) {
125 Trap currentTrap = TrapContext.get().currentTrap;
126 if (currentTrap == null || (cx != null && Context.getCurrentContext().currentFunction != currentTrap.f)) {
127 if (Log.on) Log.log(this, "attempt to cascade() by a function that was not invoked as a trap at " +
128 cx.interpreterSourceFile + ":" + cx.interpreterLine);
131 if (args.length != 0) TrapContext.get().putCascadeHappened = true;
132 Trap t = currentTrap.next;
133 // if we've hit the end of the trap stack, just do a put(,,,true)
135 if (args.length == 0) return currentTrap.trapee.get(currentTrap.name, currentTrap.trapee, true);
136 currentTrap.trapee.put(currentTrap.name, currentTrap.trapee, args[0], true);
139 return t.perform(args);
143 /** called by Rhino's arguments.trapee hack */
144 public static Object currentTrapee() {
145 Trap current = TrapContext.get().currentTrap;
146 if (current == null) return null;
147 else if (current.rp != null) return current.rp;
148 else return current.trapee;
151 /** called by Rhino's arguments.trapname hack */
152 public static String currentTrapname() {
153 Trap current = TrapContext.get().currentTrap;
154 if (current == null) return null;
155 else return current.name;
158 /** removes all traps whose function's ultimate parent scope is <tt>b</tt>. Used for retheming */
159 public static void removeAllTrapsByBox(Box b) {
160 Enumeration e = allTraps.keys();
161 while(e.hasMoreElements()) {
162 Weak w = (Weak)e.nextElement();
163 Trap t = (Trap)w.get();
168 for(Scriptable cur = t.f; cur != null; cur = cur.getParentScope()) {
178 // Instance Methods //////////////////////////////////////////////////////////////////////////
180 private Trap() { allTraps.put(myWeak, dummy); }
182 /** perform this trap -- arg.length == 0 if this is a get; otherwise it contains a single element to be put */
183 public Object perform(Object[] arg) {
184 TrapContext tc = TrapContext.get();
186 // save both thread-locals on the stack and update their values
187 Trap save_currentTrap = tc.currentTrap;
188 tc.currentTrap = this;
190 boolean save_putCascadeHappened = tc.putCascadeHappened;
191 tc.putCascadeHappened = false;
193 // invoke the trap function
195 if (!isreadtrap && arg.length == 0) return cascadeFunction.call(null, null, null, arg);
198 if (Log.verbose) Log.log(this, "debug: reclaimed a dangling trap on property " + name);
199 Object ret = cascadeFunction.call(null, null, null, arg);
204 Object ret = f.call(Context.enter(), f.getParentScope(), f.getParentScope(), arg);
206 // autocascade if required
207 if (arg.length > 0 && !isreadtrap && !tc.putCascadeHappened) cascadeFunction.call(null, null, null, arg);
211 } catch (EcmaError e) {
212 if (Log.on) Log.log(this, "WARNING: uncaught interpreter exception: " + e.getMessage());
213 if (Log.on) Log.log(this, " thrown from within trap '" + name + "' at " + e.getSourceName() + ":" + e.getLineNumber());
214 } catch (JavaScriptException e) {
215 if (Log.on) Log.log(this, "WARNING: uncaught ecmascript exception: " + e.getMessage());
216 if (Log.on) Log.log(this, " thrown from within trap '" + name + "' at " + e.sourceFile + ":" + e.line);
218 // restore the thread-locals
219 tc.putCascadeHappened = save_putCascadeHappened;
220 tc.currentTrap = save_currentTrap;
226 /** removes this trap */
227 public void delete() {
228 for(Trap last = null, cur = (Trap)trapee.traps.get(name); cur != null; last = cur, cur = cur.next) {
229 if (cur != this) continue;
230 else if (last != null) last.next = cur.next;
231 else if (cur.next == null) trapee.traps.remove(name);
232 else trapee.traps.put(name, cur.next);
234 if (trapee.surface != null && !trapee.is_trapped("KeyPressed") && !trapee.is_trapped("KeyReleased"))
235 trapee.surface.keywatchers.removeElement(trapee);
236 allTraps.remove(myWeak);
239 /** per-thread storage for Traps */
240 private static class TrapContext {
242 private static Hash trapContextByThread = new Hash();
243 private TrapContext() { }
245 private boolean putCascadeHappened = false;
246 private Trap currentTrap = null;
247 private int trapDepth = 0;
249 /** returns the TrapContext for the current thread */
250 static TrapContext get() {
251 TrapContext ret = (TrapContext)trapContextByThread.get(Thread.currentThread());
253 ret = new TrapContext();
254 trapContextByThread.put(Thread.currentThread(), ret);