From f8d15feced5c799c79a88b865ca1a85967c203ad Mon Sep 17 00:00:00 2001 From: megacz Date: Fri, 30 Jan 2004 06:47:41 +0000 Subject: [PATCH] 2002/05/28 21:30:03 darcs-hash:20040130064741-2ba56-7aa1e5dcb64a9ac3e476088beb29111155ca6f65.gz --- src/org/xwt/Main.java | 6 +++++- src/org/xwt/Platform.java | 20 ++++---------------- src/org/xwt/SOAP.java | 4 ++-- src/org/xwt/ThreadMessage.java | 24 ++++++++++++++++++++++++ src/org/xwt/XMLRPC.java | 21 ++++----------------- src/org/xwt/XWT.java | 9 ++++----- src/org/xwt/plat/AWT.java | 15 +++++++-------- src/org/xwt/plat/Win32.cc | 1 - 8 files changed, 50 insertions(+), 50 deletions(-) diff --git a/src/org/xwt/Main.java b/src/org/xwt/Main.java index 61c4615..f96bdf1 100644 --- a/src/org/xwt/Main.java +++ b/src/org/xwt/Main.java @@ -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)); } diff --git a/src/org/xwt/Platform.java b/src/org/xwt/Platform.java index 8efe59a..f773f71 100644 --- a/src/org/xwt/Platform.java +++ b/src/org/xwt/Platform.java @@ -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(); } } diff --git a/src/org/xwt/SOAP.java b/src/org/xwt/SOAP.java index 4ea2d40..d2d4fec 100644 --- a/src/org/xwt/SOAP.java +++ b/src/org/xwt/SOAP.java @@ -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) { diff --git a/src/org/xwt/ThreadMessage.java b/src/org/xwt/ThreadMessage.java index 558c49d..cd22019 100644 --- a/src/org/xwt/ThreadMessage.java +++ b/src/org/xwt/ThreadMessage.java @@ -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 { diff --git a/src/org/xwt/XMLRPC.java b/src/org/xwt/XMLRPC.java index efe4959..4616764 100644 --- a/src/org/xwt/XMLRPC.java +++ b/src/org/xwt/XMLRPC.java @@ -186,9 +186,9 @@ class XMLRPC extends XML implements Function { try { sb.append(" "); 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(); } } diff --git a/src/org/xwt/XWT.java b/src/org/xwt/XWT.java index 7c37978..9bb02e1 100644 --- a/src/org/xwt/XWT.java +++ b/src/org/xwt/XWT.java @@ -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); diff --git a/src/org/xwt/plat/AWT.java b/src/org/xwt/plat/AWT.java index 43168fd..7f159cf 100644 --- a/src/org/xwt/plat/AWT.java +++ b/src/org/xwt/plat/AWT.java @@ -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 ///////////////////////////////////////////////////////////////////////////////////// diff --git a/src/org/xwt/plat/Win32.cc b/src/org/xwt/plat/Win32.cc index e03526c..d2f699b 100644 --- a/src/org/xwt/plat/Win32.cc +++ b/src/org/xwt/plat/Win32.cc @@ -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); -- 1.7.10.4