2003/12/13 08:13:32
[org.ibex.core.git] / src / org / xwt / XWT.java
index 5fa9a8d..c5f56c3 100644 (file)
@@ -1,4 +1,3 @@
-// FIXME
 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
 package org.xwt;
 
@@ -17,13 +16,20 @@ public final class XWT extends JS {
     public final Res rr;
     public XWT(Res rr) { this.rr = rr; }
 
+    private Cache subCache = new Cache(20);
+    private Sub getSub(String s) {
+        Sub ret = (Sub)subCache.get(s);
+        if (ret == null) subCache.put(s, ret = new Sub(s));
+        return ret;
+    }
+
     /** lets us put multi-level get/put/call keys all in the same method */
     private class Sub extends JS {
         String key;
         Sub(String key) { this.key = key; }
         public String toString() { return "XWTSUB " + key; }
         public void put(Object key, Object val) { XWT.this.put(this.key + "." + key, val); }
-        public Object get(Object key) { return XWT.this.get(this.key + "." + key); }
+        public Object get(Object key) throws JSExn { return XWT.this.get(this.key + "." + key); }
         public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
             return XWT.this.callMethod(this.key, a0, a1, a2, rest, nargs);
         }
@@ -32,42 +38,43 @@ public final class XWT extends JS {
         }
     }
 
