1) Reintroduced finishedBuilding
[phpeclipse.git] / net.sourceforge.phpeclipse.webbrowser / src / net / sourceforge / phpeclipse / webbrowser / internal / BrowserSearcher.java
1 package net.sourceforge.phpeclipse.webbrowser.internal;
2
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
9  �*
10  * Contributors:
11  *    IBM - Initial API and implementation
12  **********************************************************************/
13 import java.io.File;
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;
19
20 import net.sourceforge.phpeclipse.webbrowser.IExternalWebBrowserWorkingCopy;
21
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;
27
28 /**
29  * 
30  */
31 public class BrowserSearcher {
32         private static boolean cancelled;
33
34         private BrowserSearcher() {
35                 super();
36         }
37
38         /**
39          * Search for installed VMs in the file system
40          */
41         protected static List search(Shell shell) {
42                 final List foundBrowsers = new ArrayList();
43                 final List existingPaths = WebBrowserUtil.getExternalBrowserPaths();
44
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"));
49
50                 String path = dialog.open();
51                 if (path == null)
52                         return null;
53
54                 cancelled = false;
55
56                 final File rootDir = new File(path);
57                 ProgressMonitorDialog pm = new ProgressMonitorDialog(shell);
58
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);
65                                 monitor.done();
66                                 if (monitor.isCanceled())
67                                         setCancelled(true);
68                         }
69                 };
70
71                 try {
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);
79                         return null;
80                 }
81
82                 if (cancelled)
83                         return null;
84
85                 return foundBrowsers;
86         }
87
88         protected static void setCancelled(boolean b) {
89                 cancelled = b;
90         }
91
92         protected static void search(File directory, List existingPaths,
93                         List foundBrowsers, IProgressMonitor monitor) {
94                 if (monitor.isCanceled())
95                         return;
96
97                 String[] names = directory.list();
98                 List subDirs = new ArrayList();
99
100                 for (int i = 0; i < names.length; i++) {
101                         if (monitor.isCanceled())
102                                 return;
103
104                         File file = new File(directory, names[i]);
105
106                         if (existingPaths.contains(file.getAbsolutePath().toLowerCase()))
107                                 continue;
108
109                         IExternalWebBrowserWorkingCopy wc = WebBrowserUtil
110                                         .createExternalBrowser(file);
111                         if (wc != null)
112                                 foundBrowsers.add(wc);
113
114                         try {
115                                 monitor.subTask(MessageFormat.format(WebBrowserUIPlugin
116                                                 .getResource("%searching"), new String[] {
117                                                 Integer.toString(foundBrowsers.size()),
118                                                 file.getCanonicalPath() }));
119                         } catch (IOException ioe) {
120                         }
121
122                         if (file.isDirectory()) {
123                                 if (monitor.isCanceled())
124                                         return;
125                                 subDirs.add(file);
126                         }
127                 }
128                 while (!subDirs.isEmpty()) {
129                         File subDir = (File) subDirs.remove(0);
130                         search(subDir, existingPaths, foundBrowsers, monitor);
131                         if (monitor.isCanceled()) {
132                                 return;
133                         }
134                 }
135         }
136 }