1 package org.eclipse.webbrowser.internal;
2 /**********************************************************************
3 * Copyright (c) 2003 IBM Corporation and others.
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Common Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/cpl-v10.html
10 * IBM - Initial API and implementation
11 **********************************************************************/
13 import java.io.IOException;
14 import java.lang.reflect.InvocationTargetException;
15 import java.text.MessageFormat;
16 import java.util.ArrayList;
17 import java.util.List;
18 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
19 import org.eclipse.jface.operation.IRunnableWithProgress;
20 import org.eclipse.swt.widgets.DirectoryDialog;
21 import org.eclipse.swt.widgets.Shell;
22 import org.eclipse.webbrowser.IExternalWebBrowserWorkingCopy;
23 import org.eclipse.core.runtime.IProgressMonitor;
27 public class BrowserSearcher {
28 private static boolean cancelled;
29 private BrowserSearcher() {
34 * Search for installed VMs in the file system
36 protected static List search(Shell shell) {
37 final List foundBrowsers = new ArrayList();
38 final List existingPaths = WebBrowserUtil.getExternalBrowserPaths();
40 // select a target directory for the search
41 DirectoryDialog dialog = new DirectoryDialog(shell);
42 dialog.setMessage(WebBrowserUIPlugin.getResource("%selectDirectory"));
43 dialog.setText(WebBrowserUIPlugin.getResource("%directoryDialogTitle"));
45 String path = dialog.open();
51 final File rootDir = new File(path);
52 ProgressMonitorDialog pm = new ProgressMonitorDialog(shell);
54 IRunnableWithProgress r = new IRunnableWithProgress() {
55 public void run(IProgressMonitor monitor) {
57 WebBrowserUIPlugin.getResource("%searchingTaskName"),
58 IProgressMonitor.UNKNOWN);
59 search(rootDir, existingPaths, foundBrowsers, monitor);
61 if (monitor.isCanceled())
67 pm.run(true, true, r);
68 } catch (InvocationTargetException e) {
69 Trace.trace(Trace.SEVERE, "Invocation Exception occured running monitor: " + e);
70 } catch (InterruptedException e) {
71 Trace.trace(Trace.SEVERE, "Interrupted exception occured running monitor: " + e);
81 protected static void setCancelled(boolean b) {
85 protected static void search(File directory, List existingPaths, List foundBrowsers, IProgressMonitor monitor) {
86 if (monitor.isCanceled())
89 String[] names = directory.list();
90 List subDirs = new ArrayList();
92 for (int i = 0; i < names.length; i++) {
93 if (monitor.isCanceled())
96 File file = new File(directory, names[i]);
98 if (existingPaths.contains(file.getAbsolutePath().toLowerCase()))
101 IExternalWebBrowserWorkingCopy wc = WebBrowserUtil.createExternalBrowser(file);
103 foundBrowsers.add(wc);
107 MessageFormat.format(WebBrowserUIPlugin.getResource("%searching"),
108 new String[] { Integer.toString(foundBrowsers.size()), file.getCanonicalPath()}));
109 } catch (IOException ioe) {
112 if (file.isDirectory()) {
113 if (monitor.isCanceled())
118 while (!subDirs.isEmpty()) {
119 File subDir = (File) subDirs.remove(0);
120 search(subDir, existingPaths, foundBrowsers, monitor);
121 if (monitor.isCanceled()) {