-    public Object get(Object name) {
+    public Object get(Object name) throws JSExn {
         //#switch(name)
+        case "_": return rr;
         case "math": return xwtMath;
         case "string": return xwtString;
         case "date": return METHOD;
         case "origin": return Main.origin;
         case "box": return new Box();
-        case "log": return new Sub("log");
-        case "ui": return new Sub("ui");
-        case "thread": return new Sub("thread");
-        case "font": return new Sub("font");
+        case "log": return getSub("log");
+        case "ui": return getSub("ui");
+        case "thread": return getSub("thread");
+        case "font": return getSub("font");
         case "font.sansserif": return Main.builtin.get("fonts/vera/Vera.ttf");
         case "font.monospace": return Main.builtin.get("fonts/vera/VeraMono.ttf");
         case "font.serif": return Main.builtin.get("fonts/vera/VeraSe.ttf");
-        case "ui.key": return new Sub("ui.key");
-        case "ui.key.alt": return Surface.alt ? Boolean.TRUE : Boolean.FALSE;
-        case "ui.key.control": return Surface.control ? Boolean.TRUE : Boolean.FALSE;
-        case "ui.key.shift": return Surface.shift ? Boolean.TRUE : Boolean.FALSE;
+        case "ui.key": return getSub("ui.key");
+        case "ui.key.alt": return Surface.alt ? T : F;
+        case "ui.key.control": return Surface.control ? T : F;
+        case "ui.key.shift": return Surface.shift ? T : F;
         case "ui.clipboard": return Platform.getClipBoard();
         case "ui.maxdim": return new Integer(Short.MAX_VALUE);
-        case "ui.key.name": return new Sub("ui.key.name");
+        case "ui.key.name": return getSub("ui.key.name");
         case "ui.key.name.alt": return Platform.altKeyName();
-        case "ui.screen": return new Sub("ui.screen");
+        case "ui.screen": return getSub("ui.screen");
         case "ui.screen.width": return new Integer(Platform.getScreenWidth());
         case "ui.screen.height": return new Integer(Platform.getScreenHeight());
         case "fs.home": return System.getProperty("user.home");
         case "fs.temp": return System.getProperty("java.io.tempdir");
-        case "ui.mouse": return new Sub("ui.mouse");
-        case "res": return new Sub("res");
+        case "ui.mouse": return getSub("ui.mouse");
+        case "res": return getSub("res");
         case "ui.mouse.button":
-            if (Surface.button1 && !Surface.button2 && !Surface.button3) return new Integer(1);
-            else if (!Surface.button1 && Surface.button2 && !Surface.button3) return new Integer(2);
-            else if (!Surface.button1 && !Surface.button2 && Surface.button3) return new Integer(3);
+            if (Surface.button1 && !Surface.button2 && !Surface.button3) return N(1);
+            else if (!Surface.button1 && Surface.button2 && !Surface.button3) return N(2);
+            else if (!Surface.button1 && !Surface.button2 && Surface.button3) return N(3);
             else return ZERO;
-        case "undocumented": return new Sub("undocumented");
-        case "undocumented.internal": return new Sub("undocumented.internal");
+        case "undocumented": return getSub("undocumented");
+        case "undocumented.internal": return getSub("undocumented.internal");
         case "thread.yield": return METHOD;
         case "thread.sleep": return METHOD;
         case "res.watch": return METHOD;
@@ -93,21 +100,12 @@ public final class XWT extends JS {
         case "stream.parse.xml": return METHOD;
         case "stream.parse.utf8": return METHOD;
         //#end
-        return rr.get(name);
+        return super.get(name);
     }
 
     public void put(Object name, final Object value) {
         //#switch(name)
-        case "thread":
-            Scheduler.add(new Scheduler.Task() {
-                    public void perform() throws JSExn {
-                        try {
-                            JS.invokePauseable((JSFunction)value);
-                        } catch (JS.PausedException pe) {
-                            // okay; wait for ourselves to be re-enqueued
-                        }
-                    }
-                });
+        case "thread": Scheduler.add((JSFunction)value);
         case "ui.clipboard": Platform.setClipBoard((String)value);
         case "ui.frame": Platform.createSurface((Box)value, true, true);
         case "ui.window": Platform.createSurface((Box)value, false, true);
@@ -124,12 +122,11 @@ public final class XWT extends JS {
             //#switch(name)
             case "res.watch": return new Res.ProgressWatcher((Res)a, (JSFunction)b);
             case "soap": /* return new SOAP((String)a, "", (String)b, null); */
-            case "apply":
-                Template.getTemplate((Res)b).apply((Box)a, XWT.this);
-                return a;
+            case "apply": Template.getTemplate((Res)b).apply((Box)a, XWT.this); return a;
             //#end
-        } else if (nargs == 3 && name.equals("soap")) {
+
             /*
+        } else if (nargs == 3 && name.equals("soap")) {
             if (name.equals("soap"))
                 return new SOAP((String)a, "", (String)b, (String)c);
             */
@@ -142,13 +139,7 @@ public final class XWT extends JS {
             case "res.uncab": return new Res.Cab((Res)a);
             case "res.cache": try { return new Res.CachedRes((Res)a, "resources", true); }
                               catch (Res.NotCacheableException e) { throw new JSExn("this resource cannot be cached"); }
-            case "res.url":
-                String url = (String)a;
-                if (url.startsWith("http://")) return new Res.HTTP(url);
-                else if (url.startsWith("https://")) return new Res.HTTP(url);
-                else if (url.startsWith("data:")) return new Res.ByteArray(Base64.decode(url.substring(5)), null);
-                else if (url.startsWith("utf8:")) return new Res.ByteArray(url.substring(5).getBytes(), null);
-                throw new JSExn("invalid resource specifier " + url);
+            case "res.url": return url2res((String)a);
             case "thread.sleep": sleep(JS.toInt(a)); return null;
             case "log.println": Log.logJS(this, a== null ? "**null**" : a.toString()); return null;
             case "log.dump": Log.recursiveLog("","",a); return null;
@@ -167,20 +158,22 @@ public final class XWT extends JS {
         return null;
     }
 
+    public Res url2res(String url) throws JSExn {
+        if (url.startsWith("http://")) return new Res.HTTP(url);
+        else if (url.startsWith("https://")) return new Res.HTTP(url);
+        else if (url.startsWith("data:")) return new Res.ByteArray(Base64.decode(url.substring(5)), null);
+        else if (url.startsWith("utf8:")) return new Res.ByteArray(url.substring(5).getBytes(), null);
+        else throw new JSExn("invalid resource specifier " + url);
+    }
+
     public static void sleep(final int i) throws JSExn {
         try {
             final JS.UnpauseCallback callback = JS.pause();
             final long currentTime = System.currentTimeMillis();
-            new Thread() {
-                public void run() {
-                    try { Thread.sleep(i); } catch (InterruptedException e) { }
-                    Scheduler.add(new Scheduler.Task() {
-                            public void perform() throws JSExn {
-                                callback.unpause(null);
-                            }
-                        });
-                }
-            }.start();
+            new Thread() { public void run() {
+                try { Thread.sleep(i); } catch (InterruptedException e) { }
+                Scheduler.add(callback);
+            } }.start();
         } catch (JS.NotPauseableException npe) {
             throw new JSExn("you cannot sleep or yield in the foreground thread");
         }