KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > jdic > desktop > internal > impl > WinUtility


1 /*
2  * Copyright (C) 2004 Sun Microsystems, Inc. All rights reserved. Use is
3  * subject to license terms.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the Lesser GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  */

20
21 package org.jdesktop.jdic.desktop.internal.impl;
22
23 import java.io.File JavaDoc;
24 import java.net.URL JavaDoc;
25
26
27 /**
28  * WinUtility provides several util funtion for windows platform.
29  *
30  * @version 0.9
31  */

32 public class WinUtility {
33     /**
34      * Suppress default constructor for noninstantiability.
35      */

36     private WinUtility() {}
37
38     /**
39      * Gets the file extension of the given file.
40      * <pre>
41      * For examples:
42      * if the given file is: C:\\test\test.txt, the returned file extension is ".txt".
43      * if the given file is: C:\\test\test, the returned file extension is null.
44      * </pre>
45      *
46      * @param file the given File object, which contains the absolute path of a file.
47      * @return the file extension of the given file.
48      */

49     private static String JavaDoc getFileExtension(File JavaDoc file) {
50         String JavaDoc trimFileStr = file.toString().trim();
51
52         if (trimFileStr == null || trimFileStr == "") {
53             return null;
54         }
55          
56         int strIndex = trimFileStr.lastIndexOf(File.separator);
57         String JavaDoc filePart = trimFileStr.substring(strIndex + 1, trimFileStr.length());
58
59         strIndex = filePart.lastIndexOf(".");
60         if (strIndex == -1 || strIndex == filePart.length() - 1) {
61             return null;
62         } else {
63             String JavaDoc fileExt = filePart.substring(strIndex, filePart.length());
64
65             return fileExt;
66         }
67     }
68     
69     /**
70      * Gets the command string associated with the given file and verb.
71      * <p>
72      * For example: suppose the given file is C:\test\test.txt, and the given verb is "print".
73      * The associated command string is retrieved as "%SystemRoot%\system32\NOTEPAD.EXE /p %1"
74      *
75      * @param file the given file.
76      * @param verb the given verb.
77      * @return the command string in the Registry associated with the given file type
78      * and verb.
79      */

80     public static String JavaDoc getVerbCommand(File JavaDoc file, String JavaDoc verb) {
81         String JavaDoc fileExt = getFileExtension(file);
82         if (fileExt == null) {
83             return null;
84         } else {
85             return(WinAPIWrapper.WinAssocQueryString(fileExt, verb));
86         }
87     }
88
89     /**
90      * Gets the command string associated with the given URL's protocol and verb in the
91      * Registry.
92      * <p>
93      * For example: suppose the given URL is http://www.foo.com, and the given verb is "open".
94      * The associated command string is retrieved as ""C:\Program Files\Internet Explorer\iexplore.exe" -nohome"
95      *
96      * @param url the given URL.
97      * @param verb the given verb.
98      * @return the command string in the Registry associated with the given URL protocol
99      * and verb.
100      */

101     public static String JavaDoc getVerbCommand(URL JavaDoc url, String JavaDoc verb) {
102         String JavaDoc protocolType = url.getProtocol().trim();
103         // For now, this api uses browser to open a file. So replace file protocol
104
// to http protocol.
105
if (protocolType.compareToIgnoreCase("file") == 0) {
106             protocolType = "http";
107         }
108         
109         if (protocolType == null) {
110             return null;
111         } else {
112             return(WinAPIWrapper.WinAssocQueryString(protocolType, verb));
113         }
114     }
115
116     /**
117      * Get the Windows default system mailer.
118      *
119      * @return Name of the default system mailer
120      */

121     static String JavaDoc getDefaultMailer() {
122         String JavaDoc defaultMailer =
123             WinAPIWrapper.WinRegQueryValueEx(
124                 WinAPIWrapper.HKEY_LOCAL_MACHINE,
125                 "SOFTWARE\\Clients\\Mail",
126                 "");
127         return defaultMailer;
128     }
129     
130     /**
131      * Get the mozilla mailer location.
132      *
133      * @param defMailer Name of the system default mailer
134      * This mailer is either "Mozilla" or "Mozilla Thunderbird"
135      * @return Location of the mozilla mailer
136      */

137     static String JavaDoc getMozMailerLocation(String JavaDoc defMailer) {
138         String JavaDoc mailerPath = WinAPIWrapper.WinRegQueryValueEx(
139                 WinAPIWrapper.HKEY_LOCAL_MACHINE,
140                 "SOFTWARE\\Clients\\Mail\\"+defMailer+"\\shell\\open\\command",
141                 "");
142         int lastSpace = mailerPath.lastIndexOf(' ');
143         return mailerPath.substring(0, lastSpace);
144     }
145     
146     /**
147      * Check if the simple MAPI is supported in the current system.
148      *
149      * @return true if simple MAPI is supported.
150      */

151     static boolean isMapiSupported() {
152         String JavaDoc regMapi =
153             WinAPIWrapper.WinRegQueryValueEx(
154                 WinAPIWrapper.HKEY_LOCAL_MACHINE,
155                 "SOFTWARE\\Microsoft\\Windows Messaging Subsystem",
156                 "MAPI");
157         if(regMapi != null) {
158             if(regMapi.equals("1")) {
159                 return true;
160             }
161         }
162         return false;
163     }
164 }
Popular Tags