KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
24 import java.net.URL JavaDoc;
25
26 import org.jdesktop.jdic.desktop.internal.BrowserService;
27 import org.jdesktop.jdic.desktop.internal.LaunchFailedException;
28
29 /**
30  * Concrete implementation of the BrowserService interface for Gnome.
31  */

32 public class GnomeBrowserService implements BrowserService {
33     static {
34         System.loadLibrary("jdic");
35     }
36
37     // Browser name pattern for MOZILLA, which are used the check the currently used browser.
38
private static final String JavaDoc MOZILLA_NAME_PATTERN = "mozilla";
39
40     /**
41      * Reserved target names, which are used by Mozilla itself.
42      */

43     private static final String JavaDoc[] RESERVED_TARGET_NAMES = {"_blank", "new-window", "new-tab"};
44     
45     /**
46      * Converts the given target name if it happens to be one of the reserved target names.
47      * <p>
48      * First reverse the name, and then triple it with character '?' as the seperator.
49      * <p>
50      * Notice: Though this conversion rule tries to make the converted string couldn't
51      * be used by the use in later calls, it couldn't be completely avoided.
52      */

53     private String JavaDoc convertTargetName(String JavaDoc target) {
54         boolean isTargetNameReserved = false;
55         for (int index = 0; index < RESERVED_TARGET_NAMES.length; index++) {
56             if (target.equals(RESERVED_TARGET_NAMES[index])) {
57                 isTargetNameReserved = true;
58                 break;
59             }
60         }
61
62         if (!isTargetNameReserved) {
63             return target;
64         } else {
65             if (target.equals("_blank")) {
66                 return "new-window";
67             } else {
68                 // Target name is "new-window" or "new-tab".
69
// First, reverse the target name.
70
char reversedChars[] = new char[target.length()];
71                 for (int i = 0; i < target.length(); i++)
72                     reversedChars[i] = target.charAt(target.length() - i - 1);
73
74                 String JavaDoc reversedTarget = new String JavaDoc(reversedChars);
75
76                 // Then, triple the reversed name with '?' as seprators.
77
String JavaDoc convertedTarget = reversedTarget + "?"
78                                          + reversedTarget + "?"
79                                          + reversedTarget;
80                 return convertedTarget;
81             }
82         }
83     }
84
85     /**
86      * Invokes the system default browser to display the given URL.
87      *
88      * @param url the given URL.
89      * @throws LaunchFailedException if the default browser is not found, or the default browser
90      * fails to be launched.
91      */

92     public void show(URL JavaDoc url) throws LaunchFailedException {
93         if (!nativeBrowseURL(url.toString())) {
94             throw new LaunchFailedException("Failed to launch the default browser.");
95         }
96     }
97
98     /**
99      * Invokes the system default browser to display the given URL in the target window.
100      *
101      * @param url the given URL.
102      * @param target the given browser target window name.
103      * @throws LaunchFailedException if the default browser is not found, or the default browser
104      * fails to be launched.
105      */

106     public void show(URL JavaDoc url, String JavaDoc target) throws LaunchFailedException {
107         // !!! Notice: for now, on Unix, only mozilla version 1.5 or later support the
108
// targeting URL feature. Here we use mozilla as the default browser. Actually,
109
// this may need to be retrieved from gconf registry.
110

111         String JavaDoc mozillaPath = MOZILLA_NAME_PATTERN;
112         
113         boolean result = browseURLInMozilla(mozillaPath, url, target);
114         if (!result) {
115             throw new LaunchFailedException("Failed to launch mozilla.");
116         }
117     }
118     
119     /**
120      * Invokes Mozilla to display the given URL in the given target window.
121      *
122      * @param browserPath the absolute path of the browser exectuable.
123      * @param url the given url to be browsed.
124      * @param target the given target browser window to show the given url
125      */

126     public boolean browseURLInMozilla(String JavaDoc browserPath, URL JavaDoc url, String JavaDoc target) {
127         String JavaDoc MOZILLA_REQUIRED_VERSION_NUMBER = "1.4";
128         String JavaDoc urlStr = url.toString();
129         try {
130             String JavaDoc verNum = GnomeUtility.getMozillaVersionNumber(browserPath);
131             if (verNum == null || !(verNum.compareToIgnoreCase(MOZILLA_REQUIRED_VERSION_NUMBER) > 0)) {
132                 // The targeting frame feature is not supported by this version of Mozilla.
133
return false;
134             } else {
135                 // Check whether there is running Mozilla instance.
136
if (!GnomeUtility.isMozillaRunning(browserPath)) {
137                       return false;
138                 }
139
140                 // There is already running Mozilla instance.
141
// Start mozilla using: mozilla -remote openurl(url, target).
142
String JavaDoc convertedTargetName = convertTargetName(target);
143                 Runtime.getRuntime().exec(new String JavaDoc[] {
144                     browserPath, "-remote", "openurl(" + urlStr + "," + convertedTargetName + ")" });
145                 
146                 return true;
147             }
148         } catch (IOException JavaDoc e) {
149             return false;
150         }
151     }
152     
153     private native boolean nativeBrowseURL(String JavaDoc urlStr);
154 }
155
Popular Tags