2003/09/21 10:26:53
[org.ibex.core.git] / src / org / xwt / Template.java
index ee0bea5..11bb1fc 100644 (file)
@@ -165,7 +165,7 @@ public class Template {
      *  @param pboxes a vector of all box parents on which to put $-references
      *  @param ptemplates a vector of the nodeNames to recieve private references on the pboxes
      */
-    void apply(Box b, Vec pboxes, Vec ptemplates, JS.Callable callback, int numerator, int denominator) {
+    void apply(Box b, Vec pboxes, Vec ptemplates, JS.Callable callback, int numerator, int denominator, Res resourceRoot) {
 
         int original_numerator = numerator;
 
@@ -192,13 +192,13 @@ public class Template {
 
         for(int i=0; _preapply != null && i<_preapply.length; i++)
             if (_preapply[i] != null) {
-                _preapply[i].apply(b, null, null, callback, numerator, denominator);
+                _preapply[i].apply(b, null, null, callback, numerator, denominator, resourceRoot);
                 numerator += _preapply[i].numUnits();
             }
 
         for (int i=0; children != null && i<children.length; i++) {
             Box newkid = new Box();
-            children[i].apply(newkid, pboxes, ptemplates, callback, numerator, denominator);
+            children[i].apply(newkid, pboxes, ptemplates, callback, numerator, denominator, resourceRoot);
             b.put(Integer.MAX_VALUE, newkid);
             numerator += children[i].numUnits();
         }
@@ -208,7 +208,7 @@ public class Template {
         if (redirect != null && !"self".equals(redirect)) redir = (Box)b.get("$" + redirect);
 
         if (script != null) try {
-            script.call(new JS.Array(), b);
+            script.call(new JS.Array(), new PerInstantiationScope(b, resourceRoot));
         } catch (JS.Exn e) {
             if (Log.on) Log.log(this, "WARNING: uncaught ecmascript exception: " + e.getMessage());
         }
@@ -233,7 +233,7 @@ public class Template {
 
         for(int i=0; _postapply != null && i<_postapply.length; i++)
             if (_postapply[i] != null) {
-                _postapply[i].apply(b, null, null, callback, numerator, denominator);
+                _postapply[i].apply(b, null, null, callback, numerator, denominator, resourceRoot);
                 numerator += _postapply[i].numUnits();
             }
 
@@ -269,99 +269,6 @@ public class Template {
         for(int i=0; _postapply != null && i<_postapply.length; i++) if (_postapply[i] != null) _postapply[i].gatherPreserves(v);
     }
 
-    /** adds a theme mapping, retemplatizing as needed */
-    public static void retheme(JS.Callable callback) {
-        /*
-        XWF.flushXWFs();
-
-        // clear changed marker and relink
-        Template[] t = new Template[cache.size()];
-        Enumeration e = cache.elements();
-        for(int i=0; e.hasMoreElements(); i++) t[i] = (Template)e.nextElement();
-        for(int i=0; i<t.length; i++) {
-            t[i].changed = false;
-            t[i].numunits = -1;
-            t[i].link(true);
-        }
-
-        for(int i=0; i<Surface.allSurfaces.size(); i++) {
-            Box b = ((Surface)Surface.allSurfaces.elementAt(i)).root;
-            if (b != null) reapply(b);
-        }
-
-        if (callback != null)
-            try {
-                JS.Array args = new JS.Array();
-                args.addElement(new Double(1.0));
-                args.addElement(new Double(1.0));
-                callback.call(args);
-            } catch (JS.Exn ex) {
-                if (Log.on) Log.log(Template.class, "WARNING: uncaught ecmascript exception: " + ex.getMessage());
-            }
-        */
-    }
-
-    /** template reapplication procedure */
-    private static void reapply(Box b) {
-
-        Log.log(Template.class, "Template.reapply() not implemented");
-        /*
-        // Ref 7.5.1: check if we need to retemplatize
-        boolean retemplatize = false;
-        if (b.templatename != null) {
-            Template t = getTemplate(b.templatename, b.importlist);
-            if (t != b.template) retemplatize = true;
-            b.template = t;
-        }
-        if (b.template != null && b.template.changed) retemplatize = true;
-
-        if (retemplatize) {
-
-            // Ref 7.5.2: "Preserve all properties on the box mentioned in the <preserve> elements of any
-            //             of the templates which would be applied in step 7."
-            Vec keys = new Vec();
-            b.template.gatherPreserves(keys);
-            Object[] vals = new Object[keys.size()];
-            for(int i=0; i<keys.size(); i++) vals[i] = b.get(((String)keys.elementAt(i)), null);
-            
-            // Ref 7.5.3: "Remove and save all children of the box, or its redirect target, if it has one"
-            Box[] kids = null;
-            if (b.redirect != null) {
-                kids = new Box[b.redirect.numChildren()];
-                for(int i=b.redirect.numChildren() - 1; i >= 0; i--) {
-                    kids[i] = b.redirect.getChild(i);
-                    kids[i].remove();
-                }
-            }
-            
-            // Ref 7.5.4: "Set the box's redirect target to self"
-            b.redirect = b;
-            
-            // Ref 7.5.5: "Remove all of the box's immediate children"
-            for(Box cur = b.getChild(b.numChildren() - 1); cur != null;) {
-                Box oldcur = cur;
-                cur = cur.prevSibling();
-                oldcur.remove();
-            }
-            
-            // Ref 7.5.6: "Remove all traps set by scripts run during the application of any template to this box"
-            Trap.removeAllTrapsByBox(b);
-            
-            // Ref 7.5.7: "Apply the template to the box according to the usual application procedure"
-            b.template.apply(b, null, null, null, 0, 1);
-            
-            // Ref 7.5.8: "Re-add the saved children which were removed in step 3"
-            for(int i=0; kids != null && i<kids.length; i++) b.put(Integer.MAX_VALUE, null, kids[i]);
-            
-            // Ref 7.5.9: "Re-put any property values which were preserved in step 2"
-            for(int i=0; i<keys.size(); i++) b.put((String)keys.elementAt(i), null, vals[i]);
-        }        
-
-        // Recurse
-        for(Box j = b.getChild(0); j != null; j = j.nextSibling()) reapply(j);
-        */
-    }
-
     /** runs statics, resolves string references to other templates into actual Template instance references, and sets <tt>change</tt> as needed */
     void link() { link(false); }
 
@@ -701,6 +608,35 @@ public class Template {
         }
     }
 
+    private static class PerInstantiationScope extends JS.Scope {
+        Res resourceRoot = null;
+        public PerInstantiationScope(Scope parentScope, Res resourceRoot) {
+            super(parentScope);
+            this.resourceRoot = resourceRoot;
+        }
+        public boolean isTransparent() { return true; }
+        public boolean has(Object key) { return false; }
+        public void declare(String s) { super.declare(s); }
+        public Object get(Object key) {
+            // FIXME: access statics here
+            if (Box.SpecialBoxProperty.specialBoxProperties.get(key) == null &&
+                !super.has(key)) {
+                Object ret = resourceRoot.get(key);
+                if (ret != null) return ret;
+                throw new JS.Exn("must declare " + key + " before using it!");
+            }
+            return super.get(key);
+        }
+        public void put(Object key, Object val) {
+            // FIXME: access statics here
+            if (Box.SpecialBoxProperty.specialBoxProperties.get(key) == null &&
+                !super.has(key)) {
+                throw new JS.Exn("must declare " + key + " before using it!");
+            }
+            super.put(key, val);
+        }
+    }
+
 }