2002/08/18 05:28:23
authormegacz <megacz@xwt.org>
Fri, 30 Jan 2004 06:50:11 +0000 (06:50 +0000)
committermegacz <megacz@xwt.org>
Fri, 30 Jan 2004 06:50:11 +0000 (06:50 +0000)
darcs-hash:20040130065011-2ba56-eadde0f2dbb9db558c3f6cbdfe22de95be3cbcd8.gz

CHANGES
src/org/xwt/Trap.java

diff --git a/CHANGES b/CHANGES
index 5951acb..b507a49 100644 (file)
--- a/CHANGES
+++ b/CHANGES
 
 17-Aug megacz Vec.java: fixed infinite loop
 
+17-Aug megacz Trap.java: switched allTraps from a Vector to a Hashtable
+
 
index d2b3954..94624ae 100644 (file)
@@ -16,7 +16,7 @@ public class Trap {
     // Static Data //////////////////////////////////////////////////////////////
 
     /** a vector of weak references to all instances of Trap; used for retheming */
-    private static Vec allTraps = new Vec(1000);
+    private static Hashtable allTraps = new Hashtable(1000);
 
     /** List of properties that cannot be trapped */
     private static final Hash PROHIBITED = new Hash(120, 3);
@@ -32,6 +32,9 @@ public class Trap {
         for(int i=0; i<p.length; i++) PROHIBITED.put(p[i], Boolean.TRUE);
     };
 
+    private static Object dummy = new Object();
+
+
     // Instance Members ////////////////////////////////////////////////////////
 
     /** the box on which this trap was placed */
@@ -55,8 +58,9 @@ public class Trap {
     /** The nodeName of the Template whose script placed the trap. See Template.nodeName for more detail. */
     private String placerNodeName = null;
 
-    /** the index of this Trap in the allTraps vector */
-    private int indexInAllTraps = 0;
+    /** our weak reference in the allTraps Hash */
+    private Weak myWeak = Platform.getWeak(this);
+
 
     // Static Methods //////////////////////////////////////////////////////////////////////////
 
@@ -146,25 +150,27 @@ public class Trap {
 
     /** removes all traps whose function's ultimate parent scope is <tt>b</tt>. Used for retheming */
     public static void removeAllTrapsByBox(Box b) {
-        for(int i=0; i<allTraps.size(); i++) {
-            Trap t = (Trap)((Weak)allTraps.elementAt(i)).get();
-            if (t == null) continue;
+        Enumeration e = allTraps.keys();
+        while(e.hasMoreElements()) {
+            Weak w = (Weak)e.nextElement();
+            Trap t = (Trap)w.get();
+            if (t == null) {
+                allTraps.remove(w);
+                continue;
+            }
             for(Scriptable cur = t.f; cur != null; cur = cur.getParentScope()) {
                 if (cur == b) {
                     t.delete();
-                    i--;
                     break;
                 }
             }
         }
     }
 
+
     // Instance Methods //////////////////////////////////////////////////////////////////////////
 
-    private Trap() {
-        indexInAllTraps = allTraps.size();
-        allTraps.addElement(Platform.getWeak(this));
-    }
+    private Trap() { allTraps.put(myWeak, dummy); }
 
     /** perform this trap -- arg.length == 0 if this is a get; otherwise it contains a single element to be put */
     public Object perform(Object[] arg) {
@@ -220,11 +226,7 @@ public class Trap {
         }
         if (trapee.surface != null && !trapee.is_trapped("KeyPressed") && !trapee.is_trapped("KeyReleased"))
             trapee.surface.keywatchers.removeElement(trapee);
-        if (allTraps.size() == 1) allTraps.setSize(0);
-        else {
-            allTraps.setElementAt(allTraps.elementAt(allTraps.size() - 1), indexInAllTraps);
-            allTraps.setSize(allTraps.size() - 1);
-        }
+        allTraps.remove(myWeak);
     }
 
     /** per-thread storage for Traps */
@@ -250,3 +252,4 @@ public class Trap {
     }
 
 }
+