1 20 21 package org.jdesktop.jdic.desktop.internal.impl; 22 23 import java.net.URL ; 24 25 import org.jdesktop.jdic.desktop.internal.BrowserService; 26 import org.jdesktop.jdic.desktop.internal.LaunchFailedException; 27 28 34 public class WinBrowserService implements BrowserService { 35 38 private static final String IE_NAME_PATTERN = "iexplore"; 39 40 43 private static final String [] RESERVED_TARGET_NAMES = {"_top", "_self", "_parent"}; 44 45 56 private String convertTargetName(String 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 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 reversedTarget = new String (reversedChars); 74 75 String convertedTarget = reversedTarget + "?" 77 + reversedTarget + "?" 78 + reversedTarget; 79 return convertedTarget; 80 } 81 } 82 83 90 public void show(URL url) throws LaunchFailedException { 91 boolean findOpenNew = false; 92 String verbCmd = WinUtility.getVerbCommand(url, DesktopConstants.VERB_OPENNEW); 94 if (verbCmd != null) { 95 findOpenNew = true; 96 } else { 97 verbCmd = WinUtility.getVerbCommand(url, DesktopConstants.VERB_OPEN); 99 } 100 if (verbCmd != null) { 101 boolean result; 102 if (findOpenNew) { 103 result = WinAPIWrapper.WinShellExecute(url.toString(), DesktopConstants.VERB_OPENNEW); 105 } else { 106 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 126 public void show(URL url, String target) throws LaunchFailedException { 127 String 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 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 |