1 11 package org.eclipse.ui.internal.browser; 12 13 import java.io.File ; 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.swt.program.Program; 19 import org.eclipse.swt.widgets.Display; 20 import org.eclipse.swt.widgets.Shell; 21 import org.eclipse.core.runtime.IStatus; 22 import org.eclipse.core.runtime.Platform; 23 import org.eclipse.core.runtime.Status; 24 import org.eclipse.jface.dialogs.MessageDialog; 25 import org.eclipse.swt.SWT; 26 import org.eclipse.swt.browser.Browser; 27 import org.eclipse.ui.PlatformUI; 28 31 public class WebBrowserUtil { 32 private static final String BROWSER_PACKAGE_NAME = "org.eclipse.swt.browser.Browser"; 34 public static Boolean isInternalBrowserOperational; 35 36 private static final char STYLE_SEP = '-'; 37 38 private static final int DEFAULT_STYLE = BrowserViewer.BUTTON_BAR 39 | BrowserViewer.LOCATION_BAR; 40 41 44 public WebBrowserUtil() { 45 super(); 46 } 47 48 53 public static boolean isWindows() { 54 String os = System.getProperty("os.name"); if (os != null && os.toLowerCase().indexOf("win") >= 0) return true; 57 return false; 58 } 59 60 65 public static boolean isLinux() { 66 String os = System.getProperty("os.name"); if (os != null && os.toLowerCase().indexOf("lin") >= 0) return true; 69 return false; 70 } 71 72 78 public static void openError(String message) { 79 Display d = Display.getCurrent(); 80 if (d == null) 81 d = Display.getDefault(); 82 83 Shell shell = d.getActiveShell(); 84 MessageDialog.openError(shell, Messages.errorDialogTitle, message); 85 } 86 87 93 public static void openMessage(String message) { 94 Display d = Display.getCurrent(); 95 if (d == null) 96 d = Display.getDefault(); 97 98 Shell shell = d.getActiveShell(); 99 MessageDialog.openInformation(shell, Messages.searchingTaskName, 100 message); 101 } 102 103 119 public static boolean canUseInternalWebBrowser() { 120 if (isInternalBrowserOperational != null) 122 return isInternalBrowserOperational.booleanValue(); 123 124 try { 126 Class.forName(BROWSER_PACKAGE_NAME); 127 } catch (ClassNotFoundException e) { 128 isInternalBrowserOperational = new Boolean (false); 129 return false; 130 } 131 132 Shell shell = null; 134 try { 135 shell = new Shell(PlatformUI.getWorkbench().getDisplay()); 136 new Browser(shell, SWT.NONE); 137 isInternalBrowserOperational = new Boolean (true); 138 return true; 139 } catch (Throwable t) { 140 WebBrowserUIPlugin.getInstance().getLog().log( 141 new Status(IStatus.WARNING, WebBrowserUIPlugin.PLUGIN_ID, 142 0, "Internal browser is not available: " + t.getMessage(), null)); isInternalBrowserOperational = new Boolean (false); 144 return false; 145 } finally { 146 if (shell != null) 147 shell.dispose(); 148 } 149 } 150 151 public static boolean canUseSystemBrowser() { 152 if (Platform.OS_SOLARIS.equals(Platform.getOS())) 154 return false; 155 return Program.findProgram("html") != null; } 157 158 public static List getExternalBrowserPaths() { 159 List paths = new ArrayList (); 160 Iterator iterator = BrowserManager.getInstance().getWebBrowsers() 161 .iterator(); 162 while (iterator.hasNext()) { 163 IBrowserDescriptor wb = (IBrowserDescriptor) iterator.next(); 164 if (wb != null && wb.getLocation() != null) 165 paths.add(wb.getLocation().toLowerCase()); 166 } 167 return paths; 168 } 169 170 174 public static void addFoundBrowsers(List list) { 175 List paths = getExternalBrowserPaths(); 176 177 String os = Platform.getOS(); 178 File [] roots = getUsableDrives(File.listRoots()); 179 int rootSize = roots.length; 180 181 183 IBrowserExt[] browsers = WebBrowserUIPlugin.getBrowsers(); 184 int size = browsers.length; 185 for (int i = 0; i < size; i++) { 186 if (browsers[i].getDefaultLocations() != null 187 && browsers[i].getOS().toLowerCase().indexOf(os) >= 0) { 188 for (int k = 0; k < rootSize; k++) { 189 int size2 = browsers[i].getDefaultLocations().length; 190 for (int j = 0; j < size2; j++) { 191 String location = browsers[i].getDefaultLocations()[j]; 192 try { 193 File f = new File (roots[k], location); 194 if (!paths.contains(f.getAbsolutePath() 195 .toLowerCase())) { 196 if (f.exists()) { 197 BrowserDescriptor browser = new BrowserDescriptor(); 198 browser.name = browsers[i].getName(); 199 browser.location = f.getAbsolutePath(); 200 browser.parameters = browsers[i] 201 .getParameters(); 202 list.add(browser); 203 j += size2; 204 } 205 } 206 } catch (Exception e) { 207 } 209 } 210 } 211 } 212 } 213 } 214 215 private static File [] getUsableDrives(File [] roots) { 216 if (!Platform.getOS().equals(Platform.OS_WIN32)) 217 return roots; 218 ArrayList list = new ArrayList (); 219 for (int i = 0; i < roots.length; i++) { 220 String path = roots[i].getAbsolutePath(); 221 if (path != null 222 && (path.toLowerCase().startsWith("a:") || path.toLowerCase().startsWith("b:"))) continue; 224 list.add(roots[i]); 225 } 226 return (File []) list.toArray(new File [list.size()]); 227 } 228 229 236 public static IBrowserDescriptorWorkingCopy createExternalBrowser(File file) { 237 if (file == null || !file.isFile()) 238 return null; 239 240 String executable = file.getName(); 241 IBrowserExt[] browsers = WebBrowserUIPlugin.getBrowsers(); 242 int size = browsers.length; 243 for (int i = 0; i < size; i++) { 244 if (executable.equals(browsers[i].getExecutable())) { 245 IBrowserDescriptorWorkingCopy browser = BrowserManager 246 .getInstance().createExternalWebBrowser(); 247 browser.setName(browsers[i].getName()); 248 browser.setLocation(file.getAbsolutePath()); 249 browser.setParameters(browsers[i].getParameters()); 250 return browser; 251 } 252 } 253 254 return null; 255 } 256 257 264 public static String encodeStyle(String browserId, int style) { 265 return browserId + STYLE_SEP + style; 266 } 267 268 274 public static int decodeStyle(String secondaryId) { 275 if (secondaryId != null) { 276 int sep = secondaryId.lastIndexOf(STYLE_SEP); 277 if (sep != -1) { 278 String stoken = secondaryId.substring(sep + 1); 279 try { 280 return Integer.parseInt(stoken); 281 } catch (NumberFormatException e) { 282 } 284 } 285 } 286 return DEFAULT_STYLE; 287 } 288 289 public static String decodeId(String encodedId) { 290 int sep = encodedId.lastIndexOf(STYLE_SEP); 291 if (sep != -1) { 292 return encodedId.substring(0, sep); 293 } 294 return encodedId; 295 } 296 } | Popular Tags |