2003/06/16 08:03:15
[org.ibex.core.git] / src / org / xwt / Trap.java
1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
2 package org.xwt;
3
4 import java.util.*;
5 import org.xwt.js.*;
6 import org.xwt.util.*;
7
8 /**
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
12  *  of the list.
13  */
14 public class Trap {
15
16     // Static Data //////////////////////////////////////////////////////////////
17
18     /** a vector of weak references to all instances of Trap; used for retheming */
19     private static Hashtable allTraps = new Hashtable(1000);
20
21     /** List of properties that cannot be trapped */
22     private static final Hash PROHIBITED = new Hash(120, 3);
23
24     static {
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"
31         };
32         for(int i=0; i<p.length; i++) PROHIBITED.put(p[i], Boolean.TRUE);
33     };
34
35     private static Object dummy = new Object();
36
37
38     // Instance Members ////////////////////////////////////////////////////////
39
40     /** the box on which this trap was placed */
41     private Box trapee = null;
42
43     /** If this trap was actually placed on a root proxy, this is a reference to the proxy */
44     private JS rp = null;
45     
46     /** the function for this trap */
47     JS.CompiledFunction f = null;
48
49     /** the name of the property that this trap was placed on */
50     private String name = null;
51
52     /** the next trap down the trap stack */
53     private Trap next = null;
54
55     /** true if this is a read-trap */
56     boolean isreadtrap = false;
57
58     /** The nodeName of the Template whose script placed the trap. See Template.nodeName for more detail. */
59     private String placerNodeName = null;
60
61     /** our weak reference in the allTraps Hash */
62     private Weak myWeak = Platform.getWeak(this);
63
64
65     // Static Methods //////////////////////////////////////////////////////////////////////////
66
67     /**
68      *  adds a trap.
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.
74      */
75     static void addTrap(Box trapee, String name, JS.CompiledFunction f, boolean isreadtrap, JS rp) {
76
77         if (PROHIBITED.get(name) != null || name.startsWith("xwt_")) {
78             Log.log(Trap.class, "Error: you cannot place traps on special property \"" + name + "\"");
79             return;
80         }
81
82         // find out what script is currently running
83         JS.CompiledFunction placer = Context.getContextForThread(Thread.currentThread()).getCurrentFunction();
84         if (placer == null) { Log.log(Trap.class, "placer is null"); return; }
85         String placerNodeName = placer.getSourceName();
86
87         // check if this script has already placed a trap on this property
88         if (trapee.traps == null) trapee.traps = new Hash(10, 3);
89         for(Trap t = (Trap)trapee.traps.get(name); t != null; t = t.next) if (t.placerNodeName.equals(placerNodeName)) return;
90         
91         // actually place the trap
92         Trap t = new Trap();
93         t.next = (Trap)trapee.traps.get(name);
94         trapee.traps.put(name, t);
95         t.trapee = trapee;
96         t.f = f;
97         t.placerNodeName = placerNodeName;
98         t.rp = rp;
99         t.name = name;
100         t.isreadtrap = isreadtrap;
101
102         // make sure that the surface knows when we place traps on these two properties (slightly inelegant)
103         if (trapee.surface != null && (name.equals("KeyPressed") || name.equals("KeyReleased"))) {
104             trapee.surface.keywatchers.removeElement(trapee);
105             trapee.surface.keywatchers.addElement(trapee);
106         }
107     }
108
109     /** returns the function placed as a trap on property <code>name</code> of box <code>b</code> by the currently-running script */
110     public static Trap getTrap(Box b, String name) {
111         if (b.traps == null) return null;
112
113         String currentFunctionNodeName = Context.getContextForThread(Thread.currentThread()).getCurrentFunction().getSourceName();
114         for(Trap cur = (Trap)b.traps.get(name); cur != null; cur = cur.next)
115             if (cur.placerNodeName.equals(currentFunctionNodeName))
116                 return cur;
117
118         return null;
119     }
120
121     /** Called by Rhino's arguments.cascade. Note: cx will be null if this was invoked from perform() rather than from a script. */
122     public static final CascadeFunction cascadeFunction = new CascadeFunction();
123     private static class CascadeFunction extends JS.Callable {
124         CascadeFunction() { setSeal(true); }
125         public Object call(JS.Array args) { return call(args, Context.getContextForThread(Thread.currentThread()).getCurrentFunction()); }
126         public Object call(JS.Array args, JS.CompiledFunction currentFunction) {
127             Trap currentTrap = TrapContext.get().currentTrap;
128             if (args.length() != 0) TrapContext.get().putCascadeHappened = true;
129             Trap t = currentTrap.next;
130             // if we've hit the end of the trap stack, just do a put(,,,true)
131             if (t == null) {
132                 if (args.length() == 0) return currentTrap.trapee.get(currentTrap.name, true);
133                 currentTrap.trapee.put(currentTrap.name, args.elementAt(0), true);
134                 return null;
135             }
136             return t.perform(args);
137         }
138     };
139
140     /** called by Rhino's arguments.trapee hack */
141     public static Object currentTrapee() {
142         Trap current = TrapContext.get().currentTrap;
143         if (current == null) return null;
144         else if (current.rp != null) return current.rp;
145         else return current.trapee;
146     }
147
148     /** called by Rhino's arguments.trapname hack */
149     public static String currentTrapname() {
150         Trap current = TrapContext.get().currentTrap;
151         if (current == null) return null;
152         else return current.name;
153     }
154
155
156     // Instance Methods //////////////////////////////////////////////////////////////////////////
157
158     private Trap() { allTraps.put(myWeak, dummy); }
159
160     /** perform this trap -- arg.length == 0 if this is a get; otherwise it contains a single element to be put */
161     public Object perform(JS.Array args) {
162         TrapContext tc = TrapContext.get();
163
164         // save both thread-locals on the stack and update their values
165         Trap save_currentTrap = tc.currentTrap;
166         tc.currentTrap = this;
167
168         boolean save_putCascadeHappened = tc.putCascadeHappened; 
169         tc.putCascadeHappened = false;
170
171         // invoke the trap function
172         try {
173             if (!isreadtrap && args.length() == 0) return cascadeFunction.call(args, f);
174
175             if (f == null) {
176                 if (Log.verbose) Log.log(this, "debug: reclaimed a dangling trap on property " + name);
177                 Object ret = cascadeFunction.call(args, f);
178                 delete();
179                 return ret;
180             }
181             
182             Object ret = f.call(args);
183             
184             // autocascade if required
185             if (args.length() > 0 && !isreadtrap && !tc.putCascadeHappened) cascadeFunction.call(args, f);
186             
187             return ret;
188
189         } catch (JS.Exn e) {
190             if (Log.on) Log.log(this, e);
191
192         } finally {
193             // restore the thread-locals
194             tc.putCascadeHappened = save_putCascadeHappened;
195             tc.currentTrap = save_currentTrap;
196             tc.trapDepth--;
197         }
198         return null;
199     }
200
201     /** removes this trap */
202     public void delete() {
203         for(Trap last = null, cur = (Trap)trapee.traps.get(name); cur != null; last = cur, cur = cur.next) {
204             if (cur != this) continue;
205             else if (last != null) last.next = cur.next;
206             else if (cur.next == null) trapee.traps.remove(name);
207             else trapee.traps.put(name, cur.next);
208         }
209         if (trapee.surface != null && !trapee.is_trapped("KeyPressed") && !trapee.is_trapped("KeyReleased"))
210             trapee.surface.keywatchers.removeElement(trapee);
211         allTraps.remove(myWeak);
212     }
213
214     /** per-thread storage for Traps */
215     private static class TrapContext {
216
217         private static Hash trapContextByThread = new Hash();
218         private TrapContext() { }
219
220         private boolean putCascadeHappened = false;
221         private Trap currentTrap = null;
222         private int trapDepth = 0;
223
224         /** returns the TrapContext for the current thread */
225         static TrapContext get() {
226             TrapContext ret = (TrapContext)trapContextByThread.get(Thread.currentThread());
227             if (ret == null) {
228                 ret = new TrapContext();
229                 trapContextByThread.put(Thread.currentThread(), ret);
230             }
231             return ret;
232         }
233
234     }
235
236 }
237