Bug 427195 - Migration wizard sort issue.
Improved migration wizard message.

Change-Id: Ia8deaf7f8915dec092fd59248a1094d80cd2236e
Signed-off-by: Hamdan Msheik <hamdan.msheik@ericsson.com>
diff --git a/bundles/org.eclipse.equinox.p2.ui.sdk.scheduler/src/org/eclipse/equinox/internal/p2/ui/sdk/scheduler/migration/MigrationPage.java b/bundles/org.eclipse.equinox.p2.ui.sdk.scheduler/src/org/eclipse/equinox/internal/p2/ui/sdk/scheduler/migration/MigrationPage.java
index f065189..ac8a7bc 100644
--- a/bundles/org.eclipse.equinox.p2.ui.sdk.scheduler/src/org/eclipse/equinox/internal/p2/ui/sdk/scheduler/migration/MigrationPage.java
+++ b/bundles/org.eclipse.equinox.p2.ui.sdk.scheduler/src/org/eclipse/equinox/internal/p2/ui/sdk/scheduler/migration/MigrationPage.java
@@ -9,7 +9,7 @@
  *     WindRiver Corporation - initial API and implementation
  *     IBM Corporation - Ongoing development
  *     Ericsson AB (Pascal Rapicault) - Bug 387115 - Allow to export everything
- *     Ericsson AB (Hamdan Msheik) - Bug 398833, 402560
+ *     Ericsson AB (Hamdan Msheik) - Bug 398833, 402560, 427195
  *******************************************************************************/
 package org.eclipse.equinox.internal.p2.ui.sdk.scheduler.migration;
 
@@ -75,6 +75,7 @@
 
 	protected IProvisioningAgent otherInstanceAgent = null;
 	private Collection<IInstallableUnit> unitsToMigrate;
+	private Set<IInstallableUnit> selectedUnitsToMigrate; // selected units to be migrated, initially contains all units not installed in current profile.
 	private IProfile toImportFrom = null;
 	//	private File instancePath = null;
 	private URI[] metaURIs = null;
@@ -277,11 +278,11 @@
 			column.getColumn().setMoveable(true);
 			if (titles[i].equals(ProvUIMessages.Column_Name)) {
 				column.getColumn().setWidth(300);
+				updateTableSorting(i);
 			} else {
 				column.getColumn().setWidth(200);
 			}
-			if (ProvUIMessages.Column_Name.equals(titles[i]))
-				updateTableSorting(i);
+
 			final int columnIndex = i;
 			column.getColumn().addSelectionListener(new SelectionAdapter() {
 				@Override
@@ -404,7 +405,7 @@
 			}
 		});
 		parent.addControlListener(new ControlAdapter() {
-			private final int[] columnRate = new int[] {6, 2, 2};
+			private final int[] columnRate = new int[] {4, 2, 2};
 
 			@Override
 			public void controlResized(ControlEvent e) {
@@ -444,6 +445,9 @@
 				}
 			}
 		});
+
+		selectedUnitsToMigrate = identifyUnitsToBeMigrated();
+
 		ICheckStateProvider provider = getViewerDefaultState();
 		if (provider != null)
 			viewer.setCheckStateProvider(provider);
