2002/05/16 04:00:52
[org.ibex.core.git] / src / org / xwt / XWT.java
1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
2 package org.xwt;
3
4 import java.io.*;
5 import java.net.*;
6 import java.text.*;
7 import java.util.*;
8 import org.xwt.util.*;
9 import org.mozilla.javascript.*;
10
11 /** Singleton class that provides all functionality in the xwt.* namespace */
12 public final class XWT extends JSObject {
13
14     public static final XWT singleton = new XWT();
15     public String getClassName() { return "XWT"; }
16     private XWT() { setSeal(true); }
17
18     public Object get(String name, Scriptable start) {
19         if (name == null) return null;
20         else if (name.equals("maxdim")) return new Integer(Short.MAX_VALUE);
21         else if (name.equals("parseFloat")) return JSObject.defaultObjects.get("parseFloat", null);
22         else if (name.equals("parseInt")) return JSObject.defaultObjects.get("parseInt", null);
23         else if (name.equals("alt")) return Surface.alt ? Boolean.TRUE : Boolean.FALSE;
24         else if (name.equals("control")) return Surface.control ? Boolean.TRUE : Boolean.FALSE;
25         else if (name.equals("shift")) return Surface.shift ? Boolean.TRUE : Boolean.FALSE;
26         else if (name.equals("date")) return date;
27         else if (name.equals("listfonts")) return listfonts;
28         else if (name.equals("regexp")) return regexp;
29         else if (name.equals("sleep")) return sleep;
30         else if (name.equals("yield")) return yield;
31         else if (name.equals("newBrowserWindow")) return newBrowserWindow;
32         else if (name.equals("textwidth")) return textwidth;
33         else if (name.equals("textheight")) return textheight;
34         else if (name.equals("newBox")) return newBox;
35         else if (name.equals("soap")) return soap;
36         else if (name.equals("xmlrpc")) return xmlrpc;
37         else if (name.equals("clipboard")) return Platform.getClipBoard();
38         else if (name.equals("altKeyName")) return Platform.altKeyName();
39         else if (name.equals("screenWidth")) return new Integer(Platform.getScreenWidth());
40         else if (name.equals("screenHeight")) return new Integer(Platform.getScreenHeight());
41         else if (name.equals("static")) return Static.getStatic("");
42         else if (name.equals("theme")) return theme;
43         else if (name.equals("button")) {
44             if (Surface.button1 && !Surface.button2 && !Surface.button3) return new Integer(1);
45             else if (!Surface.button1 && Surface.button2 && !Surface.button3) return new Integer(1);
46             else if (!Surface.button1 && !Surface.button2 && Surface.button3) return new Integer(1);
47             else return new Integer(0);
48         }
49         else if (name.equals("println")) return println;
50         else if (name.equals("math")) return org.xwt.util.JSObject.defaultObjects.get("Math", null);
51         else return super.get(name, start);
52     }
53
54     public void put(String name, Scriptable start, Object value) {
55         if (name == null) return;
56         else if (name.equals("thread") && value != null && value instanceof Function) ThreadMessage.newthread((Function)value);
57         else if (name.equals("clipboard")) Platform.setClipBoard(value.toString());
58         else super.put(name, start, value);
59     }
60
61
62     // JSFunction Instances ///////////////////////////////////////////////////////////////////
63
64     /** Helper class for defining functions. */
65     private static abstract class JSFunction extends JSObject implements Function {
66         JSFunction() { setSeal(true); }
67         public Scriptable construct(Context cx, Scriptable scope, java.lang.Object[] args) { return null; }
68     }
69
70     private static final JSFunction newBrowserWindow = new JSFunction() {
71             public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
72                 if (args.length != 1 || args[0] == null) return null;
73                 Platform.newBrowserWindow(args[0].toString());
74                 return null;
75             }
76         };
77
78     private static final JSFunction yield = new JSFunction() {
79             public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
80                 sleep.call(cx, null, null, null);
81                 return null;
82             }
83         };
84
85     private static final JSFunction println = new JSFunction() {
86             public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
87                 if (args.length == 1)
88                     if (Log.on)
89                         Log.log(cx.interpreterSourceFile, args[0] == null ? "null" : args[0].toString());
90                 return null;
91             }
92         };
93
94     private static final JSFunction date = new JSFunction() {
95             public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
96                 try { return Context.enter().newObject(org.xwt.util.JSObject.defaultObjects, "Date", args);
97                 } catch (Exception e) {
98                     if (Log.on) Log.log(this, "Exception in Context.newObject() -- this should never happen");
99                     if (Log.on) Log.log(this, e);
100                     return null;
101                 }
102             }
103         };
104
105     private static final JSFunction regexp = new JSFunction() {
106             public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
107                 try { return Context.enter().newObject(org.xwt.util.JSObject.defaultObjects, "RegExp", args);
108                 } catch (Exception e) {
109                     if (Log.on) Log.log(this, "Exception in Context.newObject() -- this should never happen");
110                     if (Log.on) Log.log(this, e);
111                     return null;
112                 }
113             }
114         };
115
116     private static final JSFunction listfonts = new JSFunction() {
117             public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
118                 return Context.enter().newArray(org.xwt.util.JSObject.defaultObjects,  Platform.listFonts());
119             }
120         };
121
122     private static final JSFunction theme = new JSFunction() {
123             public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
124                 if (args.length != 2) return null;
125                 if (args[0] == null || args[1] == null) return null;
126                 Template.retheme(args[0].toString(), args[1].toString());
127                 return null;
128             }
129         };
130
131     private static final JSFunction xmlrpc = new JSFunction() {
132             public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
133                 if (args.length != 1 || args[0] == null) return null;
134                 return new XMLRPC(args[0].toString(), "");
135             }
136         };
137
138     private static final JSFunction soap = new JSFunction() {
139             public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
140                 if (args.length == 1 && args[0] != null) return new SOAP(args[0].toString(), "", null, null);
141                 else if (args.length == 2 && args[0] != null && args[1] != null)
142                     return new SOAP(args[0].toString(), "", args[1].toString(), null);
143                 else if (args.length == 3 && args[0] != null && args[1] != null && args[2] != null)
144                     return new SOAP(args[0].toString(), "", args[1].toString(), args[2].toString());
145                 else return null;
146             }
147         };
148
149     private static final JSFunction textwidth = new JSFunction() {
150             public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) throws JavaScriptException {
151                 if (args.length < 1 || args.length > 2) return null;
152                 if (args[0] == null || (args.length == 2 && args[1] == null)) return null;
153                 String font = args.length == 1 ? Platform.getDefaultFont() : args[0].toString();
154                 String text = args.length == 1 ? args[0].toString() : args[1].toString();
155                 XWF xwf = XWF.getXWF(font);
156                 if (xwf == null) return new Integer(Platform.stringWidth(font, text));
157                 else return new Integer(xwf.stringWidth(text));
158             }
159         };
160
161
162     private static final JSFunction textheight = new JSFunction() {
163             public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
164                 if (args.length > 1) return null;
165                 if (args.length == 1 && args[0] == null) return null;
166                 String font = args.length == 0 || args[0] == null ? Platform.getDefaultFont() : args[0].toString();
167                 XWF xwf = XWF.getXWF(font);
168                 if (xwf == null) return new Integer(Platform.getMaxAscent(font) + Platform.getMaxDescent(font));
169                 else return new Integer(xwf.getMaxAscent() + xwf.getMaxDescent());
170             }
171         };
172
173     private static final JSFunction newBox = new JSFunction() {
174             public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
175                 Box ret = new Box(args.length == 0 || args[0] == null ? "box" : args[0].toString(), Template.defaultImportList);
176                 for(int i=1; i<args.length; i++)
177                     if (args[i] instanceof Box)
178                         ret.put(ret.numChildren(), null, (Box)args[i]);
179                 for(int i=1; i<args.length; i++)
180                     if (args[i] instanceof Scriptable && !(args[i] instanceof Box)) {
181                         Scriptable s = (Scriptable)args[i];
182                         Object[] keys = s.getIds();
183                         for(int j=0; j<keys.length; j++) ret.put(keys[j].toString(), null, s.get(keys[j].toString(), s));
184                     }
185                 return ret;
186             }
187         };
188
189     private static final JSFunction sleep = new JSFunction() {
190             public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
191                 if (args != null && (args.length != 1 || args[0] == null)) return null;
192                 int i = args == null ? 0 : SpecialBoxProperty.stoi(args[0].toString());
193
194                 Thread thread = Thread.currentThread();
195                 if (!(thread instanceof ThreadMessage)) {
196                     if (Log.on) Log.log(this, "cannot sleep() or yield() in the foreground thread");
197                     return null;
198                 }
199                 ThreadMessage mythread = (ThreadMessage)thread;
200                 mythread.done.release();
201
202                 if (i > 0) try { Thread.sleep(i); } catch (Exception e) { }
203                 
204                 MessageQueue.add(mythread);
205                 mythread.go.block();
206                 return null;
207             }
208         };
209
210 }
211
212
213
214
215