1 package net.sourceforge.phpeclipse.webbrowser.internal;
 
   3 /**********************************************************************
 
   4  * Copyright (c) 2003 IBM Corporation and others.
 
   5  * All rights reserved. This program and the accompanying materials
 
   6  * are made available under the terms of the Common Public License v1.0
 
   7  * which accompanies this distribution, and is available at
 
   8  * http://www.eclipse.org/legal/cpl-v10.html
 
  11  *    IBM - Initial API and implementation
 
  12  **********************************************************************/
 
  14 import java.io.IOException;
 
  15 import java.lang.reflect.InvocationTargetException;
 
  16 import java.text.MessageFormat;
 
  17 import java.util.ArrayList;
 
  18 import java.util.List;
 
  20 import net.sourceforge.phpeclipse.webbrowser.IExternalWebBrowserWorkingCopy;
 
  22 import org.eclipse.core.runtime.IProgressMonitor;
 
  23 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
 
  24 import org.eclipse.jface.operation.IRunnableWithProgress;
 
  25 import org.eclipse.swt.widgets.DirectoryDialog;
 
  26 import org.eclipse.swt.widgets.Shell;
 
  31 public class BrowserSearcher {
 
  32         private static boolean cancelled;
 
  34         private BrowserSearcher() {
 
  39          * Search for installed VMs in the file system
 
  41         protected static List search(Shell shell) {
 
  42                 final List foundBrowsers = new ArrayList();
 
  43                 final List existingPaths = WebBrowserUtil.getExternalBrowserPaths();
 
  45                 // select a target directory for the search
 
  46                 DirectoryDialog dialog = new DirectoryDialog(shell);
 
  47                 dialog.setMessage(WebBrowserUIPlugin.getResource("%selectDirectory"));
 
  48                 dialog.setText(WebBrowserUIPlugin.getResource("%directoryDialogTitle"));
 
  50                 String path = dialog.open();
 
  56                 final File rootDir = new File(path);
 
  57                 ProgressMonitorDialog pm = new ProgressMonitorDialog(shell);
 
  59                 IRunnableWithProgress r = new IRunnableWithProgress() {
 
  60                         public void run(IProgressMonitor monitor) {
 
  61                                 monitor.beginTask(WebBrowserUIPlugin
 
  62                                                 .getResource("%searchingTaskName"),
 
  63                                                 IProgressMonitor.UNKNOWN);
 
  64                                 search(rootDir, existingPaths, foundBrowsers, monitor);
 
  66                                 if (monitor.isCanceled())
 
  72                         pm.run(true, true, r);
 
  73                 } catch (InvocationTargetException e) {
 
  74                         Trace.trace(Trace.SEVERE,
 
  75                                         "Invocation Exception occured running monitor: " + e);
 
  76                 } catch (InterruptedException e) {
 
  77                         Trace.trace(Trace.SEVERE,
 
  78                                         "Interrupted exception occured running monitor: " + e);
 
  88         protected static void setCancelled(boolean b) {
 
  92         protected static void search(File directory, List existingPaths,
 
  93                         List foundBrowsers, IProgressMonitor monitor) {
 
  94                 if (monitor.isCanceled())
 
  97                 String[] names = directory.list();
 
  98                 List subDirs = new ArrayList();
 
 100                 for (int i = 0; i < names.length; i++) {
 
 101                         if (monitor.isCanceled())
 
 104                         File file = new File(directory, names[i]);
 
 106                         if (existingPaths.contains(file.getAbsolutePath().toLowerCase()))
 
 109                         IExternalWebBrowserWorkingCopy wc = WebBrowserUtil
 
 110                                         .createExternalBrowser(file);
 
 112                                 foundBrowsers.add(wc);
 
 115                                 monitor.subTask(MessageFormat.format(WebBrowserUIPlugin
 
 116                                                 .getResource("%searching"), new String[] {
 
 117                                                 Integer.toString(foundBrowsers.size()),
 
 118                                                 file.getCanonicalPath() }));
 
 119                         } catch (IOException ioe) {
 
 122                         if (file.isDirectory()) {
 
 123                                 if (monitor.isCanceled())
 
 128                 while (!subDirs.isEmpty()) {
 
 129                         File subDir = (File) subDirs.remove(0);
 
 130                         search(subDir, existingPaths, foundBrowsers, monitor);
 
 131                         if (monitor.isCanceled()) {