@@ -457,13 +461,14 @@
 		viewer.setInput(getInput());
 
 		viewer.getTree().addListener(SWT.Selection, new Listener() {
-
 			public void handleEvent(Event event) {
-				if (event.detail == SWT.CHECK) {
-					if (hasInstalled(ProvUI.getAdapter(event.item.getData(), IInstallableUnit.class))) {
-						viewer.getTree().setRedraw(false);
-						((TreeItem) event.item).setChecked(false);
-						viewer.getTree().setRedraw(true);
+				if (event.item instanceof TreeItem && event.detail == SWT.CHECK) {
+					TreeItem treeItem = (TreeItem) event.item;
+					IInstallableUnit iu = ProvUI.getAdapter(event.item.getData(), IInstallableUnit.class);
+					if (treeItem.getChecked()) {
+						selectedUnitsToMigrate.add(iu);
+					} else {
+						selectedUnitsToMigrate.remove(iu);
 					}
 				}
 				updatePageCompletion();
@@ -498,6 +503,15 @@
 			@Override
 			public void widgetSelected(SelectionEvent e) {
 				for (TreeItem item : viewer.getTree().getItems()) {
+					if (item.getChecked()) {
+						item.setChecked(false);
+						Event event = new Event();
+						event.widget = item.getParent();
+						event.detail = SWT.CHECK;
+						event.item = item;
+						event.type = SWT.Selection;
+						viewer.getTree().notifyListeners(SWT.Selection, event);
+					}
 					viewer.setSubtreeChecked(item.getData(), false);
 				}
 				updatePageCompletion();
@@ -506,6 +520,21 @@
 
 	}
 
+	private Set<IInstallableUnit> identifyUnitsToBeMigrated() {
+
+		Set<IInstallableUnit> ius = new HashSet<IInstallableUnit>();
+		if (profile != null) {
+			for (IInstallableUnit iu : unitsToMigrate) {
+				IQueryResult<IInstallableUnit> collector = profile.query(QueryUtil.createIUQuery(iu.getId(), new VersionRange(iu.getVersion(), true, null, false)), new NullProgressMonitor());
+				if (collector.isEmpty()) {
+					ius.add(iu);
+				}
+			}
+		}
+
+		return ius;
+	}
+
 	protected void createAdditionOptions(Composite parent) {
 
 		Composite composite = new Composite(parent, SWT.BORDER);
@@ -532,22 +561,15 @@
 			}
 
 			public boolean isChecked(Object element) {
-				if (profile != null) {
-					IInstallableUnit iu = ProvUI.getAdapter(element, IInstallableUnit.class);
-					IQueryResult<IInstallableUnit> collector = profile.query(QueryUtil.createIUQuery(iu.getId(), new VersionRange(iu.getVersion(), true, null, false)), new NullProgressMonitor());
-					if (collector.isEmpty()) {
-						return true;
-					}
+				IInstallableUnit iu = ProvUI.getAdapter(element, IInstallableUnit.class);
+				if (selectedUnitsToMigrate.contains(iu)) {
+					return true;
 				}
 				return false;
 			}
 		};
 	}
 
-	//	protected ITableLabelProvider getLabelProvider() {
-	//		return new IUDetailsLabelProvider(null, getColumnConfig(), null);
-	//	}
-
 	protected ITreeContentProvider getContentProvider() {
 		ProvElementContentProvider provider = new ProvElementContentProvider() {
 			@Override
@@ -604,8 +626,6 @@
 		boolean pageComplete = determinePageCompletion();
 		setPageComplete(pageComplete);
 		if (pageComplete) {
-			//			if (this instanceof AbstractImportPage_c)
-			//				saveWidgetValues();
 			setMessage(null);
 		}
 	}
diff --git a/bundles/org.eclipse.equinox.p2.ui.sdk.scheduler/src/org/eclipse/equinox/internal/p2/ui/sdk/scheduler/migration/messages.properties b/bundles/org.eclipse.equinox.p2.ui.sdk.scheduler/src/org/eclipse/equinox/internal/p2/ui/sdk/scheduler/migration/messages.properties
index 0063c3b..c92e67f 100644
--- a/bundles/org.eclipse.equinox.p2.ui.sdk.scheduler/src/org/eclipse/equinox/internal/p2/ui/sdk/scheduler/migration/messages.properties
+++ b/bundles/org.eclipse.equinox.p2.ui.sdk.scheduler/src/org/eclipse/equinox/internal/p2/ui/sdk/scheduler/migration/messages.properties
@@ -10,7 +10,7 @@
 MigrationWizard_WINDOWTITLE_FIRSTRUN=Import
 MigrationPage_DIALOG_TITLE=Migration wizard
 MigrationPage_DIALOG_TITLE_FIRSTRUN=Import wizard
-MigrationPage_DIALOG_DESCRIPTION=Because {0} has been updated, the plug-ins you had installed are now disabled.\nThis wizard will help you install those again.
+MigrationPage_DIALOG_DESCRIPTION=This wizard helps you to import and reinstall previously installed plug-ins. It is triggered because\neither you are launching eclipse for the first time or your {0} has been updated.
 MigrationPage_DIALOG_DESCRIPTION_FIRSTRUN=It is the first time you are running {0} from this location.\nThis wizard will help you install plug-ins you may have previously installed.
 MigrationPage_CONFIRMATION_TITLE=Migration cancellation
 MigrationPage_CONFIRMATION_DIALOG=Canceling this operation will stop the re-installation of your plug-ins. Are you sure you want to cancel?