- // Theming Logic ////////////////////////////////////////////////////////////
-
- /** helper method to recursively gather up the list of keys to be preserved */
- private void gatherPreserves(Vec v) {
- for(int i=0; preserve != null && i<preserve.length; i++) v.addElement(preserve[i]);
- for(int i=0; _preapply != null && i<_preapply.length; i++) if (_preapply[i] != null) _preapply[i].gatherPreserves(v);
- 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(Function 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 {
- callback.call(Context.enter(), null, null, new Object[] { new Double(1.0), new Double(1.0) });
- } catch (EcmaError ex) {
- if (Log.on) Log.log(Template.class, "WARNING: uncaught interpreter exception: " + ex.getMessage());
- if (Log.on) Log.log(Template.class, " thrown from within progress callback at " + ex.getSourceName() + ":" + ex.getLineNumber());
- } catch (JavaScriptException ex) {
- if (Log.on) Log.log(Template.class, "WARNING: uncaught ecmascript exception: " + ex.getMessage());
- if (Log.on) Log.log(Template.class, " thrown from within progress callback at " + ex.sourceFile + ":" + ex.line);
- }
- }
-
- /** template reapplication procedure */
- private static void reapply(Box b) {
-
- // 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); }
-
- /** same as link(), except that with a true value, it will force a re-link */
- private void link(boolean force) {
-
- if (staticscript != null) try {
- Scriptable s = Static.createStatic(nodeName, false);
- if (staticscript != null) {
- Script temp = staticscript;
- ((InterpretedScript)temp).setParentScope(s); // so we know how to handle Static.get("xwt")
- staticscript = null;
- temp.exec(Context.enter(), s);
- }
- } catch (EcmaError e) {
- if (Log.on) Log.log(this, "WARNING: uncaught interpreter exception: " + e.getMessage());
- if (Log.on) Log.log(this, " thrown while executing <static/> block for " + nodeName +
- " at " + e.getSourceName() + ":" + e.getLineNumber());
- } catch (JavaScriptException e) {
- if (Log.on) Log.log(this, "WARNING: uncaught ecmascript exception: " + e.getMessage());
- if (Log.on) Log.log(this, " thrown while executing <static/> block for " + nodeName + " at " + e.sourceFile + ":" + e.line);
- }
-
- if (!(force || (preapply != null && _preapply == null) || (postapply != null && _postapply == null))) return;
-
- if (preapply != null) {
- if (_preapply == null) _preapply = new Template[preapply.length];
- for(int i=0; i<_preapply.length; i++) {
- Template t = getTemplate(preapply[i], importlist);
- if (t != _preapply[i]) changed = true;
- _preapply[i] = t;
- }
- }
- if (postapply != null) {
- if (_postapply == null) _postapply = new Template[postapply.length];
- for(int i=0; i<_postapply.length; i++) {
- Template t = getTemplate(postapply[i], importlist);
- if (t != _postapply[i]) changed = true;
- _postapply[i] = t;
- }
- }
-
- for(int i=0; children != null && i<children.length; i++) children[i].link(force);
- }
-