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