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