KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > program > Program


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.program;
12
13 import org.eclipse.swt.internal.win32.*;
14 import org.eclipse.swt.*;
15 import org.eclipse.swt.graphics.*;
16
17 /**
18  * Instances of this class represent programs and
19  * their associated file extensions in the operating
20  * system.
21  */

22 public final class Program {
23     String JavaDoc name;
24     String JavaDoc command;
25     String JavaDoc iconName;
26     String JavaDoc extension;
27     static final String JavaDoc [] ARGUMENTS = new String JavaDoc [] {"%1", "%l", "%L"}; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
28

29 /**
30  * Prevents uninitialized instances from being created outside the package.
31  */

32 Program () {
33 }
34
35 static String JavaDoc assocQueryString (int assocStr, TCHAR key, boolean expand) {
36     TCHAR pszOut = new TCHAR(0, 1024);
37     int[] pcchOut = new int[1];
38     pcchOut[0] = pszOut.length();
39     int result = OS.AssocQueryString(OS.ASSOCF_NOTRUNCATE, assocStr, key, null, pszOut, pcchOut);
40     if (result == OS.E_POINTER) {
41         pszOut = new TCHAR(0, pcchOut [0]);
42         result = OS.AssocQueryString(OS.ASSOCF_NOTRUNCATE, assocStr, key, null, pszOut, pcchOut);
43     }
44     if (result == 0) {
45         if (!OS.IsWinCE && expand) {
46             int length = OS.ExpandEnvironmentStrings (pszOut, null, 0);
47             if (length != 0) {
48                 TCHAR lpDst = new TCHAR (0, length);
49                 OS.ExpandEnvironmentStrings (pszOut, lpDst, length);
50                 return lpDst.toString (0, Math.max (0, length - 1));
51             } else {
52                 return "";
53             }
54         } else {
55             return pszOut.toString (0, Math.max (0, pcchOut [0] - 1));
56         }
57     }
58     return null;
59 }
60
61 /**
62  * Finds the program that is associated with an extension.
63  * The extension may or may not begin with a '.'. Note that
64  * a <code>Display</code> must already exist to guarantee that
65  * this method returns an appropriate result.
66  *
67  * @param extension the program extension
68  * @return the program or <code>null</code>
69  *
70  * @exception IllegalArgumentException <ul>
71  * <li>ERROR_NULL_ARGUMENT when extension is null</li>
72  * </ul>
73  */

74 public static Program findProgram (String JavaDoc extension) {
75     if (extension == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
76     if (extension.length () == 0) return null;
77     if (extension.charAt (0) != '.') extension = "." + extension; //$NON-NLS-1$
78
/* Use the character encoding for the default locale */
79     TCHAR key = new TCHAR (0, extension, true);
80     Program program = null;
81     if (OS.IsWinCE) {
82         int [] phkResult = new int [1];
83         if (OS.RegOpenKeyEx (OS.HKEY_CLASSES_ROOT, key, 0, OS.KEY_READ, phkResult) != 0) {
84             return null;
85         }
86         int [] lpcbData = new int [1];
87         int result = OS.RegQueryValueEx (phkResult [0], null, 0, null, (TCHAR) null, lpcbData);
88         if (result == 0) {
89             TCHAR lpData = new TCHAR (0, lpcbData [0] / TCHAR.sizeof);
90             result = OS.RegQueryValueEx (phkResult [0], null, 0, null, lpData, lpcbData);
91             if (result == 0) program = getProgram (lpData.toString (0, lpData.strlen ()), extension);
92         }
93         OS.RegCloseKey (phkResult [0]);
94     } else {
95         String JavaDoc command = assocQueryString (OS.ASSOCSTR_COMMAND, key, true);
96         if (command != null) {
97             String JavaDoc name = null;
98             if (name == null) name = assocQueryString (OS.ASSOCSTR_FRIENDLYDOCNAME, key, false);
99             if (name == null) name = assocQueryString (OS.ASSOCSTR_FRIENDLYAPPNAME, key, false);
100             if (name == null) name = "";
101             String JavaDoc iconName = assocQueryString (OS.ASSOCSTR_DEFAULTICON, key, true);
102             if (iconName == null) iconName = "";
103             program = new Program ();
104             program.name = name;
105             program.command = command;
106             program.iconName = iconName;
107             program.extension = extension;
108         }
109     }
110     return program;
111 }
112
113 /**
114  * Answer all program extensions in the operating system. Note
115  * that a <code>Display</code> must already exist to guarantee
116  * that this method returns an appropriate result.
117  *
118  * @return an array of extensions
119  */

120 public static String JavaDoc [] getExtensions () {
121     String JavaDoc [] extensions = new String JavaDoc [1024];
122     /* Use the character encoding for the default locale */
123     TCHAR lpName = new TCHAR (0, 1024);
124     int [] lpcName = new int [] {lpName.length ()};
125     FILETIME ft = new FILETIME ();
126     int dwIndex = 0, count = 0;
127     while (OS.RegEnumKeyEx (OS.HKEY_CLASSES_ROOT, dwIndex, lpName, lpcName, null, null, null, ft) != OS.ERROR_NO_MORE_ITEMS) {
128         String JavaDoc extension = lpName.toString (0, lpcName [0]);
129         lpcName [0] = lpName.length ();
130         if (extension.length () > 0 && extension.charAt (0) == '.') {
131             if (count == extensions.length) {
132                 String JavaDoc [] newExtensions = new String JavaDoc [extensions.length + 1024];
133                 System.arraycopy (extensions, 0, newExtensions, 0, extensions.length);
134                 extensions = newExtensions;
135             }
136             extensions [count++] = extension;
137         }
138         dwIndex++;
139     }
140     if (count != extensions.length) {
141         String JavaDoc [] newExtension = new String JavaDoc [count];
142         System.arraycopy (extensions, 0, newExtension, 0, count);
143         extensions = newExtension;
144     }
145     return extensions;
146 }
147
148 static String JavaDoc getKeyValue (String JavaDoc string, boolean expand) {
149     /* Use the character encoding for the default locale */
150     TCHAR key = new TCHAR (0, string, true);
151     int [] phkResult = new int [1];
152     if (OS.RegOpenKeyEx (OS.HKEY_CLASSES_ROOT, key, 0, OS.KEY_READ, phkResult) != 0) {
153         return null;
154     }
155     String JavaDoc result = null;
156     int [] lpcbData = new int [1];
157     if (OS.RegQueryValueEx (phkResult [0], (TCHAR) null, 0, null, (TCHAR) null, lpcbData) == 0) {
158         result = "";
159         int length = lpcbData [0] / TCHAR.sizeof;
160         if (length != 0) {
161             /* Use the character encoding for the default locale */
162             TCHAR lpData = new TCHAR (0, length);
163             if (OS.RegQueryValueEx (phkResult [0], null, 0, null, lpData, lpcbData) == 0) {
164                 if (!OS.IsWinCE && expand) {
165                     length = OS.ExpandEnvironmentStrings (lpData, null, 0);
166                     if (length != 0) {
167                         TCHAR lpDst = new TCHAR (0, length);
168                         OS.ExpandEnvironmentStrings (lpData, lpDst, length);
169                         result = lpDst.toString (0, Math.max (0, length - 1));
170                     }
171                 } else {
172                     length = Math.max (0, lpData.length () - 1);
173                     result = lpData.toString (0, length);
174                 }
175             }
176         }
177     }
178     if (phkResult [0] != 0) OS.RegCloseKey (phkResult [0]);
179     return result;
180 }
181
182 static Program getProgram (String JavaDoc key, String JavaDoc extension) {
183
184     /* Name */
185     String JavaDoc name = getKeyValue (key, false);
186     if (name == null || name.length () == 0) {
187         name = key;
188     }
189
190     /* Command */
191     String JavaDoc DEFAULT_COMMAND = "\\shell"; //$NON-NLS-1$
192
String JavaDoc defaultCommand = getKeyValue (key + DEFAULT_COMMAND, true);
193     if (defaultCommand == null || defaultCommand.length() == 0) defaultCommand = "open"; //$NON-NLS-1$
194
String JavaDoc COMMAND = "\\shell\\" + defaultCommand + "\\command"; //$NON-NLS-1$
195
String JavaDoc command = getKeyValue (key + COMMAND, true);
196     if (command == null || command.length () == 0) return null;
197
198     /* Icon */
199     String JavaDoc DEFAULT_ICON = "\\DefaultIcon"; //$NON-NLS-1$
200
String JavaDoc iconName = getKeyValue (key + DEFAULT_ICON, true);
201     if (iconName == null) iconName = ""; //$NON-NLS-1$
202

203     /* Program */
204     Program program = new Program ();
205     program.name = name;
206     program.command = command;
207     program.iconName = iconName;
208     program.extension = extension;
209     return program;
210 }
211
212 /**
213  * Answers all available programs in the operating system. Note
214  * that a <code>Display</code> must already exist to guarantee
215  * that this method returns an appropriate result.
216  *
217  * @return an array of programs
218  */

219 public static Program [] getPrograms () {
220     Program [] programs = new Program [1024];
221     /* Use the character encoding for the default locale */
222     TCHAR lpName = new TCHAR (0, 1024);
223     int [] lpcName = new int [] {lpName.length ()};
224     FILETIME ft = new FILETIME ();
225     int dwIndex = 0, count = 0;
226     while (OS.RegEnumKeyEx (OS.HKEY_CLASSES_ROOT, dwIndex, lpName, lpcName, null, null, null, ft) != OS.ERROR_NO_MORE_ITEMS) {
227         String JavaDoc path = lpName.toString (0, lpcName [0]);
228         lpcName [0] = lpName.length ();
229         Program program = getProgram (path, null);
230         if (program != null) {
231             if (count == programs.length) {
232                 Program [] newPrograms = new Program [programs.length + 1024];
233                 System.arraycopy (programs, 0, newPrograms, 0, programs.length);
234                 programs = newPrograms;
235             }
236             programs [count++] = program;
237         }
238         dwIndex++;
239     }
240     if (count != programs.length) {
241         Program [] newPrograms = new Program [count];
242         System.arraycopy (programs, 0, newPrograms, 0, count);
243         programs = newPrograms;
244     }
245     return programs;
246 }
247
248 /**
249  * Launches the executable associated with the file in
250  * the operating system. If the file is an executable,
251  * then the executable is launched. Note that a <code>Display</code>
252  * must already exist to guarantee that this method returns
253  * an appropriate result.
254  *
255  * @param fileName the file or program name
256  * @return <code>true</code> if the file is launched, otherwise <code>false</code>
257  *
258  * @exception IllegalArgumentException <ul>
259  * <li>ERROR_NULL_ARGUMENT when fileName is null</li>
260  * </ul>
261  */

262 public static boolean launch (String JavaDoc fileName) {
263     if (fileName == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
264     
265     /* Use the character encoding for the default locale */
266     int hHeap = OS.GetProcessHeap ();
267     TCHAR buffer = new TCHAR (0, fileName, true);
268     int byteCount = buffer.length () * TCHAR.sizeof;
269     int lpFile = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, byteCount);
270     OS.MoveMemory (lpFile, buffer, byteCount);
271     SHELLEXECUTEINFO info = new SHELLEXECUTEINFO ();
272     info.cbSize = SHELLEXECUTEINFO.sizeof;
273     info.lpFile = lpFile;
274     info.nShow = OS.SW_SHOW;
275     boolean result = OS.ShellExecuteEx (info);
276     if (lpFile != 0) OS.HeapFree (hHeap, 0, lpFile);
277     return result;
278 }
279
280 /**
281  * Executes the program with the file as the single argument
282  * in the operating system. It is the responsibility of the
283  * programmer to ensure that the file contains valid data for
284  * this program.
285  *
286  * @param fileName the file or program name
287  * @return <code>true</code> if the file is launched, otherwise <code>false</code>
288  *
289  * @exception IllegalArgumentException <ul>
290  * <li>ERROR_NULL_ARGUMENT when fileName is null</li>
291  * </ul>
292  */

293 public boolean execute (String JavaDoc fileName) {
294     if (fileName == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
295     int index = 0;
296     boolean append = true;
297     String JavaDoc prefix = command, suffix = ""; //$NON-NLS-1$
298
while (index < ARGUMENTS.length) {
299         int i = command.indexOf (ARGUMENTS [index]);
300         if (i != -1) {
301             append = false;
302             prefix = command.substring (0, i);
303             suffix = command.substring (i + ARGUMENTS [index].length (), command.length ());
304             break;
305         }
306         index++;
307     }
308     if (append) fileName = " \"" + fileName + "\"";
309     String JavaDoc commandLine = prefix + fileName + suffix;
310     int hHeap = OS.GetProcessHeap ();
311     /* Use the character encoding for the default locale */
312     TCHAR buffer = new TCHAR (0, commandLine, true);
313     int byteCount = buffer.length () * TCHAR.sizeof;
314     int lpCommandLine = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, byteCount);
315     OS.MoveMemory (lpCommandLine, buffer, byteCount);
316     STARTUPINFO lpStartupInfo = new STARTUPINFO ();
317     lpStartupInfo.cb = STARTUPINFO.sizeof;
318     PROCESS_INFORMATION lpProcessInformation = new PROCESS_INFORMATION ();
319     boolean success = OS.CreateProcess (0, lpCommandLine, 0, 0, false, 0, 0, 0, lpStartupInfo, lpProcessInformation);
320     if (lpCommandLine != 0) OS.HeapFree (hHeap, 0, lpCommandLine);
321     if (lpProcessInformation.hProcess != 0) OS.CloseHandle (lpProcessInformation.hProcess);
322     if (lpProcessInformation.hThread != 0) OS.CloseHandle (lpProcessInformation.hThread);
323     return success;
324 }
325
326 /**
327  * Returns the receiver's image data. This is the icon
328  * that is associated with the receiver in the operating
329  * system.
330  *
331  * @return the image data for the program, may be null
332  */

333 public ImageData getImageData () {
334     if (extension != null) {
335         SHFILEINFO shfi = OS.IsUnicode ? (SHFILEINFO) new SHFILEINFOW () : new SHFILEINFOA ();
336         int flags = OS.SHGFI_ICON | OS.SHGFI_SMALLICON | OS.SHGFI_USEFILEATTRIBUTES;
337         TCHAR pszPath = new TCHAR (0, extension, true);
338         OS.SHGetFileInfo (pszPath, OS.FILE_ATTRIBUTE_NORMAL, shfi, SHFILEINFO.sizeof, flags);
339         if (shfi.hIcon != 0) {
340             Image image = Image.win32_new (null, SWT.ICON, shfi.hIcon);
341             ImageData imageData = image.getImageData ();
342             image.dispose ();
343             return imageData;
344         }
345     }
346     int nIconIndex = 0;
347     String JavaDoc fileName = iconName;
348     int index = iconName.indexOf (',');
349     if (index != -1) {
350         fileName = iconName.substring (0, index);
351         String JavaDoc iconIndex = iconName.substring (index + 1, iconName.length ()).trim ();
352         try {
353             nIconIndex = Integer.parseInt (iconIndex);
354         } catch (NumberFormatException JavaDoc e) {}
355     }
356     /* Use the character encoding for the default locale */
357     TCHAR lpszFile = new TCHAR (0, fileName, true);
358     int [] phiconSmall = new int[1], phiconLarge = null;
359     OS.ExtractIconEx (lpszFile, nIconIndex, phiconLarge, phiconSmall, 1);
360     if (phiconSmall [0] == 0) return null;
361     Image image = Image.win32_new (null, SWT.ICON, phiconSmall [0]);
362     ImageData imageData = image.getImageData ();
363     image.dispose ();
364     return imageData;
365 }
366
367 /**
368  * Returns the receiver's name. This is as short and
369  * descriptive a name as possible for the program. If
370  * the program has no descriptive name, this string may
371  * be the executable name, path or empty.
372  *
373  * @return the name of the program
374  */

375 public String JavaDoc getName () {
376     return name;
377 }
378
379 /**
380  * Compares the argument to the receiver, and returns true
381  * if they represent the <em>same</em> object using a class
382  * specific comparison.
383  *
384  * @param other the object to compare with this object
385  * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise
386  *
387  * @see #hashCode()
388  */

389 public boolean equals(Object JavaDoc other) {
390     if (this == other) return true;
391     if (other instanceof Program) {
392         final Program program = (Program) other;
393         return name.equals(program.name) && command.equals(program.command)
394             && iconName.equals(program.iconName);
395     }
396     return false;
397 }
398
399 /**
400  * Returns an integer hash code for the receiver. Any two
401  * objects that return <code>true</code> when passed to
402  * <code>equals</code> must return the same value for this
403  * method.
404  *
405  * @return the receiver's hash
406  *
407  * @see #equals(Object)
408  */

409 public int hashCode() {
410     return name.hashCode() ^ command.hashCode() ^ iconName.hashCode();
411 }
412
413 /**
414  * Returns a string containing a concise, human-readable
415  * description of the receiver.
416  *
417  * @return a string representation of the program
418  */

419 public String JavaDoc toString () {
420     return "Program {" + name + "}"; //$NON-NLS-1$ //$NON-NLS-2$
421
}
422
423 }
424
Popular Tags