KickJava   Java API By Example, From Geeks To Geeks.

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


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.dev.shell.CompilingClassLoader;
19 import com.google.gwt.dev.shell.JsValueGlue;
20 import com.google.gwt.dev.shell.LowLevel;
21
22 import org.eclipse.swt.browser.Browser;
23 import org.eclipse.swt.internal.ole.win32.COM;
24 import org.eclipse.swt.internal.ole.win32.COMObject;
25 import org.eclipse.swt.internal.win32.OS;
26 import org.eclipse.swt.ole.win32.Variant;
27
28 /**
29  * A bag of static helper methods for mucking about with low-level SWT and COM
30  * constructs. Much of this is necessary simply to do things that the SWT
31  * implementers weren't really thinking about when they wrote the COM layer.
32  */

33 class SwtOleGlue {
34
35   /**
36    * Converts a java object to its equivalent variant. A ClassLoader is passed
37    * here so that Handles can be manipulated properly.
38    */

39   public static Variant convertObjectToVariant(CompilingClassLoader cl,
40       Class JavaDoc type, Object JavaDoc o) {
41     if (type.equals(Variant.class)) {
42       return (Variant) o;
43     }
44     JsValueIE6 jsValue = new JsValueIE6();
45     JsValueGlue.set(jsValue, cl, type, o);
46     return jsValue.getVariant();
47   }
48
49   /**
50    * Converts an array of variants to their equivalent java objects.
51    */

52   public static Object JavaDoc[] convertVariantsToObjects(Class JavaDoc[] argTypes,
53       Variant[] varArgs, String JavaDoc msgPrefix) {
54     Object JavaDoc[] javaArgs = new Object JavaDoc[Math.min(varArgs.length, argTypes.length)];
55     for (int i = 0; i < javaArgs.length; i++) {
56       try {
57         Object JavaDoc javaArg = JsValueGlue.get(new JsValueIE6(varArgs[i]),
58             argTypes[i], msgPrefix);
59         javaArgs[i] = javaArg;
60       } catch (IllegalArgumentException JavaDoc e) {
61         throw new IllegalArgumentException JavaDoc("Error converting argument "
62             + (i + 1) + ": " + e.getMessage());
63       }
64     }
65
66     return javaArgs;
67   }
68
69   /**
70    * Extracts an array of strings from an (OLECHAR**) type (useful for
71    * implementing GetIDsOfNames()).
72    */

73   public static String JavaDoc[] extractStringArrayFromOleCharPtrPtr(int ppchar,
74       int count) {
75     String JavaDoc[] strings = new String JavaDoc[count];
76     for (int i = 0; i < count; ++i) {
77       int[] pchar = new int[1];
78       OS.MoveMemory(pchar, ppchar + 4 * i, 4);
79       strings[i] = extractStringFromOleCharPtr(pchar[0]);
80     }
81     return strings;
82   }
83
84   /**
85    * Extracts a string from an (OLECHAR*) type.
86    */

87   public static String JavaDoc extractStringFromOleCharPtr(int pOleChar) {
88     // TODO: double-check the encoding (is it UTF-16?, what)
89
int size = COM.SysStringByteLen(pOleChar);
90     if (size > 8192) {
91       size = 8192;
92     }
93     char[] buffer = new char[(size + 1) / 2];
94     COM.MoveMemory(buffer, pOleChar, size);
95
96     String JavaDoc s = new String JavaDoc(buffer);
97     if (s.indexOf('\0') != -1) {
98       return s.substring(0, s.indexOf('\0'));
99     } else {
100       return s;
101     }
102   }
103
104   /**
105    * Injects an object into the Browser class that resolves to IE's
106    * 'window.external' object.
107    */

108   public static void injectBrowserScriptExternalObject(Browser browser,
109       final IDispatchImpl external) {
110     // Grab the browser's 'site.iDocHostUIHandler' field.
111
//
112
Object JavaDoc webSite = LowLevel.snatchFieldObjectValue(browser, "site");
113     COMObject iDocHostUIHandler = (COMObject) LowLevel.snatchFieldObjectValue(
114         webSite, "iDocHostUIHandler");
115
116     // Create a COMObjectProxy that will override GetExternal().
117
//
118
COMObjectProxy webSiteProxy = new COMObjectProxy(new int[] {
119         2, 0, 0, 4, 1, 5, 0, 0, 1, 1, 1, 3, 3, 2, 2, 1, 3, 2}) {
120
121       {
122         // make sure we hold onto a ref on the external object
123
external.AddRef();
124       }
125
126       public int method15(int[] args) {
127         // GetExternal() is method 15.
128
//
129
return GetExternal(args[0]);
130       }
131
132       public int method2(int[] args) {
133         int result = super.method2(args);
134         if (result == 0) {
135           external.Release();
136         }
137         return result;
138       }
139
140       // CHECKSTYLE_OFF
141
int GetExternal(int ppDispatch) {
142         // CHECKSTYLE_ON
143
if (ppDispatch != 0) {
144           try {
145             // Return the 'external' object.
146
//
147
external.AddRef();
148             OS.MoveMemory(ppDispatch, new int[] {external.getAddress()}, 4);
149             return COM.S_OK;
150           } catch (Throwable JavaDoc e) {
151             e.printStackTrace();
152             return COM.E_FAIL;
153           }
154         } else {
155           OS.MoveMemory(ppDispatch, new int[] {0}, 4);
156           return COM.E_NOTIMPL;
157         }
158       }
159
160     };
161
162     // Interpose the proxy in front of the browser's iDocHostUiHandler.
163
//
164
webSiteProxy.interpose(iDocHostUIHandler);
165   }
166
167   /**
168    * Wrapper for the OS' SysAllocString().
169    */

170   public static int sysAllocString(String JavaDoc s) {
171     int len = s.length();
172     char[] chars = new char[len + 1];
173     s.getChars(0, len, chars, 0);
174     return COM.SysAllocString(chars);
175   }
176  
177 }
178
Popular Tags