KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > dev > shell > JavaScriptHost


1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.dev.shell;
17
18 /**
19  * This class contains a set of static methods that can be used to interact with
20  * the browser in hosted mode.
21  */

22 public class JavaScriptHost {
23
24   private static ShellJavaScriptHost sHost;
25
26   /**
27    * Defines a new native JavaScript function.
28    *
29    * @param file source file of the function
30    * @param line starting line number of the function
31    * @param jsniSignature the function's jsni signature
32    * @param paramNames the parameter types
33    * @param js the script body
34    */

35   public static void createNative(String JavaDoc file, int line, String JavaDoc jsniSignature,
36       String JavaDoc[] paramNames, String JavaDoc js) {
37     sHost.createNative(file, line, jsniSignature, paramNames, js);
38   }
39
40   public static void exceptionCaught(int number, String JavaDoc name, String JavaDoc description) {
41     sHost.exceptionCaught(number, name, description);
42   }
43
44   /**
45    * Invoke a native JavaScript function that returns a boolean value.
46    */

47   public static boolean invokeNativeBoolean(String JavaDoc name, Object JavaDoc jthis,
48       Class JavaDoc[] types, Object JavaDoc[] args) throws Throwable JavaDoc {
49     return sHost.invokeNativeBoolean(name, jthis, types, args);
50   }
51
52   /**
53    * Invoke a native JavaScript function that returns a byte value.
54    */

55   public static byte invokeNativeByte(String JavaDoc name, Object JavaDoc jthis, Class JavaDoc[] types,
56       Object JavaDoc[] args) throws Throwable JavaDoc {
57     return sHost.invokeNativeByte(name, jthis, types, args);
58   }
59
60   /**
61    * Invoke a native JavaScript function that returns a character value.
62    */

63   public static char invokeNativeChar(String JavaDoc name, Object JavaDoc jthis, Class JavaDoc[] types,
64       Object JavaDoc[] args) throws Throwable JavaDoc {
65     return sHost.invokeNativeChar(name, jthis, types, args);
66   }
67
68   /**
69    * Invoke a native JavaScript function that returns a double value.
70    */

71   public static double invokeNativeDouble(String JavaDoc name, Object JavaDoc jthis,
72       Class JavaDoc[] types, Object JavaDoc[] args) throws Throwable JavaDoc {
73     return sHost.invokeNativeDouble(name, jthis, types, args);
74   }
75
76   /**
77    * Invoke a native JavaScript function that returns a float value.
78    */

79   public static float invokeNativeFloat(String JavaDoc name, Object JavaDoc jthis,
80       Class JavaDoc[] types, Object JavaDoc[] args) throws Throwable JavaDoc {
81     return sHost.invokeNativeFloat(name, jthis, types, args);
82   }
83
84   /**
85    * Invoke a native JavaScript function that returns a handle value.
86    */

87   public static Object JavaDoc invokeNativeHandle(String JavaDoc name, Object JavaDoc jthis,
88       Class JavaDoc returnType, Class JavaDoc[] types, Object JavaDoc[] args) throws Throwable JavaDoc {
89     return sHost.invokeNativeHandle(name, jthis, returnType, types, args);
90   }
91
92   /**
93    * Invoke a native JavaScript function that returns an integer value.
94    */

95   public static int invokeNativeInt(String JavaDoc name, Object JavaDoc jthis, Class JavaDoc[] types,
96       Object JavaDoc[] args) throws Throwable JavaDoc {
97     return sHost.invokeNativeInt(name, jthis, types, args);
98   }
99
100   /**
101    * Invoke a native JavaScript function that returns a long value.
102    */

103   public static long invokeNativeLong(String JavaDoc name, Object JavaDoc jthis, Class JavaDoc[] types,
104       Object JavaDoc[] args) throws Throwable JavaDoc {
105     return sHost.invokeNativeLong(name, jthis, types, args);
106   }
107
108   /**
109    * Invoke a native JavaScript function that returns an object value.
110    */

111   public static Object JavaDoc invokeNativeObject(String JavaDoc name, Object JavaDoc jthis,
112       Class JavaDoc[] types, Object JavaDoc[] args) throws Throwable JavaDoc {
113     return sHost.invokeNativeObject(name, jthis, types, args);
114   }
115
116   /**
117    * Invoke a native JavaScript function that returns a short value.
118    */

119   public static short invokeNativeShort(String JavaDoc name, Object JavaDoc jthis,
120       Class JavaDoc[] types, Object JavaDoc[] args) throws Throwable JavaDoc {
121     return sHost.invokeNativeShort(name, jthis, types, args);
122   }
123
124   /**
125    * Invoke a native JavaScript function that returns a string value.
126    */

127   public static String JavaDoc invokeNativeString(String JavaDoc name, Object JavaDoc jthis,
128       Class JavaDoc[] types, Object JavaDoc[] args) throws Throwable JavaDoc {
129     return sHost.invokeNativeString(name, jthis, types, args);
130   }
131
132   /**
133    * Invoke a native JavaScript function that returns no value.
134    */

135   public static void invokeNativeVoid(String JavaDoc name, Object JavaDoc jthis, Class JavaDoc[] types,
136       Object JavaDoc[] args) throws Throwable JavaDoc {
137     sHost.invokeNativeVoid(name, jthis, types, args);
138   }
139
140   /**
141    * Logs in dev shell.
142    */

143   public static void log(String JavaDoc message, Throwable JavaDoc e) {
144     sHost.log(message, e);
145   }
146
147   /**
148    * Resolves a deferred binding request and create the requested object.
149    */

150   public static Object JavaDoc rebindAndCreate(Class JavaDoc requestedClass) {
151     String JavaDoc className = requestedClass.getName();
152     try {
153       return sHost.rebindAndCreate(className);
154     } catch (Throwable JavaDoc e) {
155       String JavaDoc msg = "Deferred binding failed for '" + className
156           + "' (did you forget to inherit a required module?)";
157       throw new RuntimeException JavaDoc(msg, e);
158     }
159   }
160
161   /**
162    * This method is called via reflection from the shell, providing the hosted
163    * mode application with all of the methods it needs to interface with the
164    * browser and the server (for deferred binding).
165    */

166   public static void setHost(ShellJavaScriptHost host) {
167     sHost = host;
168   }
169 }
170
Popular Tags