Bug 460381 - Switch from SelectionDialog to AbstractSelectionDialog

Switched FeatureSelectionDialog

Change-Id: I98d1cd9522c7a1f768ae0dd719d80513d92feea0
Signed-off-by: Alexander Fedorov <alexander.fedorov@arsysop.ru>
diff --git a/bundles/org.eclipse.jface/META-INF/MANIFEST.MF b/bundles/org.eclipse.jface/META-INF/MANIFEST.MF
index 440518d..267c38e 100644
--- a/bundles/org.eclipse.jface/META-INF/MANIFEST.MF
+++ b/bundles/org.eclipse.jface/META-INF/MANIFEST.MF
@@ -2,7 +2,7 @@
 Bundle-ManifestVersion: 2
 Bundle-Name: %pluginName
 Bundle-SymbolicName: org.eclipse.jface;singleton:=true
-Bundle-Version: 3.15.200.qualifier
+Bundle-Version: 3.16.0.qualifier
 Bundle-ClassPath: .
 Bundle-Vendor: %providerName
 Bundle-Localization: plugin
diff --git a/bundles/org.eclipse.jface/pom.xml b/bundles/org.eclipse.jface/pom.xml
index 9416d2c..e3c0c0a 100644
--- a/bundles/org.eclipse.jface/pom.xml
+++ b/bundles/org.eclipse.jface/pom.xml
@@ -20,7 +20,7 @@
   </parent>
   <groupId>org.eclipse.jface</groupId>
   <artifactId>org.eclipse.jface</artifactId>
-  <version>3.15.200-SNAPSHOT</version>
+  <version>3.16.0-SNAPSHOT</version>
   <packaging>eclipse-plugin</packaging>
 
   <properties>
diff --git a/bundles/org.eclipse.jface/src/org/eclipse/jface/dialogs/AbstractSelectionDialog.java b/bundles/org.eclipse.jface/src/org/eclipse/jface/dialogs/AbstractSelectionDialog.java
index e9b1d43..fa8d0b7 100644
--- a/bundles/org.eclipse.jface/src/org/eclipse/jface/dialogs/AbstractSelectionDialog.java
+++ b/bundles/org.eclipse.jface/src/org/eclipse/jface/dialogs/AbstractSelectionDialog.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2015 vogella GmbH and others.
+ * Copyright (c) 2015-2019 vogella GmbH and others.
  *
  * This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License 2.0
@@ -10,6 +10,7 @@
  *
  * Contributors:
  *     Simon Scholz <simon.scholz@vogella.com> - Bug 446616
+ *     Alexander Fedorov <alexander.fedorov@arsysop.ru> - Bug 460381
  *******************************************************************************/
 package org.eclipse.jface.dialogs;
 
