KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > jdic > browser > internal > WebBrowserUtil


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 package org.jdesktop.jdic.browser.internal;
21
22 import java.io.File JavaDoc;
23
24 /**
25  * Utility class for <code>WebBrowser</code> class.
26  */

27 public class WebBrowserUtil {
28     // Native browser embedding binary: IeEmbed.exe or MozEmbed.exe on Windows,
29
// mozembed-<os>-gtk<version> on Linux/Unix. Which runs as a standalone
30
// native instance.
31
private static final String JavaDoc EMBED_BINARY_WINDOWS_IE
32         = "IeEmbed.exe";
33     private static final String JavaDoc EMBED_BINARY_WINDOWS_MOZILLA
34         = "MozEmbed.exe";
35     private static final String JavaDoc EMBED_BINARY_LINUX_GTK1
36         = "mozembed-linux-gtk1.2";
37     private static final String JavaDoc EMBED_BINARY_LINUX_GTK2
38         = "mozembed-linux-gtk2";
39     private static final String JavaDoc EMBED_BINARY_FREEBSD_GTK1
40         = "mozembed-freebsd-gtk1.2";
41     private static final String JavaDoc EMBED_BINARY_FREEBSD_GTK2
42         = "mozembed-freebsd-gtk2";
43     private static final String JavaDoc EMBED_BINARY_SOLARIS_GTK1
44         = "mozembed-solaris-gtk1.2";
45     private static final String JavaDoc EMBED_BINARY_SOLARIS_GTK2
46         = "mozembed-solaris-gtk2";
47
48     private static String JavaDoc embedBinary;
49     
50     private static String JavaDoc browserPath = null;
51     
52     /* native functions */
53     private static native String JavaDoc nativeGetBrowserPath();
54     private static native String JavaDoc nativeGetMozillaGreHome();
55     
56     // Flag to enable or disable debug message output.
57
private static boolean isDebugOn = false;
58
59     static {
60         System.loadLibrary("jdic");
61     }
62         
63     /**
64      * Returns the name of the native browser embedding binary. If no default
65      * browser is set, null is returned.
66      */

67     public static String JavaDoc getEmbedBinaryName() {
68         if (embedBinary != null && embedBinary.length() > 0)
69             return embedBinary;
70
71         String JavaDoc nativePath = WebBrowserUtil.getBrowserPath();
72         if (null == nativePath) {
73             trace("No default browser is found. " +
74                     "Or environment variable MOZILLA_FIVE_HOME is not set to " +
75                     "a Mozilla binary path if you are on Linux/Unix platform.");
76             return null;
77         }
78
79         String JavaDoc osname = System.getProperty("os.name");
80         if (osname.indexOf("Windows") >= 0) {
81             String JavaDoc windowspath = nativePath;
82             int index = windowspath.indexOf("mozilla.exe");
83             if (index >= 0)
84                 embedBinary = EMBED_BINARY_WINDOWS_MOZILLA;
85             else
86                 embedBinary = EMBED_BINARY_WINDOWS_IE;
87         }
88         else {
89             String JavaDoc libwidgetpath = nativePath + File.separator +
90                                    "components" + File.separator +
91                                    "libwidget_gtk2.so";
92             File JavaDoc file = new File JavaDoc(libwidgetpath);
93             if (!file.exists()) {
94                 if (osname.indexOf("Linux") >= 0) {
95                     embedBinary = EMBED_BINARY_LINUX_GTK1;
96                 }
97                 else if (osname.indexOf("SunOS") >= 0) {
98                     embedBinary = EMBED_BINARY_SOLARIS_GTK1;
99                 }
100                 else if (osname.indexOf("FreeBSD") >= 0) {
101                     embedBinary = EMBED_BINARY_FREEBSD_GTK1;
102                 }
103             }
104             else {
105                 if (osname.indexOf("Linux") >= 0) {
106                     embedBinary = EMBED_BINARY_LINUX_GTK2;
107                 }
108                 else if (osname.indexOf("SunOS") >= 0) {
109                     embedBinary = EMBED_BINARY_SOLARIS_GTK2;
110                 }
111                 else if (osname.indexOf("FreeBSD") >= 0) {
112                     embedBinary = EMBED_BINARY_FREEBSD_GTK2;
113                 }
114             }
115         }
116                 
117         return embedBinary;
118     }
119     
120     /**
121      * Gets the native browser path.
122      * @return the path of the default browser in the current system
123      */

124     public static String JavaDoc getBrowserPath() {
125         if (browserPath == null) {
126             browserPath = nativeGetBrowserPath();
127         }
128         return browserPath;
129     }
130     
131     /**
132      * Checks if the default browser for the current platform is Mozilla.
133      * @return true on Solaris and Linux and true on Windows platform if Mozilla
134      * is set as the default browser.
135      */

136     public static boolean isDefaultBrowserMozilla() {
137         String JavaDoc osName = System.getProperty("os.name").toLowerCase();
138         
139         if ((osName.indexOf("solaris") >= 0) ||
140             (osName.indexOf("linux") >= 0) ) {
141             return true;
142         } else {
143             String JavaDoc nativeBrowserPath = getBrowserPath();
144             // Only when Mozilla is set as the default browser, return true.
145
// Or else, fall back to Internet Explorer.
146
// FireFox 1.0 is statically linked into Gecko and therefore can not
147
// be embedded. If FireFox is embeddable for some future version,
148
// we would have to explicitly check for both Mozilla and FireFox.
149
if (nativeBrowserPath.indexOf("mozilla") >= 0) {
150                 return true;
151             } else {
152                 return false;
153             }
154         }
155     }
156
157     /**
158      * Gets the native Mozilla GRE home directory installed with a .exe package.
159      * @return the GRE home directory of the currently installed Mozilla.
160      */

161     public static String JavaDoc getMozillaGreHome() {
162         return nativeGetMozillaGreHome();
163     }
164
165     public static void enableDebugMessages(boolean b) {
166         isDebugOn = b;
167     }
168     
169     /**
170      * Helper method to output given debug message.
171      *
172      * @param msg the given debug message.
173      */

174     public static void trace(String JavaDoc msg) {
175         if (isDebugOn)
176             System.out.println("*** Jtrace: " + msg);
177     }
178     
179     /*
180      * Sets native environment variables for running native browser binary.
181      */

182     public static native void nativeSetEnv();
183 }
184
Popular Tags