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 /** 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 */
46 /** the function for this trap */
47 JS.CompiledFunction f = null;
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, JS.CompiledFunction f, boolean isreadtrap, JS 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 JS.CompiledFunction placer = JS.Thread.fromJavaThread(java.lang.Thread.currentThread()).getCurrentCompiledFunction();
84 if (placer == null) { Log.log(Trap.class, "placer is null"); return; }
85 String placerNodeName = placer.getSourceName();
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;
91 // actually place the trap
93 t.next = (Trap)trapee.traps.get(name);
94 trapee.traps.put(name, t);
97 t.placerNodeName = placerNodeName;
100 t.isreadtrap = isreadtrap;
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);
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;
113 String currentFunctionNodeName = JS.Thread.fromJavaThread(java.lang.Thread.currentThread()).getCurrentCompiledFunction().getSourceName();
114 for(Trap cur = (Trap)b.traps.get(name); cur != null; cur = cur.next)
115 if (cur.placerNodeName.equals(currentFunctionNodeName))
121 // Instance Methods //////////////////////////////////////////////////////////////////////////
123 private Trap() { allTraps.put(myWeak, dummy); }
125 /** perform this trap -- arg.length == 0 if this is a get; otherwise it contains a single element to be put */
126 public Object perform(JS.Array jsArrayArgs) throws JS.Exn {
127 // TrapContext tc = TrapContext.get();
128 TrapArgs args = new TrapArgs(this,jsArrayArgs);
130 // invoke the trap function
132 if (!isreadtrap && args.length() == 0) return cascade(args);
135 if (Log.verbose) Log.log(this, "debug: reclaimed a dangling trap on property " + name);
136 Object ret = cascade(args);
141 Object ret = f.call(args);
143 // autocascade if required
144 if(args.length() > 0 && !isreadtrap && !args.cascadeHappened) cascade(args);
153 public Object cascade(JS.Array args) {
154 // if we've hit the end of the trap stack, just do a put(,,,true)
156 if (args.length() == 0) return trapee.get(name, true);
157 trapee.put(name, args.elementAt(0), true);
160 return next.perform(args);
163 /** removes this trap */
164 public void delete() {
165 for(Trap last = null, cur = (Trap)trapee.traps.get(name); cur != null; last = cur, cur = cur.next) {
166 if (cur != this) continue;
167 else if (last != null) last.next = cur.next;
168 else if (cur.next == null) trapee.traps.remove(name);
169 else trapee.traps.put(name, cur.next);
171 if (trapee.surface != null && !trapee.is_trapped("KeyPressed") && !trapee.is_trapped("KeyReleased"))
172 trapee.surface.keywatchers.removeElement(trapee);
173 allTraps.remove(myWeak);
176 private static class TrapArgs extends JS.Array {
177 public boolean cascadeHappened;
179 public TrapArgs(Trap t,JS.Array args) {
180 int size = args.length();
182 for(int i=0;i<args.length();i++) setElementAt(args.elementAt(i),i);
183 cascadeHappened = false;
187 public Object get(Object key) {
189 if(!(key instanceof String)) return super.get(key);
191 if(key.equals("trapee")) return t.trapee;
192 if(key.equals("trapname")) return t.name;
193 // FIXME: GETCALL when its available
194 if(key.equals("cascade")) return new JS.Callable() {
195 public Object call(JS.Array args) {
196 if(args.length() != 0) cascadeHappened = true;
197 return t.cascade(args);
200 return super.get(key);