make compilable in 3.2
[phpeclipse.git] / net.sourceforge.phpeclipse.webbrowser / src / net / sourceforge / phpeclipse / webbrowser / internal / BrowserSearcher.java
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
8   *
9  * Contributors:
10  *    IBM - Initial API and implementation
11  **********************************************************************/
12 import java.io.File;
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
19 import net.sourceforge.phpeclipse.webbrowser.IExternalWebBrowserWorkingCopy;
20
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
23 import org.eclipse.jface.operation.IRunnableWithProgress;
24 import org.eclipse.swt.widgets.DirectoryDialog;
25 import org.eclipse.swt.widgets.Shell;
26 /**
27  * 
28  */
29 public class BrowserSearcher {
30         private static boolean cancelled;
31         private BrowserSearcher() {
32                 super();
33         }
34
35         /**
36          * Search for installed VMs in the file system
37          */
38         protected static List search(Shell shell) {
39                 final List foundBrowsers = new ArrayList();
40                 final List existingPaths = WebBrowserUtil.getExternalBrowserPaths();
41
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"));
46
47                 String path = dialog.open();
48                 if (path == null)
49                         return null;
50                 
51                 cancelled = false;
52                 
53                 final File rootDir = new File(path);
54                 ProgressMonitorDialog pm = new ProgressMonitorDialog(shell);
55
56                 IRunnableWithProgress r = new IRunnableWithProgress() {
57                         public void run(IProgressMonitor monitor) {
58                                 monitor.beginTask(
59                                         WebBrowserUIPlugin.getResource("%searchingTaskName"),
60                                         IProgressMonitor.UNKNOWN);
61                                 search(rootDir, existingPaths, foundBrowsers, monitor);
62                                 monitor.done();
63                                 if (monitor.isCanceled())
64                                         setCancelled(true);
65                         }
66                 };
67
68                 try {
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);
74                         return null;
75                 }
76                 
77                 if (cancelled)
78                         return null;
79
80                 return foundBrowsers;
81         }
82         
83         protected static void setCancelled(boolean b) {
84                 cancelled = b;
85         }
86
87         protected static void search(File directory, List existingPaths, List foundBrowsers, IProgressMonitor monitor) {
88                 if (monitor.isCanceled())
89                         return;
90
91                 String[] names = directory.list();
92                 List subDirs = new ArrayList();
93
94                 for (int i = 0; i < names.length; i++) {
95                         if (monitor.isCanceled())
96                                 return;
97
98                         File file = new File(directory, names[i]);
99                         
100                         if (existingPaths.contains(file.getAbsolutePath().toLowerCase()))
101                                 continue;
102
103                         IExternalWebBrowserWorkingCopy wc = WebBrowserUtil.createExternalBrowser(file);
104                         if (wc != null)
105                                 foundBrowsers.add(wc);
106
107                         try {
108                                 monitor.subTask(
109                                         MessageFormat.format(WebBrowserUIPlugin.getResource("%searching"),
110                                                 new String[] { Integer.toString(foundBrowsers.size()), file.getCanonicalPath()}));
111                         } catch (IOException ioe) {
112                         }
113
114                         if (file.isDirectory()) {
115                                 if (monitor.isCanceled())
116                                         return;
117                                 subDirs.add(file);
118                         }
119                 }
120                 while (!subDirs.isEmpty()) {
121                         File subDir = (File) subDirs.remove(0);
122                         search(subDir, existingPaths, foundBrowsers, monitor);
123                         if (monitor.isCanceled()) {
124                                 return;
125                         }
126                 }
127         }
128 }