2003/10/31 09:50:08
[org.ibex.core.git] / src / org / xwt / XWT.java
index d276f05..c5f7a13 100644 (file)
@@ -25,6 +25,7 @@ public final class XWT extends JS.Obj {
     public Object get(Object name) {
         if (name.equals("alt")) return Surface.alt ? Boolean.TRUE : Boolean.FALSE;
         else if (name.equals("rr")) return rr;
+        else if (name.equals("box")) return new Box();
         else if (name.equals("control")) return Surface.control ? Boolean.TRUE : Boolean.FALSE;
         else if (name.equals("shift")) return Surface.shift ? Boolean.TRUE : Boolean.FALSE;
         else if (name.equals("clipboard")) return Platform.getClipBoard();
@@ -46,12 +47,18 @@ public final class XWT extends JS.Obj {
             else if (!Surface.button1 && !Surface.button2 && Surface.button3) return new Integer(1);
             else return new Integer(0);
         }
-        else return super.get(name);
+        else return rr.get(name);
     }
 
-    public void put(Object name, Object value) {
-        if (name.equals("thread") && value != null && value instanceof JS.Callable) ThreadMessage.newthread((JS.Callable)value);
-        else if (name.equals("clipboard")) Platform.setClipBoard(value.toString());
+    public void put(Object name, final Object value) {
+        if (name.equals("thread") && value != null && (value instanceof JS.Callable || value instanceof JS.CompiledFunction)) {
+            Scheduler.add(new Scheduler.Task() { public Object call(Object arg) {
+                new JS.Thread((CompiledFunction)value).resume();
+                return null;
+            } });
+        } else if (name.equals("clipboard")) Platform.setClipBoard(value.toString());
+        else if (name.equals("frame")) Platform.createSurface((Box)value, true, true);
+        else if (name.equals("window")) Platform.createSurface((Box)value, false, true);
         else if (name.equals("proxyAuthorization")) {
             HTTP.Proxy.Authorization.authorization = value.toString();
             HTTP.Proxy.Authorization.waitingForUser.release();
@@ -67,8 +74,26 @@ public final class XWT extends JS.Obj {
             return null;
 
         } else if (method.equals("clone")) {
+            if (checkOnly) return Boolean.TRUE;
             return new XWT((Res)args.elementAt(0));
 
+        } else if (method.equals("graft")) {
+            if (checkOnly) return Boolean.TRUE;
+            if (args.elementAt(0) instanceof Box) throw new JS.Exn("can't graft onto Boxes (yet)");
+            if (args.elementAt(0) instanceof Number) throw new JS.Exn("can't graft onto Numbers (yet)");
+            if (args.elementAt(0) instanceof String) throw new JS.Exn("can't graft onto Strings (yet)");
+            if (args.elementAt(0) instanceof Res)
+                return new Res.Graft((Res)args.elementAt(0), args.elementAt(1), args.elementAt(2));
+            return new JS.Graft((JS)args.elementAt(0), args.elementAt(1), args.elementAt(2));
+
+        } else if (method.equals("unzip")) {
+            if (checkOnly) return Boolean.TRUE;
+            return new Res.Zip((Res)args.elementAt(0));
+
+        } else if (method.equals("watchProgress")) {
+            if (checkOnly) return Boolean.TRUE;
+            return new Res.ProgressWatcher((Res)args.elementAt(0), (JS.CompiledFunction)args.elementAt(1));
+
         } else if (method.equals("yield")) {
             if (checkOnly) return Boolean.TRUE;
             sleep(0);
@@ -93,6 +118,12 @@ public final class XWT extends JS.Obj {
             if (checkOnly) return Boolean.TRUE;
             return new Regexp(args);
 
+        } else if (method.equals("apply")) {
+            if (checkOnly) return Boolean.TRUE;
+            Box b = (Box)args.elementAt(0);
+            Template.getTemplate((Res)args.elementAt(1)).apply(b, null, this);
+            return b;
+
         } else if (method.equals("xmlrpc")) {
             if (checkOnly) return Boolean.TRUE;
             if (args.length() != 1 || args.elementAt(0) == null) return null;
@@ -107,10 +138,6 @@ public final class XWT extends JS.Obj {
                 return new SOAP(args.elementAt(0).toString(), "", args.elementAt(1).toString(), args.elementAt(2).toString());
             else return null;
 
-        } else if (method.equals("newBox")) {
-            if (checkOnly) return Boolean.TRUE;
-            return new Box();
-
         } else if (method.equals("sleep")) {
             if (checkOnly) return Boolean.TRUE;
             if (args != null && (args.length() != 1 || args.elementAt(0) == null)) return null;
@@ -122,49 +149,37 @@ public final class XWT extends JS.Obj {
             if (checkOnly) return Boolean.TRUE;
             if (args.length() != 1) return null;
             String file = Platform.fileDialog(args.elementAt(0).toString(), false);
-            return file == null ? null : Res.stringToRes("file:" + file);
+            return file == null ? null : new Res.File(file);
 
-        } else if (method.equals("saveFile")) {
+        } else if (method.equals("saveFile") || method.equals("saveFileAs")) {
             if (checkOnly) return Boolean.TRUE;
-            // FIXME
-            /*
             if (args.length() != 2) return null;
-            if (!(args.elementAt(1) instanceof ByteStream)) return null;
+            if (!(args.elementAt(1) instanceof Res)) return null;
             String file = args.elementAt(0).toString();
-            if (safeFiles.get(Platform.isCaseSensitive() ? file : file.toLowerCase()) == null) {
+            if (method.equals("saveFileAs") ||
+                safeFiles.get(Platform.isCaseSensitive() ? file : file.toLowerCase()) == null) {
                 file = Platform.fileDialog(file, true);
+                // FIXME: throw exception here
                 if (file == null) return null;
                 safeFiles.put(Platform.isCaseSensitive() ? file : file.toLowerCase(), new Object());
             }
             try {
-                ((ByteStream)args.elementAt(1)).writeTo(new FileOutputStream(file));
-                return null;
-            } catch (IOException e) {
-                if (Log.on) Log.log(ByteStream.class, "IO Exception while writing a ByteStream to a file");
-                if (Log.on) Log.log(ByteStream.class, e);
-                throw new JS.Exn("error while writing a ByteStream to a file");
-            }
-            */
-
-        } else if (method.equals("saveFileAs")) {
-            // FIXME
-            /*
-            if (checkOnly) return Boolean.TRUE;
-            if (args.length() != 2) return null;
-            if (!(args.elementAt(1) instanceof ByteStream)) return null;
-            String file = args.elementAt(0).toString();
-            file = Platform.fileDialog(file, true);
-            if (file == null) return null;
-            safeFiles.put(Platform.isCaseSensitive() ? file : file.toLowerCase(), new Object());
-            try {
-                ((ByteStream)args.elementAt(1)).writeTo(new FileOutputStream(file));
+                InputStream is = ((Res)args.elementAt(1)).getInputStream();
+                FileOutputStream out = new FileOutputStream(file);
+                byte[] buffer = new byte[1024 * 16];
+                while(true) {
+                    int numread = is.read(buffer, 0, buffer.length);
+                    if (numread == -1) break;
+                    out.write(buffer, 0, numread);
+                }
+                is.close();
+                out.close();
                 return null;
             } catch (IOException e) {
-                if (Log.on) Log.log(ByteStream.class, "IO Exception while writing a ByteStream to a file");
-                if (Log.on) Log.log(ByteStream.class, e);
-                throw new JS.Exn("error while writing a ByteStream to a file");
+                if (Log.on) Log.log(XWT.class, "IO Exception while writing a ByteStream to a file");
+                if (Log.on) Log.log(XWT.class, e);
+                throw new JS.Exn("error while writing a Resource to a file");
             }
-            */
 
         } else if (method.equals("parseHTML")) {
             if (checkOnly) return Boolean.TRUE;
@@ -220,20 +235,22 @@ public final class XWT extends JS.Obj {
         }
     }
 
-    public static void sleep(int i) {
-        java.lang.Thread thread = java.lang.Thread.currentThread();
-        if (!(thread instanceof ThreadMessage)) {
-            if (Log.on) Log.log(XWT.class, "cannot sleep() or yield() in the foreground thread");
-        } else {
-            ThreadMessage mythread = (ThreadMessage)thread;
-            mythread.done.release();
-            if (i > 0) try { java.lang.Thread.sleep(i); } catch (Exception e) { }
-            Message.Q.add(mythread);
-            mythread.go.block();
-        }
+    public static void sleep(final int i) {
+        final JS.Thread jsthread = JS.Thread.current();
+        final long currentTime = System.currentTimeMillis();
+        final Scheduler.Task task = new Scheduler.Task() { public Object call(Object arg) {
+            if (System.currentTimeMillis() - currentTime < i) {
+                Scheduler.add(this);
+            } else {
+                jsthread.resume();
+            }
+            return null;
+        } };
+        jsthread.pause();
+        Scheduler.add(task);
     }
     
-    private static class XWTMath extends JS.Obj {
+    private static class XWTMath extends org.xwt.js.Math {
         public XWTMath() {
             JS gs = new JS.GlobalScope();
             put("isNaN",gs.get("isNaN"));