KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > dev > shell > ie > ModuleSpaceIE6


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.ie;
17
18 import com.google.gwt.dev.shell.JsValue;
19 import com.google.gwt.dev.shell.ModuleSpace;
20 import com.google.gwt.dev.shell.ModuleSpaceHost;
21 import com.google.gwt.dev.shell.ie.IDispatchImpl.HResultException;
22
23 import org.eclipse.swt.internal.ole.win32.IDispatch;
24 import org.eclipse.swt.ole.win32.OleAutomation;
25 import org.eclipse.swt.ole.win32.Variant;
26
27 /**
28  * An implementation of {@link com.google.gwt.dev.shell.ModuleSpace} for
29  * Internet Explorer 6.
30  */

31 public class ModuleSpaceIE6 extends ModuleSpace {
32   /**
33    * Invoke a JavaScript function. The static function exists to allow
34    * platform-dependent code to make JavaScript calls without having a
35    * ModuleSpaceIE6 (and all that entails) if it is not required.
36    *
37    * @param window the window containing the function
38    * @param name the name of the function
39    * @param vArgs the array of arguments. vArgs[0] is the this parameter
40    * supplied to the function, which must be null if it is static.
41    * @return the return value of the JavaScript function
42    */

43   protected static Variant doInvokeOnWindow(OleAutomation window, String JavaDoc name,
44       Variant[] vArgs) {
45     OleAutomation funcObj = null;
46     Variant funcObjVar = null;
47     try {
48
49       // Get the function object and its 'call' method.
50
//
51
int[] ids = window.getIDsOfNames(new String JavaDoc[] {name});
52       if (ids == null) {
53         throw new RuntimeException JavaDoc(
54             "Could not find a native method with the signature '" + name + "'");
55       }
56       int functionId = ids[0];
57       funcObjVar = window.getProperty(functionId);
58       funcObj = funcObjVar.getAutomation();
59       int callDispId = funcObj.getIDsOfNames(new String JavaDoc[] {"call"})[0];
60
61       // Invoke it and return the result.
62
//
63
return funcObj.invoke(callDispId, vArgs);
64
65     } finally {
66       if (funcObjVar != null) {
67         funcObjVar.dispose();
68       }
69
70       if (funcObj != null) {
71         funcObj.dispose();
72       }
73     }
74   }
75
76   // CHECKSTYLE_OFF
77
private static int CODE(int hresult) {
78     return hresult & 0xFFFF;
79   }
80   // CHECKSTYLE_ON
81

82   private final OleAutomation window;
83
84   /**
85    * Constructs a browser interface for use with an IE6 'window' automation
86    * object.
87    *
88    * @param moduleName
89    */

90   public ModuleSpaceIE6(ModuleSpaceHost host, IDispatch scriptFrameWindow,
91       String JavaDoc moduleName, Object JavaDoc key) {
92     super(host, moduleName, key);
93
94     window = new OleAutomation(scriptFrameWindow);
95   }
96
97   public void createNative(String JavaDoc file, int line, String JavaDoc jsniSignature,
98       String JavaDoc[] paramNames, String JavaDoc js) {
99     // Execute the function definition within the browser, which will define
100
// a new top-level function.
101
//
102
String JavaDoc newScript = createNativeMethodInjector(jsniSignature, paramNames, js);
103     try {
104       // TODO: somehow insert file/line info into the script
105
Variant result = execute(newScript);
106       if (result != null) {
107         result.dispose();
108       }
109     } catch (RuntimeException JavaDoc e) {
110       throw new RuntimeException JavaDoc(file + "(" + line
111           + "): Failed to create JSNI method with signature '" + jsniSignature
112           + "'", e);
113     }
114   }
115
116   public void dispose() {
117     // Dispose everything else.
118
if (window != null) {
119       window.dispose();
120     }
121     super.dispose();
122   }
123
124   /**
125    * Invokes a native javascript function.
126    *
127    * @param name the name of the function to invoke
128    * @param jthis the function's 'this' context
129    * @param types the type of each argument
130    * @param args the arguments to be passed
131    * @return the return value as a Variant.
132    */

133   protected JsValue doInvoke(String JavaDoc name, Object JavaDoc jthis, Class JavaDoc[] types,
134       Object JavaDoc[] args) throws Throwable JavaDoc {
135     Variant[] vArgs = null;
136     try {
137       // Build the argument list, including 'jthis'.
138
//
139
int len = args.length;
140       vArgs = new Variant[len + 1];
141       JsValueIE6 jsValue = new JsValueIE6();
142       jsValue.setWrappedJavaObject(getIsolatedClassLoader(), jthis);
143       vArgs[0] = jsValue.getVariant();
144
145       for (int i = 0; i < len; ++i) {
146         vArgs[i + 1] = SwtOleGlue.convertObjectToVariant(
147             getIsolatedClassLoader(), types[i], args[i]);
148       }
149
150       Variant result = doInvokeOnWindow(window, name, vArgs);
151       try {
152         return new JsValueIE6(result);
153       } finally {
154         if (result != null) {
155           result.dispose();
156         }
157       }
158     } finally {
159       // We allocated variants for all arguments, so we must dispose them all.
160
//
161
for (int i = 0; i < vArgs.length; ++i) {
162         if (vArgs[i] != null) {
163           vArgs[i].dispose();
164         }
165       }
166     }
167   }
168   
169   protected Object JavaDoc getStaticDispatcher() {
170     return new IDispatchProxy(getIsolatedClassLoader());
171   }
172
173   protected boolean isExceptionSame(Throwable JavaDoc original, int number, String JavaDoc name, String JavaDoc message) {
174     HResultException hre = new HResultException(original);
175     return CODE(hre.getHResult()) == CODE(number) && hre.getMessage().equals(message);
176   }
177
178   private Variant execute(String JavaDoc code) {
179     int[] dispIds = window.getIDsOfNames(new String JavaDoc[] {"execScript", "code"});
180     Variant[] vArgs = new Variant[1];
181     vArgs[0] = new Variant(code);
182     int[] namedArgs = new int[1];
183     namedArgs[0] = dispIds[1];
184     Variant result = window.invoke(dispIds[0], vArgs, namedArgs);
185     vArgs[0].dispose();
186     if (result == null) {
187       String JavaDoc lastError = window.getLastError();
188       throw new RuntimeException JavaDoc("Error (" + lastError
189           + ") executing JavaScript:\n" + code);
190     }
191     return result;
192   }
193 }
194
Popular Tags