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 Vec allTraps = new Vec(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 // Instance Members ////////////////////////////////////////////////////////
37 /** the box on which this trap was placed */
38 private Box trapee = null;
40 /** If this trap was actually placed on a root proxy, this is a reference to the proxy */
41 private Scriptable rp = null;
43 /** the function for this trap */
46 /** the name of the property that this trap was placed on */
47 private String name = null;
49 /** the next trap down the trap stack */
50 private Trap next = null;
52 /** true if this is a read-trap */
53 boolean isreadtrap = false;
55 /** The nodeName of the Template whose script placed the trap. See Template.nodeName for more detail. */
56 private String placerNodeName = null;
58 /** the index of this Trap in the allTraps vector */
59 private int indexInAllTraps = 0;
61 // Static Methods //////////////////////////////////////////////////////////////////////////
65 * @param trapee the box to place the trap on
66 * @param name the name of the property to trap on
67 * @param f the function to place as a trap
68 * @param isreadtrap true iff this is a read (double-underscore) trap
69 * @param rp if this trap is being placed via a rootproxy, this is that proxy object.
71 static void addTrap(Box trapee, String name, Function f, boolean isreadtrap, Scriptable rp) {
73 if (PROHIBITED.get(name) != null || name.startsWith("xwt_")) {
74 System.out.println("Error: you cannot place traps on special property \"" + name + "\"");
78 // find out what script is currently running
79 String placerNodeName = JSObject.getCurrentFunctionSourceName();
81 // check if this script has already placed a trap on this property
82 if (trapee.traps == null) trapee.traps = new Hash(10, 3);
83 for(Trap t = (Trap)trapee.traps.get(name); t != null; t = t.next) if (t.placerNodeName.equals(placerNodeName)) return;
85 // actually place the trap
87 t.next = (Trap)trapee.traps.get(name);
88 trapee.traps.put(name, t);
91 t.placerNodeName = placerNodeName;
94 t.isreadtrap = isreadtrap;
96 // make sure that the surface knows when we place traps on these two properties (slightly inelegant)
97 if (trapee.surface != null && (name.equals("KeyPressed") || name.equals("KeyReleased"))) {
98 trapee.surface.keywatchers.removeElement(trapee);
99 trapee.surface.keywatchers.addElement(trapee);
103 /** returns the function placed as a trap on property <code>name</code> of box <code>b</code> by the currently-running script */
104 public static Trap getTrap(Box b, String name) {
105 if (b.traps == null) return null;
107 String currentFunctionNodeName = JSObject.getCurrentFunctionSourceName();
108 for(Trap cur = (Trap)b.traps.get(name); cur != null; cur = cur.next)
109 if (cur.placerNodeName.equals(currentFunctionNodeName))
115 /** Called by Rhino's arguments.cascade. Note: cx will be null if this was invoked from perform() rather than from a script. */
116 public static final Function cascadeFunction = new CascadeFunction();
117 private static class CascadeFunction extends JSObject implements Function {
118 CascadeFunction() { setSeal(true); }
119 public Scriptable construct(Context cx, Scriptable scope, java.lang.Object[] args) { return null; }
120 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) {
121 Trap currentTrap = TrapContext.get().currentTrap;
122 if (currentTrap == null || (cx != null && Context.getCurrentContext().currentFunction != currentTrap.f)) {
123 if (Log.on) Log.log(this, "attempt to cascade() by a function that was not invoked as a trap at " +
124 cx.interpreterSourceFile + ":" + cx.interpreterLine);
127 if (args.length != 0) TrapContext.get().putCascadeHappened = true;
128 Trap t = currentTrap.next;
129 // if we've hit the end of the trap stack, just do a put(,,,true)
131 if (args.length == 0) return currentTrap.trapee.get(currentTrap.name, currentTrap.trapee, true);
132 currentTrap.trapee.put(currentTrap.name, currentTrap.trapee, args[0], true);
135 return t.perform(args);
139 /** called by Rhino's arguments.trapee hack */
140 public static Object currentTrapee() {
141 Trap current = TrapContext.get().currentTrap;
142 if (current == null) return null;
143 else if (current.rp != null) return current.rp;
144 else return current.trapee;
147 /** removes all traps whose function's ultimate parent scope is <tt>b</tt>. Used for retheming */
148 public static void removeAllTrapsByBox(Box b) {
149 for(int i=0; i<allTraps.size(); i++) {
150 Trap t = (Trap)((Weak)allTraps.elementAt(i)).get();
151 if (t == null) continue;
152 for(Scriptable cur = t.f; cur != null; cur = cur.getParentScope()) {
162 // Instance Methods //////////////////////////////////////////////////////////////////////////
165 indexInAllTraps = allTraps.size();
166 allTraps.addElement(Platform.getWeak(this));
169 /** perform this trap -- arg.length == 0 if this is a get; otherwise it contains a single element to be put */
170 public Object perform(Object[] arg) {
171 TrapContext tc = TrapContext.get();
173 // save both thread-locals on the stack and update their values
174 Trap save_currentTrap = tc.currentTrap;
175 tc.currentTrap = this;
177 boolean save_putCascadeHappened = tc.putCascadeHappened;
178 tc.putCascadeHappened = false;
180 // invoke the trap function
182 if (!isreadtrap && arg.length == 0) return cascadeFunction.call(null, null, null, arg);
185 if (Log.verbose) Log.log(this, "debug: reclaimed a dangling trap on property " + name);
186 Object ret = cascadeFunction.call(null, null, null, arg);
191 Object ret = f.call(Context.enter(), f.getParentScope(), f.getParentScope(), arg);
193 // autocascade if required
194 if (arg.length > 0 && !isreadtrap && !tc.putCascadeHappened) cascadeFunction.call(null, null, null, arg);
198 } catch (EcmaError e) {
199 if (Log.on) Log.log(this, "WARNING: uncaught interpreter exception: " + e.getMessage());
200 if (Log.on) Log.log(this, " thrown from within trap '" + name + "' at " + e.getSourceName() + ":" + e.getLineNumber());
201 } catch (JavaScriptException e) {
202 if (Log.on) Log.log(this, "WARNING: uncaught ecmascript exception: " + e.getMessage());
203 if (Log.on) Log.log(this, " thrown from within trap '" + name + "' at " + e.sourceFile + ":" + e.line);
205 // restore the thread-locals
206 tc.putCascadeHappened = save_putCascadeHappened;
207 tc.currentTrap = save_currentTrap;
213 /** removes this trap */
214 public void delete() {
215 for(Trap last = null, cur = (Trap)trapee.traps.get(name); cur != null; last = cur, cur = cur.next) {
216 if (cur != this) continue;
217 else if (last != null) last.next = cur.next;
218 else if (cur.next == null) trapee.traps.remove(name);
219 else trapee.traps.put(name, cur.next);
221 if (trapee.surface != null && !trapee.is_trapped("KeyPressed") && !trapee.is_trapped("KeyReleased"))
222 trapee.surface.keywatchers.removeElement(trapee);
223 if (allTraps.size() == 1) allTraps.setSize(0);
225 allTraps.setElementAt(allTraps.elementAt(allTraps.size() - 1), indexInAllTraps);
226 allTraps.setSize(allTraps.size() - 1);
230 /** per-thread storage for Traps */
231 private static class TrapContext {
233 private static Hash trapContextByThread = new Hash();
234 private TrapContext() { }
236 private boolean putCascadeHappened = false;
237 private Trap currentTrap = null;
238 private int trapDepth = 0;
240 /** returns the TrapContext for the current thread */
241 static TrapContext get() {
242 TrapContext ret = (TrapContext)trapContextByThread.get(Thread.currentThread());
244 ret = new TrapContext();
245 trapContextByThread.put(Thread.currentThread(), ret);