/** * Computes the full command-line that can be passed directly to {@link ProcessBuilder}. */ public static List<String> computeCommandLine(IBrowserDescriptor browser, String url) { String params = browser.getParameters(); if (params == null) { params = ""; } boolean paramsHasUrlVariable = params.contains("%URL%"); if (paramsHasUrlVariable) { // Quote the URL in case it has spaces params = params.replaceAll("%URL%", '"' + url + '"'); } final List<String> command = LaunchConfigurationProcessorUtilities.parseArgs(params); command.add(0, browser.getLocation()); if (!paramsHasUrlVariable) { // Cover the case where the user forgot the %URL% command.add(url); } return command; }
/** * Creates and saves a browser model object. * * @param browserName * the name of the browser * @param browserLocation * the location, unescaped * @return the new object */ public static IBrowserDescriptor createBrowserDescriptor(String browserName, String browserLocation) { assert browserName != null; assert browserLocation != null; IBrowserDescriptorWorkingCopy browserWc = BrowserManager.getInstance().createExternalWebBrowser(); browserWc.setName(browserName); if (Platform.OS_MACOSX.equals(Platform.getOS())) { // Special handling necessary to get smooth launches on Mac browserWc.setLocation("/usr/bin/open"); browserWc.setParameters("-a \"" + browserLocation + "\" %URL%"); } else { browserWc.setLocation(browserLocation); browserWc.setParameters("%URL%"); } return browserWc.save(); }
/** * @return a browser that looks like Chrome, or null * @see #findChromeBrowserName() */ @SuppressWarnings("unchecked") public static IBrowserDescriptor findChromeBrowser() { BrowserManager browserManager = BrowserManager.getInstance(); List<IBrowserDescriptor> browsers = new ArrayList<IBrowserDescriptor>(browserManager.getWebBrowsers()); // Give the default web browser the highest priority IBrowserDescriptor currentWebBrowser = browserManager.getCurrentWebBrowser(); if (currentWebBrowser != null) { browsers.remove(currentWebBrowser); browsers.add(0, currentWebBrowser); } for (IBrowserDescriptor browser : browsers) { if (containsChromeRelatedString(browser.getName()) || containsChromeRelatedString(browser.getLocation())) { return browser; } } return null; }
@SuppressWarnings("unchecked") public static IBrowserDescriptor findBrowser(String browserName) { List<IBrowserDescriptor> browsers = BrowserManager.getInstance().getWebBrowsers(); for (IBrowserDescriptor browser : browsers) { if (browser.getName().equals(browserName)) { return browser; } } return null; }
public static List<String> getBrowserNames() { List<String> browserNames = new ArrayList<String>(); for (Object browserObj : BrowserManager.getInstance().getWebBrowsers()) { IBrowserDescriptor browser = (IBrowserDescriptor) browserObj; browserNames.add(browser.getName()); } return browserNames; }
/** * Launches the browser with the given name. This method does not use the Eclipse browser methods to launch the * browser since they do not properly pass quoted strings as a single argument. */ public static void launchBrowser(String browserName, String url) throws CoreException, IOException { IBrowserDescriptor browser = findBrowser(browserName); if (browser == null) { throw new CoreException( StatusUtilities.newErrorStatus("Could not find browser \"" + browserName + "\".", CorePlugin.PLUGIN_ID)); } // SystemBrowserDescriptors have no info in them... if (browser instanceof SystemBrowserDescriptor) { Program p = Program.findProgram("html"); boolean launched = false; if (p != null) { launched = p.execute(url); } if (!launched) { String msg = "Could not launch the default " + "browser, please configure a browser in " + "Preferences -> General -> Web Browsers"; MessageBox mb = new MessageBox(Display.getCurrent().getActiveShell()); mb.setMessage(msg); mb.open(); throw new CoreException(StatusUtilities.newErrorStatus(msg, CorePlugin.PLUGIN_ID)); } } else { List<String> command = computeCommandLine(browser, url); new ProcessBuilder(command).start(); } }
/** * Opens the dialog for the user to choose a browser. * * @param initialSelection optional * @return the selected browser, or <code>null</code> if the dialog was * canceled */ public static IBrowserDescriptor chooseBrowser(Shell shell, IBrowserDescriptor initialSelection) { ElementListSelectionDialog dialog = new BrowserSelectionDialog(shell, initialSelection); if (dialog.open() == Window.OK) { return (IBrowserDescriptor) dialog.getFirstResult(); } return null; }
private BrowserSelectionDialog(Shell parent, IBrowserDescriptor initialSelection) { super(parent, new BrowserLabelProvider()); setTitle("Browser Selection"); setMessage("Choose a browser:"); List<IBrowserDescriptor> browers = getBrowsers(); setElements(browers.toArray(new IBrowserDescriptor[browers.size()])); setHelpAvailable(false); if (initialSelection != null) { setInitialSelections(new Object[] {initialSelection}); } }
@Override protected void buttonPressed(int buttonId) { super.buttonPressed(buttonId); if (buttonId == ADD_BROWSER_ID) { new AddBrowserDialog(getShell()).open(); IBrowserDescriptor[] elements = getBrowsers().toArray( new IBrowserDescriptor[0]); setElements(elements); setListElements(elements); } }
/** * @return the name for a Chrome browser or empty string * @see #findChromeBrowser() */ public static String findChromeBrowserName() { IBrowserDescriptor browser = findChromeBrowser(); return browser != null ? browser.getName() : ""; }
public String getText(Object element) { IBrowserDescriptor browser = (IBrowserDescriptor) element; return browser.getName(); }
@SuppressWarnings("unchecked") private static List<IBrowserDescriptor> getBrowsers() { return BrowserManager.getInstance().getWebBrowsers(); }
public BrowserSelectionBlock(IBrowserDescriptor browser, Listener listener) { super("Browser:", "&Browse..."); this.browser = browser; this.listener = listener; }
public IBrowserDescriptor getBrowser() { return browser; }
@Override protected String onButtonClicked() { IBrowserDescriptor selectedBrowser = BrowserSelectionDialog.chooseBrowser( getShell(), browser); return selectedBrowser != null ? selectedBrowser.getName() : null; }
private void setBrowserText(IBrowserDescriptor browser) { setText(browser != null ? browser.getName() : ""); }
public Collection<BrowserInfo> searchMoreBrowsers() { org.eclipse.ui.internal.browser.BrowserManager eclipseBrowserManager = org.eclipse.ui.internal.browser.BrowserManager .getInstance(); List<IBrowserDescriptor> webBrowsers = eclipseBrowserManager.getWebBrowsers(); Set<String> currentBrowsers = new HashSet<String>(); for (IBrowserDescriptor iBrowserDescriptor : webBrowsers) { String path = getRealPath(iBrowserDescriptor.getLocation()); if (path == null) { continue; } currentBrowsers.add(path); } Collection<BrowserInfo> browsersFound = new ArrayList<BrowserInfo>(); List<BrowserInfo> discoverInstalledBrowsers = BrowserUtil.discoverInstalledBrowsers(); boolean needSave = false; for (BrowserInfo browserInfo : discoverInstalledBrowsers) { String browserLocation = getRealPath(browserInfo.getLocation()); if (browserLocation != null && !currentBrowsers.contains(browserLocation)) { currentBrowsers.add(browserLocation); BrowserDescriptorWorkingCopy workingCopy = new BrowserDescriptorWorkingCopy(); workingCopy.setName(browserInfo.getName()); workingCopy.setLocation(browserInfo.getLocation()); workingCopy.save(); browsersFound.add(browserInfo); needSave = true; } } if (needSave) { // forces a save on the new list of browsers eclipseBrowserManager.setCurrentWebBrowser(eclipseBrowserManager.getCurrentWebBrowser()); } return browsersFound; }
public BrowserInfo getCurrentWebBrowser() { IBrowserDescriptor currentWebBrowser = org.eclipse.ui.internal.browser.BrowserManager.getInstance() .getCurrentWebBrowser(); return new BrowserInfo(currentWebBrowser.getName(), currentWebBrowser.getLocation()); }
void browserSelected(IBrowserDescriptor browserDescriptor, IStatus status);