2003/06/18 06:22:49
[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     /** 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)
25                 if (t == null) {
26                     if (args.length() == 0) return currentTrap.trapee.get(currentTrap.name, true);
27                     currentTrap.trapee.put(currentTrap.name, args.elementAt(0), true);
28                     return null;
29                 }
30                 return t.perform(args);
31             }
32         };
33
34     /** a vector of weak references to all instances of Trap; used for retheming */
35     private static Hashtable allTraps = new Hashtable(1000);
36
37     /** List of properties that cannot be trapped */
38     private static final Hash PROHIBITED = new Hash(120, 3);
39
40     static {
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"
47         };
48         for(int i=0; i<p.length; i++) PROHIBITED.put(p[i], Boolean.TRUE);
49     };
50
51     private static Object dummy = new Object();
52
53
54     // Instance Members ////////////////////////////////////////////////////////
55
56     /** the box on which this trap was placed */
57     private Box trapee = null;
58
59     /** If this trap was actually placed on a root proxy, this is a reference to the proxy */
60     private JS rp = null;
61     
62     /** the function for this trap */
63     JS.CompiledFunction f = null;
64
65     /** the name of the property that this trap was placed on */
66     private String name = null;
67
68     /** the next trap down the trap stack */
69     private Trap next = null;
70
71     /** true if this is a read-trap */
72     boolean isreadtrap = false;
73
74     /** The nodeName of the Template whose script placed the trap. See Template.nodeName for more detail. */
75     private String placerNodeName = null;
76
77     /** our weak reference in the allTraps Hash */
78     private Weak myWeak = Platform.getWeak(this);
79
80
81     // Static Methods //////////////////////////////////////////////////////////////////////////
82
83     /**
84      *  adds a trap.
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.
90      */
91     static void addTrap(Box trapee, String name, JS.CompiledFunction f, boolean isreadtrap, JS rp) {
92
93         if (PROHIBITED.get(name) != null || name.startsWith("xwt_")) {
94             Log.log(Trap.class, "Error: you cannot place traps on special property \"" + name + "\"");
95             return;
96         }
97
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();
102
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;
106         
107         // actually place the trap
108         Trap t = new Trap();
109         t.next = (Trap)trapee.traps.get(name);
110         trapee.traps.put(name, t);
111         t.trapee = trapee;
112         t.f = f;
113         t.placerNodeName = placerNodeName;
114         t.rp = rp;
115         t.name = name;
116         t.isreadtrap = isreadtrap;
117
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);
122         }
123     }
124
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;
128
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))
132                 return cur;
133
134         return null;
135     }
136
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;
143     }
144
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;
150     }
151
152
153     // Instance Methods //////////////////////////////////////////////////////////////////////////
154
155     private Trap() { allTraps.put(myWeak, dummy); }
156
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();
160
161         // save both thread-locals on the stack and update their values
162         Trap save_currentTrap = tc.currentTrap;
163         tc.currentTrap = this;
164
165         boolean save_putCascadeHappened = tc.putCascadeHappened; 
166         tc.putCascadeHappened = false;
167
168         // invoke the trap function
169         try {
170             if (!isreadtrap && args.length() == 0) return cascadeFunction.call(args);
171
172             if (f == null) {
173                 if (Log.verbose) Log.log(this, "debug: reclaimed a dangling trap on property " + name);
174                 Object ret = cascadeFunction.call(args);
175                 delete();
176                 return ret;
177             }
178             
179             Object ret = f.call(args);
180             
181             // autocascade if required
182             if (args.length() > 0 && !isreadtrap && !tc.putCascadeHappened) cascadeFunction.call(args);
183             
184             return ret;
185
186         } catch (JS.Exn e) {
187             if (Log.on) Log.log(this, e);
188
189         } finally {
190             // restore the thread-locals
191             tc.putCascadeHappened = save_putCascadeHappened;
192             tc.currentTrap = save_currentTrap;
193             tc.trapDepth--;
194         }
195         return null;
196     }
197
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);
205         }
206         if (trapee.surface != null && !trapee.is_trapped("KeyPressed") && !trapee.is_trapped("KeyReleased"))
207             trapee.surface.keywatchers.removeElement(trapee);
208         allTraps.remove(myWeak);
209     }
210
211     /** per-thread storage for Traps */
212     private static class TrapContext {
213
214         private static Hash trapContextByThread = new Hash();
215         private TrapContext() { }
216
217         private boolean putCascadeHappened = false;
218         private Trap currentTrap = null;
219         private int trapDepth = 0;
220
221         /** returns the TrapContext for the current thread */
222         static TrapContext get() {
223             TrapContext ret = (TrapContext)trapContextByThread.get(java.lang.Thread.currentThread());
224             if (ret == null) {
225                 ret = new TrapContext();
226                 trapContextByThread.put(Thread.currentThread(), ret);
227             }
228             return ret;
229         }
230
231     }
232
233 }
234