2002/06/05 19:54:47
[org.ibex.core.git] / src / org / xwt / XWT.java
index e3368aa..2eeaadf 100644 (file)
@@ -12,6 +12,10 @@ import org.mozilla.javascript.*;
 public final class XWT extends JSObject {
 
     public static final XWT singleton = new XWT();
+
+    /** each key is a string representing a filename which the user has already given XWT permission to write to */
+    private static Hashtable safeFiles = new Hashtable();
+
     public String getClassName() { return "XWT"; }
     private XWT() { setSeal(true); }
 
@@ -28,15 +32,27 @@ public final class XWT extends JSObject {
         else if (name.equals("regexp")) return regexp;
         else if (name.equals("sleep")) return sleep;
         else if (name.equals("yield")) return yield;
+        else if (name.equals("newBrowserWindow")) return newBrowserWindow;
         else if (name.equals("textwidth")) return textwidth;
         else if (name.equals("textheight")) return textheight;
         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());
+        else if (name.equals("screenHeight")) return new Integer(Platform.getScreenHeight());
         else if (name.equals("static")) return Static.getStatic("");
         else if (name.equals("theme")) return theme;
+        else if (name.equals("openFile")) return openFile;
+        else if (name.equals("saveFile")) return saveFile;
+        else if (name.equals("saveFileAs")) return saveFileAs;
+        else if (name.equals("utfEncode")) return utfEncode;
+        else if (name.equals("fileSeparator")) return File.separator;
+        else if (name.equals("homeDir")) return System.getProperty("user.home");
+        else if (name.equals("tempDir")) return System.getProperty("java.io.tempdir");
+        else if (name.equals("recursivePrintObject")) return recursivePrintObject;
         else if (name.equals("button")) {
             if (Surface.button1 && !Surface.button2 && !Surface.button3) return new Integer(1);
             else if (!Surface.button1 && Surface.button2 && !Surface.button3) return new Integer(1);
@@ -64,6 +80,14 @@ public final class XWT extends JSObject {
         public Scriptable construct(Context cx, Scriptable scope, java.lang.Object[] args) { return null; }
     }
 
+    private static final JSFunction newBrowserWindow = new JSFunction() {
+            public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
+                if (args.length != 1 || args[0] == null) return null;
+                Platform.newBrowserWindow(args[0].toString());
+                return null;
+            }
+        };
+
     private static final JSFunction yield = new JSFunction() {
             public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
                 sleep.call(cx, null, null, null);
@@ -169,6 +193,8 @@ public final class XWT extends JSObject {
                     if (args[i] instanceof Scriptable && !(args[i] instanceof Box)) {
                         Scriptable s = (Scriptable)args[i];
                         Object[] keys = s.getIds();
+
+                        // FIXME: need to ensure that this is putGlobally(), but still run traps...
                         for(int j=0; j<keys.length; j++) ret.put(keys[j].toString(), null, s.get(keys[j].toString(), s));
                     }
                 return ret;
@@ -196,6 +222,96 @@ 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.length != 1) return null;
+                String file = Platform.fileDialog(args[0].toString(), false);
+                return file == null ? null : new ByteStream(file);
+            }
+        };
+
+    private static final JSFunction saveFile = new JSFunction() {
+            public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
+                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;
+                    safeFiles.put(Platform.isCaseSensitive() ? file : file.toLowerCase(), new Object());
+                }
+                try {
+                    ((ByteStream)args[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 JavaScriptException("error while writing a ByteStream to a file");
+                }
+            }
+        };
+
+    private static final JSFunction saveFileAs = new JSFunction() {
+            public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
+                if (args.length != 2) return null;
+                if (!(args[1] instanceof ByteStream)) return null;
+                String file = args[0].toString();
+                file = Platform.fileDialog(file, true);
+                if (file == null) return null;
+                safeFiles.put(Platform.isCaseSensitive() ? file : file.toLowerCase(), new Object());
+                try {
+                    ((ByteStream)args[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 JavaScriptException("error while writing a ByteStream to a file");
+                }
+            }
+        };
+
+    private static final JSFunction utfEncode = new JSFunction() {
+            public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
+                if (args == null || args.length != 1) return null;
+                return new ByteStream(args[0].toString().getBytes());
+            }
+        };
+
+    
+    private static void recurse(String indent, String name, Object o, Context cx) {
+        if (!name.equals("")) name += " : ";
+
+        if (o == null) {
+            Log.log(cx.interpreterSourceFile, indent + name + "<null>");
+
+        } else if (o instanceof NativeArray) {
+            Log.log(cx.interpreterSourceFile, indent + name + "<array>");
+            NativeArray na = (NativeArray)o;
+            for(int i=0; i<na.jsGet_length(); i++)
+                recurse(indent + "  ", i + "", na.get(i, null), cx);
+
+        } else if (o instanceof JSObject || o instanceof ScriptableObject) {
+            Log.log(cx.interpreterSourceFile, indent + name + "<object>");
+            Scriptable s = (Scriptable)o;
+            Object[] keys = s.getIds();
+            for(int i=0; i<keys.length; i++)
+                recurse(indent + "  ", keys[i].toString(),
+                        keys[i] instanceof Integer ? s.get(((Integer)keys[i]).intValue(), null) : s.get(keys[i].toString(), null), cx);
+
+        } else {
+            Log.log(cx.interpreterSourceFile, indent + name + o);
+
+        }
+    }
+
+    private static final JSFunction recursivePrintObject = new JSFunction() {
+            public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
+                if (args == null || args.length != 1) return null;
+                recurse("", "", args[0], cx);
+                return null;
+            }
+        };
+
 }