1 package net.sourceforge.phpeclipse.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;
19 import net.sourceforge.phpeclipse.webbrowser.IExternalWebBrowserWorkingCopy;
21 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
22 import org.eclipse.jface.operation.IRunnableWithProgress;
23 import org.eclipse.swt.widgets.DirectoryDialog;
24 import org.eclipse.swt.widgets.Shell;
25 import org.eclipse.core.runtime.IProgressMonitor;
29 public class BrowserSearcher {
30 private static boolean cancelled;
31 private BrowserSearcher() {
36 * Search for installed VMs in the file system
38 protected static List search(Shell shell) {
39 final List foundBrowsers = new ArrayList();
40 final List existingPaths = WebBrowserUtil.getExternalBrowserPaths();
42 // select a target directory for the search
43 DirectoryDialog dialog = new DirectoryDialog(shell);
44 dialog.setMessage(WebBrowserUIPlugin.getResource("%selectDirectory"));
45 dialog.setText(WebBrowserUIPlugin.getResource("%directoryDialogTitle"));
47 String path = dialog.open();
53 final File rootDir = new File(path);
54 ProgressMonitorDialog pm = new ProgressMonitorDialog(shell);
56 IRunnableWithProgress r = new IRunnableWithProgress() {
57 public void run(IProgressMonitor monitor) {
59 WebBrowserUIPlugin.getResource("%searchingTaskName"),
60 IProgressMonitor.UNKNOWN);
61 search(rootDir, existingPaths, foundBrowsers, monitor);
63 if (monitor.isCanceled())
69 pm.run(true, true, r);
70 } catch (InvocationTargetException e) {
71 Trace.trace(Trace.SEVERE, "Invocation Exception occured running monitor: " + e);
72 } catch (InterruptedException e) {
73 Trace.trace(Trace.SEVERE, "Interrupted exception occured running monitor: " + e);
83 protected static void setCancelled(boolean b) {
87 protected static void search(File directory, List existingPaths, List foundBrowsers, IProgressMonitor monitor) {
88 if (monitor.isCanceled())
91 String[] names = directory.list();
92 List subDirs = new ArrayList();
94 for (int i = 0; i < names.length; i++) {
95 if (monitor.isCanceled())
98 File file = new File(directory, names[i]);
100 if (existingPaths.contains(file.getAbsolutePath().toLowerCase()))
103 IExternalWebBrowserWorkingCopy wc = WebBrowserUtil.createExternalBrowser(file);
105 foundBrowsers.add(wc);
109 MessageFormat.format(WebBrowserUIPlugin.getResource("%searching"),
110 new String[] { Integer.toString(foundBrowsers.size()), file.getCanonicalPath()}));
111 } catch (IOException ioe) {
114 if (file.isDirectory()) {
115 if (monitor.isCanceled())
120 while (!subDirs.isEmpty()) {
121 File subDir = (File) subDirs.remove(0);
122 search(subDir, existingPaths, foundBrowsers, monitor);
123 if (monitor.isCanceled()) {