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); }
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("button")) {
if (Surface.button1 && !Surface.button2 && !Surface.button3) return new Integer(1);
else if (!Surface.button1 && Surface.button2 && !Surface.button3) return new Integer(1);
}
};
+ 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;
+ 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 == null || 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) {
+ 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 == null || 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());
+ }
+ };
+
}