Bug 545912: [Mac] Installed JRE Search should default directory dialog
to java home

Set default directory dialog to /Library/Java/JavaVirtualMachines.
Restored the fast doMacSearch() code. Use doMacSearch() if chosen
directory is
/Library/Java/JavaVirtualMachines

Change-Id: I0481f73a2787a841f449b709cfaebc4ca20dc78a
Signed-off-by: Lakshmi Shanmugam <lshanmug@in.ibm.com>
diff --git a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/jres/InstalledJREsBlock.java b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/jres/InstalledJREsBlock.java
index ad1c819..092d02b 100644
--- a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/jres/InstalledJREsBlock.java
+++ b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/jres/InstalledJREsBlock.java
@@ -22,11 +22,15 @@
 import java.util.List;
 import java.util.Set;
 
+import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.ListenerList;
+import org.eclipse.core.runtime.Platform;
+import org.eclipse.core.runtime.SubMonitor;
 import org.eclipse.debug.internal.ui.SWTFactory;
 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
+import org.eclipse.jdt.internal.launching.MacInstalledJREs;
 import org.eclipse.jdt.launching.AbstractVMInstall;
 import org.eclipse.jdt.launching.AbstractVMInstallType;
 import org.eclipse.jdt.launching.IVMInstall;
@@ -851,7 +855,19 @@
 		DirectoryDialog dialog = new DirectoryDialog(getShell(), SWT.SHEET);
 		dialog.setMessage(JREMessages.InstalledJREsBlock_9);
 		dialog.setText(JREMessages.InstalledJREsBlock_10);
-		String path = dialog.open();
+
+		String path = null;
+		if (Platform.OS_MACOSX.equals(Platform.getOS())) {
+			String MAC_JAVA_SEARCH_PATH = "/Library/Java/JavaVirtualMachines"; //$NON-NLS-1$
+			dialog.setFilterPath(MAC_JAVA_SEARCH_PATH);
+			path = dialog.open();
+			if (MAC_JAVA_SEARCH_PATH.equals(path)) {
+				doMacSearch();
+				return;
+			}
+		} else {
+			path = dialog.open();
+		}
 		if (path == null) {
 			return;
 		}
@@ -929,6 +945,50 @@
 		}
 	}
 
+	/**
+	 * Calls out to {@link MacVMSearch} to find all installed JREs in the standard
+	 * Mac OS location
+	 */
+	private void doMacSearch() {
+		final List<VMStandin> added = new ArrayList<>();
+		IRunnableWithProgress r = new IRunnableWithProgress() {
+			@Override
+			public void run(IProgressMonitor monitor) throws InvocationTargetException {
+				Set<String> exists = new HashSet<>();
+				for (IVMInstall vm : fVMs) {
+					exists.add(vm.getId());
+				}
+				SubMonitor localmonitor = SubMonitor.convert(monitor, JREMessages.MacVMSearch_0, 5);
+				VMStandin[] standins = null;
+				try {
+					standins = MacInstalledJREs.getInstalledJREs(localmonitor);
+					for (int i = 0; i < standins.length; i++) {
+						if (!exists.contains(standins[i].getId())) {
+							added.add(standins[i]);
+						}
+					}
+				}
+				catch(CoreException ce) {
+					JDIDebugUIPlugin.log(ce);
+				}
+				monitor.done();
+			}
+		};
+
+		try {
+            ProgressMonitorDialog progress = new ProgressMonitorDialog(getShell());
+            progress.run(true, true, r);
+		} catch (InvocationTargetException e) {
+			JDIDebugUIPlugin.log(e);
+		} catch (InterruptedException e) {
+			// canceled
+			return;
+		}
+		for(VMStandin vm: added) {
+			vmAdded(vm);
+		}
+	}
+
 	protected Shell getShell() {
 		return getControl().getShell();
 	}