KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.URL JavaDoc;
24
25 import org.jdesktop.jdic.desktop.internal.BrowserService;
26 import org.jdesktop.jdic.desktop.internal.LaunchFailedException;
27
28 /**
29  * Concrete implementation of the interface <code>BrowserService</code> for Windows.
30  *
31  * @see BrowserService
32  * @see GnomeBrowserService
33  */

34 public class WinBrowserService implements BrowserService {
35     /**
36      * Browser name pattern for IE, which are used the check the currently used browser.
37      */

38     private static final String JavaDoc IE_NAME_PATTERN = "iexplore";
39
40     /**
41      * Reserved target names by native function IWebBrowser2::Navigate2().
42      */

43     private static final String JavaDoc[] RESERVED_TARGET_NAMES = {"_top", "_self", "_parent"};
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      * @param target the given target name to be converted.
54      * @return the converted target name according the rules.
55      */

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

90     public void show(URL JavaDoc url) throws LaunchFailedException {
91         boolean findOpenNew = false;
92         //First check if we could find command for verb opennew
93
String JavaDoc verbCmd = WinUtility.getVerbCommand(url, DesktopConstants.VERB_OPENNEW);
94         if (verbCmd != null) {
95             findOpenNew = true;
96         } else {
97             //If no opennew verb command find, then check open verb command
98
verbCmd = WinUtility.getVerbCommand(url, DesktopConstants.VERB_OPEN);
99         }
100         if (verbCmd != null) {
101             boolean result;
102             if (findOpenNew) {
103                 //If there is opennew verb command, use this one
104
result = WinAPIWrapper.WinShellExecute(url.toString(), DesktopConstants.VERB_OPENNEW);
105             } else {
106                 //else use open verb command
107
result = WinAPIWrapper.WinShellExecute(url.toString(), DesktopConstants.VERB_OPEN);
108             }
109             if (!result) {
110                 throw new LaunchFailedException("Failed to launch the default browser");
111             }
112         } else {
113             throw new LaunchFailedException("No default browser associated with this URL");
114         }
115         
116     }
117
118     /**
119      * Invokes the system default browser to display the given URL in the target window.
120      *
121      * @param url the given URL.
122      * @param target the given target browser window name.
123      * @throws LaunchFailedException if the default browser is not found, or the default browser
124      * fails to be launched.
125      */

126     public void show(URL JavaDoc url, String JavaDoc target) throws LaunchFailedException {
127         // Check whether the default browser is IE.
128
String JavaDoc verbCommand = WinUtility.getVerbCommand(url, DesktopConstants.VERB_OPEN);
129         if (verbCommand.toLowerCase().indexOf(IE_NAME_PATTERN) == -1) {
130             throw new LaunchFailedException("The default browser doesn't support targeting URL feature.");
131         } else {
132             // Invoke IE to display the given URL in the given target window.
133
boolean result = WinAPIWrapper.WinBrowseURLInIE(url.toString(), convertTargetName(target));
134             
135             if (!result) {
136                 throw new LaunchFailedException("Failed to invoke the default browser.");
137             }
138         }
139     }
140 }
141
Popular Tags