Bug 401358 - Name selection for Mac VM installs needs improvement - fix
selecting and deleting JREs
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 1bffe4d..88f532a 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
@@ -19,12 +19,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;
@@ -165,7 +168,7 @@
 				IVMInstall vm= (IVMInstall)element;
 				switch(columnIndex) {
 					case 0:
-						if (isContributed(vm)) {
+						if (JavaRuntime.isContributedVMInstall(vm.getId())) {
 							return NLS.bind(JREMessages.InstalledJREsBlock_19, new String[]{vm.getName()});
 						}
 						if(fVMList.getChecked(element)) {
@@ -556,11 +559,11 @@
 		int selectionCount= selection.size();
 		fEditButton.setEnabled(selectionCount == 1);
 		fCopyButton.setEnabled(selectionCount > 0);
-		if (selectionCount > 0 && selectionCount < fVMList.getTable().getItemCount()) {
+		if (selectionCount > 0 && selectionCount <= fVMList.getTable().getItemCount()) {
 			Iterator<IVMInstall> iterator = selection.iterator();
 			while (iterator.hasNext()) {
 				IVMInstall install = iterator.next();
-				if (isContributed(install)) {
+				if (JavaRuntime.isContributedVMInstall(install.getId())) {
 					fRemoveButton.setEnabled(false);
 					return;
 				}
@@ -572,15 +575,6 @@
 	}	
 	
 	/**
-	 * Returns if the specified VM install has been contributed
-	 * @param install
-	 * @return true if the specified VM is contributed, false otherwise
-	 */
-	private boolean isContributed(IVMInstall install) {
-		return JavaRuntime.isContributedVMInstall(install.getId());
-	}
-	
-	/**
 	 * Returns this block's control
 	 * 
 	 * @return control
@@ -635,11 +629,16 @@
 	 * @see IAddVMDialogRequestor#vmAdded(IVMInstall)
 	 */
 	public void vmAdded(IVMInstall vm) {
+		boolean makeselection = fVMs.size() < 1;
 		fVMs.add(vm);
 		//update from model
 		fVMList.refresh();
 		//update labels
 		fVMList.refresh(true);
+		//if we add a VM and none are selected, select one of them
+		if(makeselection) {
+			fireSelectionChanged();
+		}
 	}
 	
 	/**
@@ -664,7 +663,7 @@
 		if (vm == null) {
 			return;
 		}
-		if (isContributed(vm)) {
+		if (JavaRuntime.isContributedVMInstall(vm.getId())) {
 			VMDetailsDialog dialog= new VMDetailsDialog(getShell(), vm);
 			dialog.open();
 		} else {
@@ -705,21 +704,21 @@
 	 * @param vms
 	 */
 	public void removeJREs(IVMInstall[] vms) {
-		IStructuredSelection prev = (IStructuredSelection) getSelection();
 		for (int i = 0; i < vms.length; i++) {
 			fVMs.remove(vms[i]);
 		}
-		IStructuredSelection curr = (IStructuredSelection) getSelection();
-		if (!curr.equals(prev)) {
-			IVMInstall[] installs = getJREs();
-			if (curr.size() == 0 && installs.length == 1) {
-				// pick a default VM automatically
-				setSelection(new StructuredSelection(installs[0]));
-			} else {
-				fireSelectionChanged();
-			}
-		}
 		fVMList.refresh();
+		IStructuredSelection curr = (IStructuredSelection) getSelection();
+		IVMInstall[] installs = getJREs();
+		if(installs.length < 1) {
+			fPrevSelection = null;
+		}
+		if (curr.size() == 0 && installs.length == 1) {
+			// pick a default VM automatically
+			setSelection(new StructuredSelection(installs[0]));
+		} else {
+			fireSelectionChanged();
+		}
 		fVMList.refresh(true);
 	}
 	
@@ -830,14 +829,19 @@
 					IVMInstall vm = (IVMInstall) iterator.next();
 					exists.add(vm.getId());
 				}
-				VMStandin[] standins = new MacVMSearch().search(monitor);
-				for (int i = 0; i < standins.length; i++) {
-					//we should be comparing install paths, as the ID can be computed differently 
-					//based on if the VM is searched or added manually
-					if (!exists.contains(standins[i].getId())) {
-						added.add(standins[i]);
+				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();
 			}
 		};
@@ -857,7 +861,6 @@
 			IVMInstall vm = iterator.next();
 			vmAdded(vm);
 		}
-
 	}
 
 	protected Shell getShell() {
diff --git a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/jres/JREMessages.java b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/jres/JREMessages.java
index b671589..cf2e94d 100644
--- a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/jres/JREMessages.java
+++ b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/jres/JREMessages.java
@@ -72,6 +72,8 @@
 	public static String JREsPreferencePage_10;
 	public static String JREsPreferencePage_13;
 
+	public static String JREsPreferencePage_3;
+
 	public static String addVMDialog_duplicateName;
 	public static String addVMDialog_enterLocation;
 	public static String addVMDialog_enterName;
diff --git a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/jres/JREMessages.properties b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/jres/JREMessages.properties
index aed10e3..31c585a 100644
--- a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/jres/JREMessages.properties
+++ b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/jres/JREMessages.properties
@@ -65,8 +65,9 @@
 JREsPreferencePage_2=Add, remove or edit JRE definitions. By default, the checked JRE is added to the build path of newly created Java projects.
 JREsPreferencePage_0=The selected JRE does not support the current compiler compliance level of {0}
 JREsPreferencePage_10=Installed JRE location no longer exists.  JRE will be removed.
-JREsPreferencePage_13=Select a default JRE
+JREsPreferencePage_13=You must select a default JRE for the workspace
 JREsPreferencePage_14=Conflicting compliance settings can be changed on the <a>Compiler</a> page.
+JREsPreferencePage_3=You must provide at least one JRE to act as the workspace default
 
 addVMDialog_duplicateName=The JRE name is already in use.
 addVMDialog_enterLocation=Enter the home directory of the JRE.
diff --git a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/jres/JREsPreferencePage.java b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/jres/JREsPreferencePage.java
index 8089105..d005487 100644
--- a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/jres/JREsPreferencePage.java
+++ b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/jres/JREsPreferencePage.java
@@ -139,7 +139,12 @@
 				IVMInstall install = getCurrentDefaultVM();
 				if (install == null) {
 					setValid(false);
-					setErrorMessage(JREMessages.JREsPreferencePage_13); 
+					if(fJREBlock.getJREs().length < 1) {
+						setErrorMessage(JREMessages.JREsPreferencePage_3);
+					}
+					else {
+						setErrorMessage(JREMessages.JREsPreferencePage_13); 
+					}
 				} else {
 					//if we change the VM make sure the compliance level supports 
 					//generated class files
diff --git a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/jres/MacVMSearch.java b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/jres/MacVMSearch.java
deleted file mode 100644
index 4e36b1f..0000000
--- a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/jres/MacVMSearch.java
+++ /dev/null
@@ -1,39 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2007, 2013 IBM Corporation and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- *     IBM Corporation - initial API and implementation
- *******************************************************************************/
-package org.eclipse.jdt.internal.debug.ui.jres;
-
-import org.eclipse.core.runtime.CoreException;
-import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.SubMonitor;
-import org.eclipse.jdt.internal.launching.MacInstalledJREs;
-import org.eclipse.jdt.launching.VMStandin;
-
-/**
- * Searches for installed JREs on the MAC, in known location.
- */
-public class MacVMSearch {
-
-	/**
-	 * Returns an array of {@link VMStandin}s found at the standard Mac OS location
-	 * or an empty listing, never <code>null</code>
-	 * @param monitor
-	 * @return a listing of {@link VMStandin}s at the standard Mac OS location or an empty listing
-	 */
-	public VMStandin[] search(IProgressMonitor monitor) {
-		SubMonitor localmonitor = SubMonitor.convert(monitor, JREMessages.MacVMSearch_0, 5);
-		try {
-			return MacInstalledJREs.getInstalledJREs(localmonitor);
-		}
-		catch(CoreException ce) {
-			return MacInstalledJREs.NO_VMS;
-		}
-	}
-}