2002/05/28 21:30:03
authormegacz <megacz@xwt.org>
Fri, 30 Jan 2004 06:47:41 +0000 (06:47 +0000)
committermegacz <megacz@xwt.org>
Fri, 30 Jan 2004 06:47:41 +0000 (06:47 +0000)
darcs-hash:20040130064741-2ba56-7aa1e5dcb64a9ac3e476088beb29111155ca6f65.gz

src/org/xwt/Main.java
src/org/xwt/Platform.java
src/org/xwt/SOAP.java
src/org/xwt/ThreadMessage.java
src/org/xwt/XMLRPC.java
src/org/xwt/XWT.java
src/org/xwt/plat/AWT.java
src/org/xwt/plat/Win32.cc

index 61c4615..f96bdf1 100644 (file)
@@ -19,6 +19,9 @@ public class Main extends Applet {
     /** scripts are allowed to make XMLRPC/SOAP calls to their origin address, even if it is firewalled */
     public static java.net.InetAddress originAddr = null;
 
+    /** the URL where the initial xwar came from */
+    public static String origin = null;
+
     /** see Template.numUnits() for a description */
     public static int instantiatedUnits = 0;
 
@@ -89,6 +92,7 @@ public class Main extends Applet {
             if (args.length > startargs) {
                 if (args[startargs].startsWith("http://")) {
                     if (Log.on) Log.log(Main.class, "downloading xwar");
+                    origin = args[startargs];
                     URL u = new URL(args[startargs]);
                     try {
                         originAddr = InetAddress.getByName(u.getHost());
@@ -99,13 +103,13 @@ public class Main extends Applet {
                     Resources.loadArchive(new HTTP(args[startargs]).getInputStream());
                     
                 } else {
-
                     if (Log.on) Log.log(Main.class, "loading xwar from local filesystem");
                     // HACK because MSIE turns \'s into /'s in URLs... argh!!
                     if (Platform.platform.getClass().getName().endsWith("Win32"))
                         args[startargs] = args[startargs].replace('/', '\\');
 
                     File f = new File(args[startargs]);
+                    origin = "file://" + f.getAbsolutePath();
                     if (f.isDirectory()) Resources.loadDirectory(f);
                     else Resources.loadArchive(new FileInputStream(f));
                 }
index 8efe59a..f773f71 100644 (file)
@@ -178,7 +178,7 @@ public class Platform {
     /** Returns null if XWT should always use direct connection; otherwise returns a ProxyInfo object with proxy settings */
     protected synchronized HTTP.ProxyInfo _detectProxy() { return null; }
 
-    /** displays a platform-specific "open file" dialog and returns the chosen filename */
+    /** displays a platform-specific "open file" dialog and returns the chosen filename, or null if the user hit cancel */
     protected String _fileDialog(String suggestedFileName, boolean write) { return null; }
 
     /** returns true iff the platform has a case-sensitive filesystem */
@@ -249,25 +249,13 @@ public class Platform {
     /** returns true iff the platform has a case-sensitive filesystem */
     public static boolean isCaseSensitive() { return platform._isCaseSensitive(); }
 
-    /** displays a platform-specific "open file" dialog and returns the chosen filename */
+    /** displays a platform-specific "open file" dialog and returns the chosen filename, or null if the user hit cancel */
     public static String fileDialog(String suggestedFileName, boolean write) {
-        // put ourselves in the background
-        Thread thread = Thread.currentThread();
-        if (!(thread instanceof ThreadMessage)) {
-            if (Log.on) Log.log(Platform.class, "xwt.openFile may only be called from background threads");
-            return null;
-        }
-        ThreadMessage mythread = (ThreadMessage)thread;
-        mythread.setPriority(Thread.MIN_PRIORITY);
-        mythread.done.release();
-
+        if (!ThreadMessage.suspendThread()) return null;
         try {
             return platform._fileDialog(suggestedFileName, write);
         } finally {
-            // okay, let ourselves be brought to the foreground
-            MessageQueue.add(mythread);
-            mythread.setPriority(Thread.NORM_PRIORITY);
-            mythread.go.block();
+            ThreadMessage.resumeThread();
         }
     }
 
index 4ea2d40..d2d4fec 100644 (file)
@@ -180,9 +180,9 @@ class SOAP extends XMLRPC {
             try {
                 sb.append("                <" + name + " xsi:type=\"SOAP-ENC:base64\">");
                 InputStream is = ((ByteStream)o).getInputStream();
-                byte[] buf = new byte[96];
+                byte[] buf = new byte[54];
                 while(true) {
-                    int numread = is.read(buf, 0, 96);
+                    int numread = is.read(buf, 0, 54);
                     if (numread == -1) break;
                     byte[] writebuf = buf;
                     if (numread < buf.length) {
index 558c49d..cd22019 100644 (file)
@@ -50,6 +50,30 @@ public class ThreadMessage extends Thread implements Message {
         ret.f = f;
         MessageQueue.add(ret);
     }
+
+    /** attempts to put this thread into the background to perform a blocking operation; returns false if unable to do so */
+    public static boolean suspendThread() {
+        // put ourselves in the background
+        Thread thread = Thread.currentThread();
+        if (!(thread instanceof ThreadMessage)) {
+            Context cx = Context.enter();
+            if (Log.on) Log.log(ThreadMessage.class, "attempt to perform background-only operation in a foreground thread at " +
+                                cx.interpreterSourceFile + ":" + cx.interpreterLine);
+            return false;
+        }
+        ThreadMessage mythread = (ThreadMessage)thread;
+        mythread.setPriority(Thread.MIN_PRIORITY);
+        mythread.done.release();
+        return true;
+    }
+
+    /** re-enqueues this thread */
+    public static void resumeThread() {
+        ThreadMessage mythread = (ThreadMessage)Thread.currentThread();
+        MessageQueue.add(mythread);
+        mythread.setPriority(Thread.NORM_PRIORITY);
+        mythread.go.block();        
+    }
     
     public void run() {
         try {
index efe4959..4616764 100644 (file)
@@ -186,9 +186,9 @@ class XMLRPC extends XML implements Function {
             try {
                 sb.append("                <value><base64>");
                 InputStream is = ((ByteStream)o).getInputStream();
-                byte[] buf = new byte[96];
+                byte[] buf = new byte[54];
                 while(true) {
-                    int numread = is.read(buf, 0, 96);
+                    int numread = is.read(buf, 0, 54);
                     if (numread == -1) break;
                     byte[] writebuf = buf;
                     if (numread < buf.length) {
@@ -344,19 +344,10 @@ class XMLRPC extends XML implements Function {
 
     public final Object call(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args) throws JavaScriptException {
 
-        // put ourselves in the background
-        Thread thread = Thread.currentThread();
-        if (!(thread instanceof ThreadMessage)) {
-            if (Log.on) Log.log(this, "RPC calls may only be made from background threads");
-            return null;
-        }
-        ThreadMessage mythread = (ThreadMessage)thread;
-        mythread.setPriority(Thread.MIN_PRIORITY);
-        mythread.done.release();
+        if (!ThreadMessage.suspendThread()) return null;
 
         try {
             return call(args);
-
         } catch (IOException se) {
             if (Log.on) Log.log(this, se);
             if (Log.on) Log.log(this, " at " + cx.interpreterSourceFile + ":" + cx.interpreterLine);
@@ -369,12 +360,8 @@ class XMLRPC extends XML implements Function {
                 if (Log.on) Log.log(this, " at " + cx.interpreterSourceFile + ":" + cx.interpreterLine);
             }
             throw jse;
-
         } finally {
-            // okay, let ourselves be brought to the foreground
-            MessageQueue.add(mythread);
-            mythread.setPriority(Thread.NORM_PRIORITY);
-            mythread.go.block();
+            ThreadMessage.resumeThread();
         }
 
     }
index 7c37978..9bb02e1 100644 (file)
@@ -38,6 +38,7 @@ public final class XWT extends JSObject {
         else if (name.equals("newBox")) return newBox;
         else if (name.equals("soap")) return soap;
         else if (name.equals("xmlrpc")) return xmlrpc;
+        else if (name.equals("origin")) return Main.origin;
         else if (name.equals("clipboard")) return Platform.getClipBoard();
         else if (name.equals("altKeyName")) return Platform.altKeyName();
         else if (name.equals("screenWidth")) return new Integer(Platform.getScreenWidth());
@@ -219,7 +220,7 @@ public final class XWT extends JSObject {
 
     private static final JSFunction openFile = new JSFunction() {
             public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
-                if (args == null || args.length != 1) return null;
+                if (args.length != 1) return null;
                 String file = Platform.fileDialog(args[0].toString(), false);
                 return file == null ? null : new ByteStream(file);
             }
@@ -227,17 +228,15 @@ public final class XWT extends JSObject {
 
     private static final JSFunction saveFile = new JSFunction() {
             public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
-                if (args == null || args.length != 2) return null;
+                if (args.length != 2) return null;
                 if (!(args[1] instanceof ByteStream)) return null;
                 String file = args[0].toString();
                 if (safeFiles.get(Platform.isCaseSensitive() ? file : file.toLowerCase()) == null) {
                     file = Platform.fileDialog(file, true);
                     if (file == null) return null;
-                    System.out.println(">>" + file + "<<");
                     safeFiles.put(Platform.isCaseSensitive() ? file : file.toLowerCase(), new Object());
                 }
                 try {
-                    System.out.println("WRITING TO " + file);
                     ((ByteStream)args[1]).writeTo(new FileOutputStream(file));
                     return null;
                 } catch (IOException e) {
@@ -250,7 +249,7 @@ public final class XWT extends JSObject {
 
     private static final JSFunction saveFileAs = new JSFunction() {
             public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
-                if (args == null || args.length != 2) return null;
+                if (args.length != 2) return null;
                 if (!(args[1] instanceof ByteStream)) return null;
                 String file = args[0].toString();
                 file = Platform.fileDialog(file, true);
index 43168fd..7f159cf 100644 (file)
@@ -49,11 +49,14 @@ public class AWT extends Platform {
         return 0;
     }
 
-    static class Open extends FileDialog implements WindowListener, ComponentListener {
+    static class FileDialogHelper extends FileDialog implements WindowListener, ComponentListener {
         Semaphore s;
-        public Open(String suggestedFileName, Semaphore s, boolean write) {
+        public FileDialogHelper(String suggestedFileName, Semaphore s, boolean write) {
             super(new Frame(), suggestedFileName, write ? FileDialog.SAVE : FileDialog.LOAD);
             this.s = s;
+            addWindowListener(this);
+            addComponentListener(this);
+            show();
         }
         public void windowActivated(WindowEvent e) { }
         public void windowClosed(WindowEvent e) { s.release(); }
@@ -69,16 +72,12 @@ public class AWT extends Platform {
     };
 
     protected String _fileDialog(String suggestedFileName, boolean write) {
-        Semaphore s = new Semaphore();
-        Open fd = new Open(suggestedFileName, s, write);
-        fd.addWindowListener(fd);
-        fd.addComponentListener(fd);
-        fd.show();
+        final Semaphore s = new Semaphore();
+        FileDialogHelper fd = new FileDialogHelper(suggestedFileName, s, write);
         s.block();
         return fd.getDirectory() + File.separatorChar + fd.getFile();
     }
 
-    protected void _saveFile(String suggestedFileName, ByteStream data) throws IOException { }
 
     // Inner Classes /////////////////////////////////////////////////////////////////////////////////////
 
index e03526c..d2f699b 100644 (file)
@@ -255,7 +255,6 @@ jstring org::xwt::plat::Win32::_fileDialog(jstring suggestedFileName, jboolean w
     ofn.nMaxFile = 1024;
 
     if (write) ofn.Flags |= OFN_OVERWRITEPROMPT;
-    ofn.Flags |= OFN_NOCHANGEDIR;
     ofn.Flags |= OFN_HIDEREADONLY;
 
     int ret = write ? GetSaveFileName(&ofn) : GetOpenFileName(&ofn);