2003/10/31 09:50:08
[org.ibex.core.git] / src / org / xwt / XWT.java
index 278532a..c5f7a13 100644 (file)
@@ -50,9 +50,15 @@ public final class XWT extends JS.Obj {
         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();
@@ -80,9 +86,13 @@ public final class XWT extends JS.Obj {
                 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.Callable)args.elementAt(1));
+            return new Res.ProgressWatcher((Res)args.elementAt(0), (JS.CompiledFunction)args.elementAt(1));
 
         } else if (method.equals("yield")) {
             if (checkOnly) return Boolean.TRUE;
@@ -108,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;
@@ -133,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;
@@ -231,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"));