Bug 567899 - P2 import software "install latest versions" not working

Fixed selection algorithm to install "latest" versions if asked to do
so, and install "oldest" versions otherwise.

Change-Id: I929b5d5f458810acd62df2d8982a89d49aa494b3
Signed-off-by: Andrey Loskutov <loskutov@gmx.de>
diff --git a/bundles/org.eclipse.equinox.p2.ui.importexport/src/org/eclipse/equinox/internal/p2/importexport/IUDetail.java b/bundles/org.eclipse.equinox.p2.ui.importexport/src/org/eclipse/equinox/internal/p2/importexport/IUDetail.java
index 0a547d0..50087fd 100644
--- a/bundles/org.eclipse.equinox.p2.ui.importexport/src/org/eclipse/equinox/internal/p2/importexport/IUDetail.java
+++ b/bundles/org.eclipse.equinox.p2.ui.importexport/src/org/eclipse/equinox/internal/p2/importexport/IUDetail.java
@@ -59,4 +59,20 @@
 	public int hashCode() {
 		return iu.hashCode();
 	}
+
+	@Override
+	public String toString() {
+		StringBuilder sb = new StringBuilder();
+		if (iu != null) {
+			sb.append("iu="); //$NON-NLS-1$
+			sb.append(iu);
+			sb.append(", "); //$NON-NLS-1$
+		}
+		if (referredRepo != null) {
+			sb.append("referredRepo="); //$NON-NLS-1$
+			sb.append(referredRepo);
+		}
+		sb.append("]"); //$NON-NLS-1$
+		return sb.toString();
+	}
 }
diff --git a/bundles/org.eclipse.equinox.p2.ui.importexport/src/org/eclipse/equinox/internal/p2/importexport/internal/wizard/ImportPage.java b/bundles/org.eclipse.equinox.p2.ui.importexport/src/org/eclipse/equinox/internal/p2/importexport/internal/wizard/ImportPage.java
index 56ff67e..6308464 100644
--- a/bundles/org.eclipse.equinox.p2.ui.importexport/src/org/eclipse/equinox/internal/p2/importexport/internal/wizard/ImportPage.java
+++ b/bundles/org.eclipse.equinox.p2.ui.importexport/src/org/eclipse/equinox/internal/p2/importexport/internal/wizard/ImportPage.java
@@ -328,27 +328,32 @@
 	public Object[] getCheckedIUElements() {
 		Object[] checked = viewer.getCheckedElements();
 		List<IUDetail> checkedFeatures = new ArrayList<>(checked.length);
+		boolean useLatest = installLatest.getSelection();
 		for (Object checked1 : checked) {
 			IUDetail feature = (IUDetail) checked1;
 			IUDetail[] existingFeatures = newProposedFeature.get(feature);
-			if (existingFeatures == null)
+			if (existingFeatures == null) {
 				checkedFeatures.add(feature);
-			else {
+			} else {
 				IUDetail matchPolicy = null;
 				for (IUDetail f : existingFeatures) {
-					if (matchPolicy == null)
+					if (matchPolicy == null) {
 						matchPolicy = f;
-					// here use exact match
-					else if (matchPolicy.getIU().getVersion().compareTo(f.getIU().getVersion()) < 0) {
-						if (installLatest.getSelection())
-							matchPolicy = f;
-						else
-							continue;
-					} else
-						matchPolicy = f;
+					} else {
+						if (matchPolicy.getIU().getVersion().compareTo(f.getIU().getVersion()) < 0) {
+							if (useLatest) {
+								matchPolicy = f;
+							}
+						} else {
+							if (!useLatest) {
+								matchPolicy = f;
+							}
+						}
+					}
 				}
-				if (matchPolicy != null)
+				if (matchPolicy != null) {
 					checkedFeatures.add(matchPolicy);
+				}
 			}
 		}
 		return checkedFeatures.toArray(new IUDetail[checkedFeatures.size()]);