@@ -18,7 +19,11 @@
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
+import java.util.Optional;
+import java.util.stream.Collectors;
 
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Label;
@@ -39,8 +44,7 @@
  *
  */
 public abstract class AbstractSelectionDialog<T> extends TrayDialog {
-	// the final collection of selected elements, or null if this dialog was
-	// canceled
+	// the final collection of selected elements
 	private Collection<T> result;
 
 	// a list of the initially-selected elements
@@ -121,11 +125,28 @@
 	/**
 	 * Returns the collection of selections made by the user.
 	 *
-	 * @return the collection of selected elements, or <code>null</code> if no
-	 *         result was set
+	 * @return the collection of selected elements, or
+	 *         <code>Collections.emptyList()</code> if no result was set
 	 */
 	public Collection<T> getResult() {
-		return result;
+		return result != null ? result : Collections.emptyList();
+	}
+
+	/**
+	 * Returns an <code>java.util.Optional&lt;T&gt;</code> containing the first
+	 * element from the collection of selections made by the user. Returns
+	 * {@link Optional#empty()} if no element has been selected.
+	 *
+	 * @return an <code>java.util.Optional&lt;T&gt;</code> containing the first
+	 *         result element if one exists. Otherwise {@link Optional#empty()} is
+	 *         returned.
+	 * @since 3.16
+	 */
+	public Optional<T> getFirstResult() {
+		if (result != null) {
+			return result.stream().findFirst();
+		}
+		return Optional.empty();
 	}
 
 	/**
@@ -161,37 +182,48 @@
 	}
 
 	/**
-	 * Set the selections made by the user, or <code>null</code> if the
-	 * selection was canceled.
+	 * Set the selections made by the user.
+	 * <p>
+	 * The result may be accessed using <code>getResult</code>.
+	 * </p>
 	 *
-	 * @param newUserSelection
-	 *            collection of selected elements, or <code>null</code> if
-	 *            Cancel was pressed
+	 * @param newUserSelection collection of selected elements
 	 */
 	protected void setResult(Collection<T> newUserSelection) {
+		result = newUserSelection;
+	}
+
+	/**
+	 * Set the selections made by the user.
+	 * <p>
+	 * The result may be accessed using <code>getResult</code>.
+	 * </p>
+	 *
+	 * @param newUserSelection - the new values
+	 */
+	protected void setResult(T... newUserSelection) {
 		if (newUserSelection == null) {
-			result = Collections.emptyList();
+			result = null;
 		} else {
-			result = newUserSelection;
+			result = Arrays.asList(newUserSelection);
 		}
 	}
 
 	/**
-	 * Set the selections made by the user, or <code>null</code> if the
-	 * selection was canceled.
-	 * <p>
-	 * The selections may accessed using <code>getResult</code>.
-	 * </p>
+	 * Set the selections obtained from a viewer.
 	 *
-	 * @param newUserSelection
-	 *            - the new values
+	 * @param selection selection obtained from a viewer
+	 * @param target    target type to check for <code>instanceof</code>
+	 * @since 3.16
 	 */
-	protected void setResult(T... newUserSelection) {
-		if (newUserSelection == null) {
-			result = Collections.emptyList();
-		} else {
-			result = Arrays.asList(newUserSelection);
+	protected void setResult(ISelection selection, Class<T> target) {
+		List<T> selected = null;
+		if (selection instanceof IStructuredSelection && target != null) {
+			IStructuredSelection structured = (IStructuredSelection) selection;
+			selected = ((List<?>) structured.toList()).stream().filter(p -> target.isInstance(p))
+					.map(p -> target.cast(p)).collect(Collectors.toList());
 		}
+		setResult(selected);
 	}
 
 	/**
diff --git a/bundles/org.eclipse.ui.ide/extensions/org/eclipse/ui/actions/QuickStartAction.java b/bundles/org.eclipse.ui.ide/extensions/org/eclipse/ui/actions/QuickStartAction.java
index eabd26b..8b9fa26 100644
--- a/bundles/org.eclipse.ui.ide/extensions/org/eclipse/ui/actions/QuickStartAction.java
+++ b/bundles/org.eclipse.ui.ide/extensions/org/eclipse/ui/actions/QuickStartAction.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2018 IBM Corporation and others.
+ * Copyright (c) 2000, 2019 IBM Corporation and others.
  *
  * This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License 2.0
@@ -11,6 +11,7 @@
  * Contributors:
  *     IBM Corporation - initial API and implementation
  *     Mickael Istria (Red Hat Inc.) - [486901] Avoid blocking URL.equals
+ *     Alexander Fedorov <alexander.fedorov@arsysop.ru> - Bug 460381
  *******************************************************************************/
 package org.eclipse.ui.actions;
 
@@ -141,10 +142,10 @@
                 product == null ? null : product.getId(), IDEWorkbenchMessages.WelcomePageSelectionDialog_title,
                 IDEWorkbenchMessages.WelcomePageSelectionDialog_message,
                 IIDEHelpContextIds.WELCOME_PAGE_SELECTION_DIALOG);
-        if (d.open() != Window.OK || d.getResult().length != 1) {
+        if (d.open() != Window.OK || d.getResult().size() != 1) {
 			return null;
 		}
-        return (AboutInfo) d.getResult()[0];
+		return d.getFirstResult().orElse(null);
     }
 
     /**
diff --git a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/FeatureSelectionDialog.java b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/FeatureSelectionDialog.java
index b5261c9..10baff3 100644
--- a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/FeatureSelectionDialog.java
+++ b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/FeatureSelectionDialog.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2018 IBM Corporation and others.
+ * Copyright (c) 2000, 2019 IBM Corporation and others.
  *
  * This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License 2.0
@@ -11,6 +11,7 @@
  * Contributors:
  *     IBM Corporation - initial API and implementation
  *     Simon Scholz <simon.scholz@vogella.com> - Bug 448060
+ *     Alexander Fedorov <alexander.fedorov@arsysop.ru> - Bug 460381
  *******************************************************************************/
 
 package org.eclipse.ui.internal.ide;
@@ -19,8 +20,9 @@
 import java.util.Comparator;
 import java.util.Locale;
 
+import org.eclipse.jface.dialogs.AbstractSelectionDialog;
+import org.eclipse.jface.dialogs.IDialogConstants;
 import org.eclipse.jface.viewers.ArrayContentProvider;
-import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.jface.viewers.LabelProvider;
 import org.eclipse.jface.viewers.ListViewer;
 import org.eclipse.jface.viewers.StructuredSelection;
@@ -30,14 +32,13 @@
 import org.eclipse.swt.widgets.Control;
 import org.eclipse.swt.widgets.Shell;
 import org.eclipse.ui.PlatformUI;
-import org.eclipse.ui.dialogs.SelectionDialog;
 
 import com.ibm.icu.text.Collator;
 
 /**
  * Dialog to allow the user to select a feature from a list.
  */
-public class FeatureSelectionDialog extends SelectionDialog {
+public class FeatureSelectionDialog extends AbstractSelectionDialog<AboutInfo> {
     /**
      * List width in characters.
      */
@@ -112,13 +113,10 @@
         // Find primary feature
         for (AboutInfo feature : features) {
             if (feature.getFeatureId().equals(primaryFeatureId)) {
-				setInitialSelections(feature);
+				setInitialSelection(feature);
                 return;
             }
         }
-
-        // set a safe default
-        setInitialSelections(new Object[0]);
     }
 
     @Override
@@ -155,12 +153,12 @@
 		listViewer.setContentProvider(ArrayContentProvider.getInstance());
 		listViewer.setInput(features);
 
-        // Set the initial selection
-        listViewer.setSelection(new StructuredSelection(
-                getInitialElementSelections()), true);
+		// Add a selection change listener
+		listViewer.addSelectionChangedListener(
+				event -> getButton(IDialogConstants.OK_ID).setEnabled(!event.getSelection().isEmpty()));
 
-        // Add a selection change listener
-        listViewer.addSelectionChangedListener(event -> getOkButton().setEnabled(!event.getSelection().isEmpty()));
+        // Set the initial selection
+		listViewer.setSelection(new StructuredSelection(getInitialSelection()), true);
 
         // Add double-click listener
         listViewer.addDoubleClickListener(event -> okPressed());
@@ -169,8 +167,7 @@
 
     @Override
 	protected void okPressed() {
-		IStructuredSelection selection = listViewer.getStructuredSelection();
-        setResult(selection.toList());
-        super.okPressed();
+		setResult(listViewer.getStructuredSelection(), AboutInfo.class);
+		super.okPressed();
     }
 }
diff --git a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/TipsAndTricksAction.java b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/TipsAndTricksAction.java
index a6a07d6..5e779cf 100644
--- a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/TipsAndTricksAction.java
+++ b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/TipsAndTricksAction.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2017 IBM Corporation and others.
+ * Copyright (c) 2000, 2019 IBM Corporation and others.
  *
  * This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License 2.0
@@ -12,6 +12,7 @@
  *     IBM Corporation - initial API and implementation
  *     Sebastian Davids <sdavids@gmx.de> - Fix for bug 93373 - [Intro]
  *     		TipsAndTricksAction should not use magic numbers
+ *     Alexander Fedorov <alexander.fedorov@arsysop.ru> - Bug 460381
  *******************************************************************************/
 package org.eclipse.ui.internal.ide;
 
@@ -99,13 +100,12 @@
                 IDEWorkbenchMessages.TipsAndTricksPageSelectionDialog_message,
                 IIDEHelpContextIds.TIPS_AND_TRICKS_PAGE_SELECTION_DIALOG);
         d.create();
-        d.getOkButton().setEnabled(false);
 
-        if (d.open() != Window.OK || d.getResult().length != 1) {
+        if (d.open() != Window.OK || d.getResult().size() != 1) {
 			return;
 		}
 
-        AboutInfo feature = (AboutInfo) d.getResult()[0];
+		AboutInfo feature = d.getFirstResult().orElse(null);
 
         /**
          * Open the tips and trick help topic