KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > ole > win32 > OLE


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.swt.ole.win32;
12
13 import org.eclipse.swt.*;
14 import java.io.File JavaDoc;
15 import org.eclipse.swt.internal.ole.win32.COM;
16 import org.eclipse.swt.internal.win32.OS;
17 import org.eclipse.swt.internal.win32.TCHAR;
18
19
20 /**
21  *
22  * OLE contains all the constants used to create an ActiveX Control or an OLE Document.
23  *
24  * <p>Definitions for these constants can be found in MSDN.
25  *
26  */

27 public class OLE extends SWT {
28
29     public static final int S_FALSE = 1; // Used for functions that semantically return a Boolean FALSE result to indicate that the function succeeded.
30
public static final int S_OK = 0; // Function succeeded.
31
public static final int E_FAIL = -2147467259; // Unspecified failure.
32
public static final int E_INVALIDARG = -2147024809; // Invalid argument
33
public static final int E_NOINTERFACE = -2147467262; // QueryInterface did not recognize the requested interface.
34
public static final int E_NOTIMPL = -2147467263; // Not implemented
35

36     public static final String JavaDoc IID_IUNKNOWN = "{00000000-0000-0000-C000-000000000046}"; //$NON-NLS-1$
37
public static final String JavaDoc IID_IDISPATCH = "{00020400-0000-0000-C000-000000000046}"; //$NON-NLS-1$
38

39     // Verbs that can be invoked on this client
40
public static final int OLEIVERB_DISCARDUNDOSTATE = -6; // close the OLE object and discard the undo state
41
public static final int OLEIVERB_HIDE = -3; // hide the OLE object
42
public static final int OLEIVERB_INPLACEACTIVATE = -5; // open the OLE for editing in-place
43
public static final int OLEIVERB_OPEN = -2; // open the OLE object for editing in a separate window
44
public static final int OLEIVERB_PRIMARY = 0; // opens the OLE object for editing
45
public static final int OLEIVERB_PROPERTIES = -7; // request the OLE object properties dialog
46
public static final int OLEIVERB_SHOW = -1; // show the OLE object
47
public static final int OLEIVERB_UIACTIVATE = -4; // activate the UI for the OLE object
48

49     public static final int PROPERTY_CHANGING = 0;
50     public static final int PROPERTY_CHANGED = 1;
51
52     /**
53      * Error code for OleError - No specific error information available
54      */

55     public static final int HRESULT_UNSPECIFIED = 0;
56     /**
57      * Error code for OleError - Failed to create file
58      */

59     public static final int ERROR_CANNOT_CREATE_FILE = 1000;
60     /**
61      * Error code for OleError - Failed to create Ole Client
62      */

63     public static final int ERROR_CANNOT_CREATE_OBJECT = 1001;
64     /**
65      * Error code for OleError - File does not exist, is not accessible to user or does not have the correct format
66      */

67     public static final int ERROR_CANNOT_OPEN_FILE = 1002;
68     /**
69      * Error code for OleError - Failed to find requested interface on OLE Object
70      */

71     public static final int ERROR_INTERFACE_NOT_FOUND = 1003;
72     /**
73      * Error code for OleError - Class ID not found in registry
74      */

75     public static final int ERROR_INVALID_CLASSID = 1004;
76     /**
77      * Error code for OleError - Failed to get the class factory for the specified classID
78      */

79     public static final int ERROR_CANNOT_ACCESS_CLASSFACTORY = 1005;
80     /**
81      * Error code for OleError - Failed to create Licensed instance
82      */

83     public static final int ERROR_CANNOT_CREATE_LICENSED_OBJECT = 1006;
84     /**
85      * Error code for OleError - Out of Memory
86      */

87     public static final int ERROR_OUT_OF_MEMORY = 1007;
88     /**
89      * Error code for OleError - Failed to change Variant type
90      */

91     public static final int ERROR_CANNOT_CHANGE_VARIANT_TYPE = 1010;
92     /**
93      * Error code for OleError - Invalid address received for Ole Interface
94      */

95     public static final int ERROR_INVALID_INTERFACE_ADDRESS = 1011;
96     /**
97      * Error code for OleError - Unable to find Application
98      */

99     public static final int ERROR_APPLICATION_NOT_FOUND = 1013;
100     /**
101      * Error code for OleError - Action can not be performed
102      */

103     public static final int ERROR_ACTION_NOT_PERFORMED = 1014;
104
105     public static final int OLECMDF_SUPPORTED = 1;
106     public static final int OLECMDF_ENABLED = 2;
107     public static final int OLECMDF_LATCHED = 4;
108     public static final int OLECMDF_NINCHED = 8;
109
110     public static final int OLECMDTEXTF_NONE = 0;
111     public static final int OLECMDTEXTF_NAME = 1;
112     public static final int OLECMDTEXTF_STATUS = 2;
113
114     public static final int OLECMDEXECOPT_DODEFAULT = 0;
115     public static final int OLECMDEXECOPT_PROMPTUSER = 1;
116     public static final int OLECMDEXECOPT_DONTPROMPTUSER = 2;
117     public static final int OLECMDEXECOPT_SHOWHELP = 3;
118     
119     public static final int OLECMDID_OPEN = 1;
120     public static final int OLECMDID_NEW = 2;
121     public static final int OLECMDID_SAVE = 3;
122     public static final int OLECMDID_SAVEAS = 4;
123     public static final int OLECMDID_SAVECOPYAS = 5;
124     public static final int OLECMDID_PRINT = 6;
125     public static final int OLECMDID_PRINTPREVIEW = 7;
126     public static final int OLECMDID_PAGESETUP = 8;
127     public static final int OLECMDID_SPELL = 9;
128     public static final int OLECMDID_PROPERTIES = 10;
129     public static final int OLECMDID_CUT = 11;
130     public static final int OLECMDID_COPY = 12;
131     public static final int OLECMDID_PASTE = 13;
132     public static final int OLECMDID_PASTESPECIAL = 14;
133     public static final int OLECMDID_UNDO = 15;
134     public static final int OLECMDID_REDO = 16;
135     public static final int OLECMDID_SELECTALL = 17;
136     public static final int OLECMDID_CLEARSELECTION = 18;
137     public static final int OLECMDID_ZOOM = 19;
138     public static final int OLECMDID_GETZOOMRANGE = 20;
139     public static final int OLECMDID_UPDATECOMMANDS = 21;
140     public static final int OLECMDID_REFRESH = 22;
141     public static final int OLECMDID_STOP = 23;
142     public static final int OLECMDID_HIDETOOLBARS = 24;
143     public static final int OLECMDID_SETPROGRESSMAX = 25;
144     public static final int OLECMDID_SETPROGRESSPOS = 26;
145     public static final int OLECMDID_SETPROGRESSTEXT = 27;
146     public static final int OLECMDID_SETTITLE = 28;
147     public static final int OLECMDID_SETDOWNLOADSTATE = 29;
148     public static final int OLECMDID_STOPDOWNLOAD = 30;
149
150     /* Ole Property Description flags */
151     public static int VARFLAG_FREADONLY = 0x1;
152     public static int VARFLAG_FSOURCE = 0x2;
153     public static int VARFLAG_FBINDABLE = 0x4;
154     public static int VARFLAG_FREQUESTEDIT = 0x8;
155     public static int VARFLAG_FDISPLAYBIND = 0x10;
156     public static int VARFLAG_FDEFAULTBIND = 0x20;
157     public static int VARFLAG_FHIDDEN = 0x40;
158     public static int VARFLAG_FRESTRICTED = 0x80;
159     public static int VARFLAG_FDEFAULTCOLLELEM = 0x100;
160     public static int VARFLAG_FUIDEFAULT = 0x200;
161     public static int VARFLAG_FNONBROWSABLE = 0x400;
162     public static int VARFLAG_FREPLACEABLE = 0x800;
163     public static int VARFLAG_FIMMEDIATEBIND = 0x1000;
164
165     /* Ole Property Description kind */
166     public static int VAR_PERINSTANCE = 0;
167     public static int VAR_STATIC = 1;
168     public static int VAR_CONST = 2;
169     public static int VAR_DISPATCH = 3;
170     
171     /* Ole Parameter Description flags */
172     public static short IDLFLAG_NONE = 0;
173     public static short IDLFLAG_FIN = 1;
174     public static short IDLFLAG_FOUT = 2;
175     public static short IDLFLAG_FLCID = 4;
176     public static short IDLFLAG_FRETVAL = 8;
177         
178     /* Ole Description types */
179     public static final short VT_BOOL = 11; // Boolean; True=-1, False=0.
180
public static final short VT_BSTR = 8; // Binary String.
181
public static final short VT_BYREF = 16384; // By reference - must be combined with one of the other VT values
182
public static final short VT_CY = 6; // Currency.
183
public static final short VT_DATE = 7; // Date.
184
public static final short VT_DISPATCH = 9; // IDispatch
185
public static final short VT_EMPTY = 0; // Not specified.
186
public static final short VT_ERROR = 10; // Scodes.
187
public static final short VT_I2 = 2; // 2-byte signed int.
188
public static final short VT_I4 = 3; // 4-byte signed int.
189
public static final short VT_NULL = 1; // Null.
190
public static final short VT_R4 = 4; // 4-byte real.
191
public static final short VT_R8 = 5; // 8-byte real.
192
public static final short VT_UI1 = 17; // Unsigned char.
193
public static final short VT_UI4 = 19; // Unsigned int.
194
public static final short VT_UNKNOWN = 13; // IUnknown FAR*.
195
public static final short VT_VARIANT = 12; // VARIANT FAR*.
196
public static final short VT_PTR = 26;
197     public static final short VT_USERDEFINED = 29;
198     public static final short VT_HRESULT = 25;
199     public static final short VT_DECIMAL = 14;
200     public static final short VT_I1 = 16;
201     public static final short VT_UI2 = 18;
202     public static final short VT_I8 = 20;
203     public static final short VT_UI8 = 21;
204     public static final short VT_INT = 22;
205     public static final short VT_UINT = 23;
206     public static final short VT_VOID = 24;
207     public static final short VT_SAFEARRAY = 27;
208     public static final short VT_CARRAY = 28;
209     public static final short VT_LPSTR = 30;
210     public static final short VT_LPWSTR = 31;
211     public static final short VT_RECORD = 36;
212     public static final short VT_FILETIME = 64;
213     public static final short VT_BLOB = 65;
214     public static final short VT_STREAM = 66;
215     public static final short VT_STORAGE = 67;
216     public static final short VT_STREAMED_OBJECT = 68;
217     public static final short VT_STORED_OBJECT = 69;
218     public static final short VT_BLOB_OBJECT = 70;
219     public static final short VT_CF = 71;
220     public static final short VT_CLSID = 72;
221     public static final short VT_VERSIONED_STREAM = 73;
222     public static final short VT_BSTR_BLOB = 0xfff;
223     public static final short VT_VECTOR = 0x1000;
224     public static final short VT_ARRAY = 0x2000;
225
226     /* Ole Function Description Invoke Kind values */
227     public static final int INVOKE_FUNC = 1;
228     public static final int INVOKE_PROPERTYGET = 2;
229     public static final int INVOKE_PROPERTYPUT = 4;
230     public static final int INVOKE_PROPERTYPUTREF = 8;
231     
232     /* Ole Function Description function kind */
233     public static final int FUNC_VIRTUAL = 0;
234     public static final int FUNC_PUREVIRTUAL = 1;
235     public static final int FUNC_NONVIRTUAL = 2;
236     public static final int FUNC_STATIC = 3;
237     public static final int FUNC_DISPATCH = 4;
238     
239     /* Ole Function Description function flags */
240     public static final short FUNCFLAG_FRESTRICTED = 1;
241     public static final short FUNCFLAG_FSOURCE = 0x2;
242     public static final short FUNCFLAG_FBINDABLE = 0x4;
243     public static final short FUNCFLAG_FREQUESTEDIT = 0x8;
244     public static final short FUNCFLAG_FDISPLAYBIND = 0x10;
245     public static final short FUNCFLAG_FDEFAULTBIND = 0x20;
246     public static final short FUNCFLAG_FHIDDEN = 0x40;
247     public static final short FUNCFLAG_FUSESGETLASTERROR = 0x80;
248     public static final short FUNCFLAG_FDEFAULTCOLLELEM = 0x100;
249     public static final short FUNCFLAG_FUIDEFAULT = 0x200;
250     public static final short FUNCFLAG_FNONBROWSABLE = 0x400;
251     public static final short FUNCFLAG_FREPLACEABLE = 0x800;
252     public static final short FUNCFLAG_FIMMEDIATEBIND = 0x1000;
253     
254     /* Ole Function Description calling convention */
255     public static final int CC_FASTCALL = 0;
256     public static final int CC_CDECL = 1;
257     public static final int CC_MSCPASCAL = 2;
258     public static final int CC_PASCAL = 2;
259     public static final int CC_MACPASCAL = 3;
260     public static final int CC_STDCALL = 4;
261     public static final int CC_FPFASTCALL = 5;
262     public static final int CC_SYSCALL = 6;
263     public static final int CC_MPWCDECL = 7;
264     public static final int CC_MPWPASCAL = 8;
265     public static final int CC_MAX = 9;
266     
267     static final String JavaDoc ERROR_NOT_IMPLEMENTED_MSG = "Required functionality not currently supported.";//$NON-NLS-1$
268
static final String JavaDoc ERROR_CANNOT_CREATE_FILE_MSG = "Failed to create file.";//$NON-NLS-1$
269
static final String JavaDoc ERROR_CANNOT_CREATE_OBJECT_MSG = "Failed to create Ole Client.";//$NON-NLS-1$
270
static final String JavaDoc ERROR_CANNOT_OPEN_FILE_MSG = "File does not exist, is not accessible to user or does not have the correct format.";//$NON-NLS-1$
271
static final String JavaDoc ERROR_INTERFACE_NOT_FOUND_MSG = "Failed to find requested interface on OLE Object.";//$NON-NLS-1$
272
static final String JavaDoc ERROR_INVALID_CLASSID_MSG = "Class ID not found in registry";//$NON-NLS-1$
273
static final String JavaDoc ERROR_CANNOT_ACCESS_CLASSFACTORY_MSG = "Failed to get the class factory for the specified classID";//$NON-NLS-1$
274
static final String JavaDoc ERROR_CANNOT_CREATE_LICENSED_OBJECT_MSG = "Failed to create Licensed instance";//$NON-NLS-1$
275
static final String JavaDoc ERROR_OUT_OF_MEMORY_MSG = "Out of Memory";//$NON-NLS-1$
276
static final String JavaDoc ERROR_CANNOT_CHANGE_VARIANT_TYPE_MSG = "Failed to change Variant type";//$NON-NLS-1$
277
static final String JavaDoc ERROR_INVALID_INTERFACE_ADDRESS_MSG = "Invalid address received for Ole Interface.";//$NON-NLS-1$
278
static final String JavaDoc ERROR_APPLICATION_NOT_FOUND_MSG = "Unable to find Application.";//$NON-NLS-1$
279
static final String JavaDoc ERROR_ACTION_NOT_PERFORMED_MSG = "Action can not be performed.";//$NON-NLS-1$
280

281
282 public static void error (int code) {
283     error (code, 0);
284 }
285 public static void error (int code, int hresult) {
286         
287     switch (code) {
288         /* Illegal Arguments (non-fatal) */
289         case ERROR_INVALID_INTERFACE_ADDRESS :{
290             throw new IllegalArgumentException JavaDoc (ERROR_INVALID_INTERFACE_ADDRESS_MSG);
291         }
292     
293         /* SWT Errors (non-fatal) */
294         case ERROR_CANNOT_CREATE_FILE : {
295             String JavaDoc msg = ERROR_CANNOT_CREATE_FILE_MSG;
296             if (hresult != 0) msg += " result = "+hresult;//$NON-NLS-1$
297
throw new SWTException (code, msg);
298         }
299         case ERROR_CANNOT_CREATE_OBJECT : {
300             String JavaDoc msg = ERROR_CANNOT_CREATE_OBJECT_MSG;
301             if (hresult != 0) msg += " result = "+hresult;//$NON-NLS-1$
302
throw new SWTException (code, msg);//$NON-NLS-1$
303
}
304         case ERROR_CANNOT_OPEN_FILE : {
305             String JavaDoc msg = ERROR_CANNOT_OPEN_FILE_MSG;
306             if (hresult != 0) msg += " result = "+hresult;//$NON-NLS-1$
307
throw new SWTException (code, msg);
308         }
309         case ERROR_INTERFACE_NOT_FOUND : {
310             String JavaDoc msg = ERROR_INTERFACE_NOT_FOUND_MSG;
311             if (hresult != 0) msg += " result = "+hresult;//$NON-NLS-1$
312
throw new SWTException (code, msg);
313         }
314         case ERROR_INVALID_CLASSID : {
315             String JavaDoc msg = ERROR_INVALID_CLASSID_MSG;
316             if (hresult != 0) msg += " result = "+hresult;//$NON-NLS-1$
317
throw new SWTException (code, msg);
318         }
319         case ERROR_CANNOT_ACCESS_CLASSFACTORY : {
320             String JavaDoc msg = ERROR_CANNOT_ACCESS_CLASSFACTORY_MSG;
321             if (hresult != 0) msg += " result = "+hresult;//$NON-NLS-1$
322
throw new SWTException (code, msg);
323         }
324         case ERROR_CANNOT_CREATE_LICENSED_OBJECT : {
325             String JavaDoc msg = ERROR_CANNOT_CREATE_LICENSED_OBJECT_MSG;
326             if (hresult != 0) msg += " result = "+hresult;//$NON-NLS-1$
327
throw new SWTException (code, msg);
328         }
329         case ERROR_CANNOT_CHANGE_VARIANT_TYPE : {
330             String JavaDoc msg = ERROR_CANNOT_CHANGE_VARIANT_TYPE_MSG;
331             if (hresult != 0) msg += " result = "+hresult;//$NON-NLS-1$
332
throw new SWTException (code, msg);
333         }
334         case ERROR_APPLICATION_NOT_FOUND : {
335             String JavaDoc msg = ERROR_APPLICATION_NOT_FOUND_MSG;
336             if (hresult != 0) msg += " result = "+hresult;//$NON-NLS-1$
337
throw new SWTException (code, msg);
338         }
339         case ERROR_ACTION_NOT_PERFORMED : {
340             String JavaDoc msg = ERROR_ACTION_NOT_PERFORMED_MSG;
341             if (hresult != 0) msg += " result = "+hresult;//$NON-NLS-1$
342
throw new SWTException (code, msg);
343         }
344         
345         /* OS Failure/Limit (fatal, may occur only on some platforms) */
346         case ERROR_OUT_OF_MEMORY : {
347             String JavaDoc msg = ERROR_ACTION_NOT_PERFORMED_MSG;
348             if (hresult != 0) msg += " result = "+hresult; //$NON-NLS-1$
349
throw new SWTError (code, msg);
350         }
351     }
352             
353     /* Unknown/Undefined Error */
354     SWT.error(code);
355 }
356
357 /*
358  * Finds the OLE program id that is associated with an extension.
359  * The extension may or may not begin with a '.'. On platforms
360  * that do not support OLE, an empty string is returned.
361  *
362  * @param extension the program extension
363  * @return a string that is the OLE program id or an empty string
364  *
365  * @exception IllegalArgumentException <ul>
366  * <li>ERROR_NULL_ARGUMENT when extension is null</li>
367  * </ul>
368  */

369 public static String JavaDoc findProgramID (String JavaDoc extension) {
370     if (extension == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
371     if (extension.length () == 0) return ""; //$NON-NLS-1$
372

373     if (extension.charAt (0) != '.') extension = "." + extension; //$NON-NLS-1$
374

375     /* Use the character encoding for the default locale */
376     TCHAR extensionKey = new TCHAR(0, extension, true);
377     String JavaDoc result = getKeyValue(extensionKey);
378     if (result != null) {
379         // look for "<programID>\NotInsertable"
380
TCHAR notInsertableKey = new TCHAR(0, result+"\\NotInsertable", true); //$NON-NLS-1$
381
if (getKeyExists(notInsertableKey)) return ""; //$NON-NLS-1$
382
// look for "<programID>\Insertable"
383
TCHAR insertableKey = new TCHAR(0, result+"\\Insertable", true); //$NON-NLS-1$
384
if (getKeyExists(insertableKey)) return result;
385         // look for "<programID>\protocol\StdFileEditing\server"
386
TCHAR serverKey = new TCHAR(0, result+"\\protocol\\StdFileEditing\\server", true); //$NON-NLS-1$
387
if (getKeyExists(serverKey)) return result;
388     }
389     
390     return ""; //$NON-NLS-1$
391
}
392 static String JavaDoc getKeyValue (TCHAR key) {
393     int [] phkResult = new int [1];
394     if (OS.RegOpenKeyEx (OS.HKEY_CLASSES_ROOT, key, 0, OS.KEY_READ, phkResult) != 0) {
395         return null;
396     }
397     String JavaDoc result = null;
398     int [] lpcbData = new int [1];
399     if (OS.RegQueryValueEx (phkResult [0], (TCHAR) null, 0, null, (TCHAR) null, lpcbData) == 0) {
400         int length = lpcbData [0] / TCHAR.sizeof;
401         if (length == 0) {
402             result = "";
403         } else {
404             /* Use the character encoding for the default locale */
405             TCHAR lpData = new TCHAR (0, length);
406             if (OS.RegQueryValueEx (phkResult [0], null, 0, null, lpData, lpcbData) == 0) {
407                 length = Math.max(0, lpData.length () - 1);
408                 result = lpData.toString (0, length);
409             }
410         }
411     }
412     if (phkResult [0] != 0) OS.RegCloseKey (phkResult [0]);
413     return result;
414 }
415 private static boolean getKeyExists (TCHAR key) {
416     int [] phkResult = new int [1];
417     if (OS.RegOpenKeyEx (OS.HKEY_CLASSES_ROOT, key, 0, OS.KEY_READ, phkResult) != 0) {
418         return false;
419     }
420     if (phkResult [0] != 0) OS.RegCloseKey (phkResult [0]);
421     return true;
422 }
423 /**
424  * Returns true if the specified file has an OLE Storage format.
425  *
426  * Note all empty files (regardless of extension) will return false.
427  *
428  * @param file the file to be checked
429  *
430  * @return true if this file has an OLE Storage format
431  */

432 public static boolean isOleFile(File JavaDoc file) {
433     if (file == null || !file.exists() || file.isDirectory())
434         return false;
435
436     return (COM.StgIsStorageFile((file.getAbsolutePath()+"\0").toCharArray()) == COM.S_OK);
437 }
438
439 }
440
Popular Tags