KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2006 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.core.ext.TreeLogger;
19 import com.google.gwt.dev.shell.BrowserWidget;
20 import com.google.gwt.dev.shell.BrowserWidgetHost;
21 import com.google.gwt.dev.shell.ModuleSpaceHost;
22
23 import org.eclipse.swt.SWTException;
24 import org.eclipse.swt.internal.ole.win32.COM;
25 import org.eclipse.swt.internal.ole.win32.IDispatch;
26 import org.eclipse.swt.ole.win32.OleAutomation;
27 import org.eclipse.swt.ole.win32.Variant;
28 import org.eclipse.swt.widgets.Shell;
29
30 import java.lang.reflect.InvocationTargetException JavaDoc;
31 import java.lang.reflect.Method JavaDoc;
32
33 /**
34  * Represents an individual browser window and all of its controls.
35  */

36 public class BrowserWidgetIE6 extends BrowserWidget {
37
38   /**
39    * IDispatch implementation of the window.external object.
40    */

41   public class External extends IDispatchImpl {
42
43     /**
44      * Called by the loaded HTML page to activate a new module.
45      *
46      * @param frameWnd a reference to the IFRAME in which the module's injected
47      * JavaScript will live
48      * @param moduleName the name of the module to load, null if this is being
49      * unloaded
50      */

51     public boolean gwtOnLoad(IDispatch frameWnd, String JavaDoc moduleName) {
52       try {
53         if (moduleName == null) {
54           // Indicates one or more modules are being unloaded.
55
handleUnload(frameWnd);
56           return true;
57         }
58         
59         // set the module ID
60
int moduleID = ++nextModuleID;
61         Integer JavaDoc key = new Integer JavaDoc(moduleID);
62         setIntProperty(frameWnd, "__gwt_module_id", moduleID);
63
64         // Attach a new ModuleSpace to make it programmable.
65
//
66
ModuleSpaceHost msh = getHost().createModuleSpaceHost(
67             BrowserWidgetIE6.this, moduleName);
68         ModuleSpaceIE6 moduleSpace = new ModuleSpaceIE6(msh, frameWnd,
69             moduleName, key);
70         attachModuleSpace(moduleSpace);
71         return true;
72       } catch (Throwable JavaDoc e) {
73         // We do catch Throwable intentionally because there are a ton of things
74
// that can go wrong trying to load a module, including Error-derived
75
// things like NoClassDefFoundError.
76
//
77
getHost().getLogger().log(TreeLogger.ERROR,
78             "Failure to load module '" + moduleName + "'", e);
79         return false;
80       }
81     }
82
83     protected void getIDsOfNames(String JavaDoc[] names, int[] ids)
84         throws HResultException {
85
86       if (names.length >= 2) {
87         throw new HResultException(DISP_E_UNKNOWNNAME);
88       }
89
90       String JavaDoc name = names[0].toLowerCase();
91       if (name.equals("gwtonload")) {
92         ids[0] = 1;
93         return;
94       }
95
96       throw new HResultException(DISP_E_UNKNOWNNAME);
97     }
98
99     /**
100      * Unload one or more modules.
101      *
102      * @param frameWnd window to unload, null if all
103      */

104     protected void handleUnload(IDispatch frameWnd) {
105       Integer JavaDoc key = null;
106       if (frameWnd != null) {
107         key = new Integer JavaDoc(getIntProperty(frameWnd, "__gwt_module_id"));
108       }
109       doUnload(key);
110     }
111
112     protected Variant invoke(int dispId, int flags, Variant[] params)
113         throws HResultException, InvocationTargetException JavaDoc {
114
115       if (dispId == 0 && (flags & COM.DISPATCH_PROPERTYGET) != 0) {
116         // MAGIC: this is the default property, let's just do toString()
117
return new Variant(toString());
118       } else if (dispId == 1) {
119         if ((flags & COM.DISPATCH_METHOD) != 0) {
120           try {
121             IDispatch frameWnd = (params[0].getType() == COM.VT_DISPATCH)
122                 ? params[0].getDispatch() : null;
123             String JavaDoc moduleName = (params[1].getType() == COM.VT_BSTR)
124                 ? params[1].getString() : null;
125             boolean success = gwtOnLoad(frameWnd, moduleName);
126
127             // boolean return type
128
return new Variant(success);
129           } catch (SWTException e) {
130             throw new HResultException(COM.E_INVALIDARG);
131           }
132         } else if ((flags & COM.DISPATCH_PROPERTYGET) != 0) {
133           // property get on the method itself
134
try {
135             Method JavaDoc gwtOnLoadMethod = getClass().getMethod("gwtOnLoad",
136                 new Class JavaDoc[] {IDispatch.class, String JavaDoc.class});
137             IDispatchImpl funcObj = new MethodDispatch(null, gwtOnLoadMethod);
138             IDispatch disp = new IDispatch(funcObj.getAddress());
139             disp.AddRef();
140             return new Variant(disp);
141           } catch (Exception JavaDoc e) {
142             // just return VT_EMPTY
143
return new Variant();
144           }
145         }
146         throw new HResultException(COM.E_NOTSUPPORTED);
147       }
148
149       // The specified member id is out of range.
150
throw new HResultException(COM.DISP_E_MEMBERNOTFOUND);
151     }
152   }
153
154   // counter to generate unique module IDs
155
private static int nextModuleID = 0;
156
157   /**
158    * Get a property off a window object as an integer.
159    *
160    * @param frameWnd inner code frame
161    * @param propName name of the property to get
162    * @return the property value as an integer
163    * @throws RuntimeException if the property does not exist
164    */

165   private static int getIntProperty(IDispatch frameWnd, String JavaDoc propName) {
166     OleAutomation window = null;
167     try {
168       window = new OleAutomation(frameWnd);
169       int[] dispID = window.getIDsOfNames(new String JavaDoc[] { propName });
170       if (dispID == null) {
171         throw new RuntimeException JavaDoc("No such property " + propName);
172       }
173       Variant value = null;
174       try {
175         value = window.getProperty(dispID[0]);
176         return value.getInt();
177       } finally {
178         if (value != null) {
179           value.dispose();
180         }
181       }
182     } finally {
183       if (window != null) {
184         window.dispose();
185       }
186     }
187   }
188
189   /**
190    * Set a property off a window object from an integer value.
191    *
192    * @param frameWnd inner code frame
193    * @param propName name of the property to set
194    * @param intValue the value to set
195    * @throws RuntimeException if the property does not exist
196    */

197   private static void setIntProperty(IDispatch frameWnd, String JavaDoc propName,
198       int intValue) {
199     OleAutomation window = null;
200     try {
201       window = new OleAutomation(frameWnd);
202       int[] dispID = window.getIDsOfNames(new String JavaDoc[] { propName });
203       if (dispID == null) {
204         throw new RuntimeException JavaDoc("No such property " + propName);
205       }
206       Variant value = null;
207       try {
208         value = new Variant(intValue);
209         window.setProperty(dispID[0], value);
210       } finally {
211         if (value != null) {
212           value.dispose();
213         }
214       }
215     } finally {
216       if (window != null) {
217         window.dispose();
218       }
219     }
220   }
221
222   public BrowserWidgetIE6(Shell shell, BrowserWidgetHost host) {
223     super(shell, host);
224
225     // Expose a 'window.external' object. This object's onLoad() method will
226
// be called when a hosted mode application's wrapper HTML is done loading.
227
//
228
SwtOleGlue.injectBrowserScriptExternalObject(browser, new External());
229
230     // Make sure that the LowLevelIE6 magic is properly initialized.
231
//
232
LowLevelIE6.init();
233   }
234 }
235
Popular Tags