1 // Copyright 2002 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 /** the arguments.cascade() function */
19 public static final JS.Callable cascadeFunction = new JS.Callable() {
20 public Object call(JS.Array args) {
21 Trap currentTrap = TrapContext.get().currentTrap;
22 if (args.length() != 0) TrapContext.get().putCascadeHappened = true;
23 Trap t = currentTrap.next;
24 // if we've hit the end of the trap stack, just do a put(,,,true)
26 if (args.length() == 0) return currentTrap.trapee.get(currentTrap.name, true);
27 currentTrap.trapee.put(currentTrap.name, args.elementAt(0), true);
30 return t.perform(args);
34 /** a vector of weak references to all instances of Trap; used for retheming */
35 private static Hashtable allTraps = new Hashtable(1000);
37 /** List of properties that cannot be trapped */
38 private static final Hash PROHIBITED = new Hash(120, 3);
41 String[] p = new String[] {
42 "sizetoimage", "shrink", "hshrink", "vshrink", "x", "y", "width", "height",
43 "flex", "align", "invisible", "absolute", "globalx", "globaly",
44 "minwidth", "minheight", "height", "width", "maxwidth", "maxheight",
45 "numchildren", "hpad", "vpad", "doublebuffered", "cursor",
46 "mousex", "mousey", "xwt", "static", "mouseinside", "root", "thisbox", "indexof", "svg"
48 for(int i=0; i<p.length; i++) PROHIBITED.put(p[i], Boolean.TRUE);
51 private static Object dummy = new Object();
54 // Instance Members ////////////////////////////////////////////////////////
56 /** the box on which this trap was placed */
57 private Box trapee = null;
59 /** If this trap was actually placed on a root proxy, this is a reference to the proxy */
62 /** the function for this trap */
63 JS.CompiledFunction f = null;
65 /** the name of the property that this trap was placed on */
66 private String name = null;
68 /** the next trap down the trap stack */
69 private Trap next = null;
71 /** true if this is a read-trap */
72 boolean isreadtrap = false;
74 /** The nodeName of the Template whose script placed the trap. See Template.nodeName for more detail. */
75 private String placerNodeName = null;
77 /** our weak reference in the allTraps Hash */
78 private Weak myWeak = Platform.getWeak(this);
81 // Static Methods //////////////////////////////////////////////////////////////////////////
85 * @param trapee the box to place the trap on
86 * @param name the name of the property to trap on
87 * @param f the function to place as a trap
88 * @param isreadtrap true iff this is a read (double-underscore) trap
89 * @param rp if this trap is being placed via a rootproxy, this is that proxy object.
91 static void addTrap(Box trapee, String name, JS.CompiledFunction f, boolean isreadtrap, JS rp) {
93 if (PROHIBITED.get(name) != null || name.startsWith("xwt_")) {
94 Log.log(Trap.class, "Error: you cannot place traps on special property \"" + name + "\"");
98 // find out what script is currently running
99 JS.CompiledFunction placer = JS.Thread.fromJavaThread(java.lang.Thread.currentThread()).getCurrentCompiledFunction();
100 if (placer == null) { Log.log(Trap.class, "placer is null"); return; }
101 String placerNodeName = placer.getSourceName();
103 // check if this script has already placed a trap on this property
104 if (trapee.traps == null) trapee.traps = new Hash(10, 3);
105 for(Trap t = (Trap)trapee.traps.get(name); t != null; t = t.next) if (t.placerNodeName.equals(placerNodeName)) return;
107 // actually place the trap
109 t.next = (Trap)trapee.traps.get(name);
110 trapee.traps.put(name, t);
113 t.placerNodeName = placerNodeName;
116 t.isreadtrap = isreadtrap;
118 // make sure that the surface knows when we place traps on these two properties (slightly inelegant)
119 if (trapee.surface != null && (name.equals("KeyPressed") || name.equals("KeyReleased"))) {
120 trapee.surface.keywatchers.removeElement(trapee);
121 trapee.surface.keywatchers.addElement(trapee);
125 /** returns the function placed as a trap on property <code>name</code> of box <code>b</code> by the currently-running script */
126 public static Trap getTrap(Box b, String name) {
127 if (b.traps == null) return null;
129 String currentFunctionNodeName = JS.Thread.fromJavaThread(java.lang.Thread.currentThread()).getCurrentCompiledFunction().getSourceName();
130 for(Trap cur = (Trap)b.traps.get(name); cur != null; cur = cur.next)
131 if (cur.placerNodeName.equals(currentFunctionNodeName))
137 /** called by Rhino's arguments.trapee hack */
138 public static Object currentTrapee() {
139 Trap current = TrapContext.get().currentTrap;
140 if (current == null) return null;
141 else if (current.rp != null) return current.rp;
142 else return current.trapee;
145 /** called by Rhino's arguments.trapname hack */
146 public static String currentTrapname() {
147 Trap current = TrapContext.get().currentTrap;
148 if (current == null) return null;
149 else return current.name;
153 // Instance Methods //////////////////////////////////////////////////////////////////////////
155 private Trap() { allTraps.put(myWeak, dummy); }
157 /** perform this trap -- arg.length == 0 if this is a get; otherwise it contains a single element to be put */
158 public Object perform(JS.Array args) {
159 TrapContext tc = TrapContext.get();
161 // save both thread-locals on the stack and update their values
162 Trap save_currentTrap = tc.currentTrap;
163 tc.currentTrap = this;
165 boolean save_putCascadeHappened = tc.putCascadeHappened;
166 tc.putCascadeHappened = false;
168 // invoke the trap function
170 if (!isreadtrap && args.length() == 0) return cascadeFunction.call(args);
173 if (Log.verbose) Log.log(this, "debug: reclaimed a dangling trap on property " + name);
174 Object ret = cascadeFunction.call(args);
179 Object ret = f.call(args);
181 // autocascade if required
182 if (args.length() > 0 && !isreadtrap && !tc.putCascadeHappened) cascadeFunction.call(args);
187 if (Log.on) Log.log(this, e);
190 // restore the thread-locals
191 tc.putCascadeHappened = save_putCascadeHappened;
192 tc.currentTrap = save_currentTrap;
198 /** removes this trap */
199 public void delete() {
200 for(Trap last = null, cur = (Trap)trapee.traps.get(name); cur != null; last = cur, cur = cur.next) {
201 if (cur != this) continue;
202 else if (last != null) last.next = cur.next;
203 else if (cur.next == null) trapee.traps.remove(name);
204 else trapee.traps.put(name, cur.next);
206 if (trapee.surface != null && !trapee.is_trapped("KeyPressed") && !trapee.is_trapped("KeyReleased"))
207 trapee.surface.keywatchers.removeElement(trapee);
208 allTraps.remove(myWeak);
211 /** per-thread storage for Traps */
212 private static class TrapContext {
214 private static Hash trapContextByThread = new Hash();
215 private TrapContext() { }
217 private boolean putCascadeHappened = false;
218 private Trap currentTrap = null;
219 private int trapDepth = 0;
221 /** returns the TrapContext for the current thread */
222 static TrapContext get() {
223 TrapContext ret = (TrapContext)trapContextByThread.get(java.lang.Thread.currentThread());
225 ret = new TrapContext();
226 trapContextByThread.put(Thread.currentThread(), ret);