Bug 535151 - Show only launch types with children in export dialog

Since a typical Eclipse user will have a lot of empty launch config
types (i.e. with no defined launch configs or prototypes), which we can
hide from the export launch config dialog.

Had to move some private members from the nested class to the top level
class to use them in both.

Signed-off-by: Michael Keppler <Michael.Keppler@gmx.de>
Also-by: Karsten Thoms <karsten.thoms@itemis.de>
Change-Id: I04a0e52b5c9d4427e5221591b33ea0aaf67488e7
Bug:535151
diff --git a/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/importexport/launchconfigurations/ExportLaunchConfigurationsWizardPage.java b/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/importexport/launchconfigurations/ExportLaunchConfigurationsWizardPage.java
index e55ada4..c54b38f 100644
--- a/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/importexport/launchconfigurations/ExportLaunchConfigurationsWizardPage.java
+++ b/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/importexport/launchconfigurations/ExportLaunchConfigurationsWizardPage.java
@@ -19,6 +19,7 @@
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 
 import org.eclipse.core.filesystem.EFS;
@@ -76,19 +77,19 @@
  */
 public class ExportLaunchConfigurationsWizardPage extends WizardPage {
 
+	private ILaunchManager lm = DebugPlugin.getDefault().getLaunchManager();
+
 	/**
 	 * The content provider for the tree viewer
 	 * @since 3.4.0
 	 */
 	class ConfigContentProvider implements ITreeContentProvider {
 
-		ILaunchManager lm = DebugPlugin.getDefault().getLaunchManager();
-
 		@Override
 		public Object[] getChildren(Object parentElement) {
 			if(parentElement instanceof ILaunchConfigurationType) {
 				try {
-					return lm.getLaunchConfigurations((ILaunchConfigurationType) parentElement, ILaunchConfiguration.CONFIGURATION | ILaunchConfiguration.PROTOTYPE);
+					return getConfigurationTypeChildren((ILaunchConfigurationType) parentElement);
 				}
 				catch (Exception e) {
 					DebugUIPlugin.logErrorMessage(e.getMessage());
@@ -96,6 +97,7 @@
 			}
 			return null;
 		}
+
 		@Override
 		public Object getParent(Object element) {
 			if(element instanceof ILaunchConfiguration) {
@@ -113,7 +115,7 @@
 		}
 		@Override
 		public Object[] getElements(Object inputElement) {
-			return lm.getLaunchConfigurationTypes();
+			return getUsedLaunchConfigurationTypes();
 		}
 	}
 	private String OVERWRITE = "overwrite"; //$NON-NLS-1$
@@ -169,7 +171,7 @@
 		fViewer.setComparator(new WorkbenchViewerComparator());
 		fContentProvider = new ConfigContentProvider();
 		fViewer.setContentProvider(fContentProvider);
-		fViewer.setInput(DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurationTypes());
+		fViewer.setInput(getUsedLaunchConfigurationTypes());
 		//we don't want to see builders....
 		fViewer.addFilter(new LaunchCategoryFilter(IInternalDebugUIConstants.ID_EXTERNAL_TOOL_BUILDER_LAUNCH_CATEGORY));
 		//need to force load the children so that select all works initially
@@ -216,6 +218,25 @@
 	}
 
 	/**
+	 * @return launch configuration types which currently have configuration or
+	 *         prototype children, and are therefore relevant for exporting
+	 */
+	private ILaunchConfigurationType[] getUsedLaunchConfigurationTypes() {
+		return Arrays.stream(DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurationTypes()).filter(launchConfigType -> {
+			try {
+				return getConfigurationTypeChildren(launchConfigType).length > 0;
+			} catch (CoreException e) {
+				DebugUIPlugin.logErrorMessage(e.getMessage());
+			}
+			return true;
+		}).toArray(ILaunchConfigurationType[]::new);
+	}
+
+	private ILaunchConfiguration[] getConfigurationTypeChildren(ILaunchConfigurationType launchConfigurationTypeParent) throws CoreException {
+		return lm.getLaunchConfigurations(launchConfigurationTypeParent, ILaunchConfiguration.CONFIGURATION | ILaunchConfiguration.PROTOTYPE);
+	}
+
+	/**
 	 * Updates the checked state of child launch configurations if the parent type is checked
 	 * @param item
 	 */