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