480539: Neon: org.eclipse.ui.internal.util.Util.getAdapter removed

Change-Id: Ie9612472980ee81b718d289488bcf1e0a9a7764d
Task-Url: https://bugs.eclipse.org/bugs/show_bug.cgi?id=480539
diff --git a/org.eclipse.mylyn.commons.repositories.ui/src/org/eclipse/mylyn/commons/repositories/ui/RepositoryUi.java b/org.eclipse.mylyn.commons.repositories.ui/src/org/eclipse/mylyn/commons/repositories/ui/RepositoryUi.java
index f6bd3b8..a7cf2a8 100644
--- a/org.eclipse.mylyn.commons.repositories.ui/src/org/eclipse/mylyn/commons/repositories/ui/RepositoryUi.java
+++ b/org.eclipse.mylyn.commons.repositories.ui/src/org/eclipse/mylyn/commons/repositories/ui/RepositoryUi.java
@@ -18,6 +18,7 @@
 import org.eclipse.mylyn.commons.ui.dialogs.ValidatableWizardDialog;
 import org.eclipse.mylyn.internal.commons.repositories.ui.Messages;
 import org.eclipse.mylyn.internal.commons.repositories.ui.RepositoriesUiPlugin;
+import org.eclipse.mylyn.internal.commons.repositories.ui.RepositoryUiUtil;
 import org.eclipse.mylyn.internal.commons.repositories.ui.wizards.NewRepositoryWizard;
 import org.eclipse.swt.widgets.Shell;
 import org.eclipse.ui.IEditorInput;
@@ -25,7 +26,6 @@
 import org.eclipse.ui.IWorkbenchPart;
 import org.eclipse.ui.IWorkbenchWindow;
 import org.eclipse.ui.internal.LegacyResourceSupport;
-import org.eclipse.ui.internal.util.Util;
 
 /**
  * @author Steffen Pingel
@@ -64,7 +64,7 @@
 				IWorkbenchPart part = workbenchWindow.getPartService().getActivePart();
 				if (part instanceof IEditorPart) {
 					IEditorInput input = ((IEditorPart) part).getEditorInput();
-					Object resource = Util.getAdapter(input, resourceClass);
+					Object resource = RepositoryUiUtil.adapt(input, resourceClass);
 					if (resource != null) {
 						selectionToPass = new StructuredSelection(resource);
 					}
diff --git a/org.eclipse.mylyn.commons.repositories.ui/src/org/eclipse/mylyn/internal/commons/repositories/ui/RepositoryUiUtil.java b/org.eclipse.mylyn.commons.repositories.ui/src/org/eclipse/mylyn/internal/commons/repositories/ui/RepositoryUiUtil.java
index 4cd155c..06d2155 100644
--- a/org.eclipse.mylyn.commons.repositories.ui/src/org/eclipse/mylyn/internal/commons/repositories/ui/RepositoryUiUtil.java
+++ b/org.eclipse.mylyn.commons.repositories.ui/src/org/eclipse/mylyn/internal/commons/repositories/ui/RepositoryUiUtil.java
@@ -11,15 +11,20 @@
 

 package org.eclipse.mylyn.internal.commons.repositories.ui;

 

+import java.lang.reflect.Method;

+

 import org.eclipse.core.runtime.IStatus;

+import org.eclipse.core.runtime.Platform;

 import org.eclipse.core.runtime.Status;

 import org.eclipse.jface.dialogs.DialogPage;

 import org.eclipse.mylyn.commons.core.StatusHandler;

 import org.eclipse.mylyn.commons.repositories.core.auth.ICredentialsStore;

 import org.eclipse.mylyn.commons.repositories.core.auth.UnavailableException;

+import org.eclipse.mylyn.commons.ui.PlatformUiUtil;

 import org.eclipse.mylyn.internal.commons.repositories.core.LocationService;

 import org.eclipse.mylyn.internal.commons.repositories.core.RepositoriesCoreInternal;

 import org.eclipse.osgi.util.NLS;

+import org.osgi.framework.Bundle;

 

 public class RepositoryUiUtil {

 

@@ -34,9 +39,31 @@
 		try {

 			store.testAvailability();

 		} catch (UnavailableException e) {

-			StatusHandler.log(new Status(IStatus.ERROR, RepositoriesCoreInternal.ID_PLUGIN, NLS.bind(

-					"Credential store not available with ID {0}", id), e)); //$NON-NLS-1$

+			StatusHandler.log(new Status(IStatus.ERROR, RepositoriesCoreInternal.ID_PLUGIN,

+					NLS.bind("Credential store not available with ID {0}", id), e)); //$NON-NLS-1$

 			page.setErrorMessage(Messages.RepositoryUiUtil_secure_storage_unavailable);

 		}

 	}

+

+	@SuppressWarnings({ "restriction", "unchecked" })

+	public static <T> T adapt(Object sourceObject, Class<T> adapter) {

+		try {

+			if (PlatformUiUtil.isNeonOrLater()) {

+				Bundle bundle = Platform.getBundle("org.eclipse.platform"); //$NON-NLS-1$

+				Class<?> clazz = bundle.loadClass("org.eclipse.core.runtime.Adapters"); //$NON-NLS-1$

+				Method adaptMethod = clazz.getMethod("adapt", new Class[] { Object.class, Class.class }); //$NON-NLS-1$

+				Object result = adaptMethod.invoke(clazz, sourceObject, adapter);

+				return (T) result;

+			} else {

+				Bundle bundle = Platform.getBundle("org.eclipse.ui.workbench"); //$NON-NLS-1$

+				Class<?> clazz = bundle.loadClass("org.eclipse.ui.internal.util.Util"); //$NON-NLS-1$

+				Method adaptMethod = clazz.getMethod("getAdapter", new Class[] { Object.class, Class.class }); //$NON-NLS-1$

+				Object result = adaptMethod.invoke(clazz, sourceObject, adapter);

+				return (T) result;

+			}

+		} catch (Exception e) {

+			StatusHandler.log(new Status(IStatus.ERROR, RepositoriesUiPlugin.ID_PLUGIN, "Could not adapt", e)); //$NON-NLS-1$

+		}

+		return null;

+	}

 }

diff --git a/org.eclipse.mylyn.commons.ui/src/org/eclipse/mylyn/commons/ui/PlatformUiUtil.java b/org.eclipse.mylyn.commons.ui/src/org/eclipse/mylyn/commons/ui/PlatformUiUtil.java
index 04e368a..98da028 100644
--- a/org.eclipse.mylyn.commons.ui/src/org/eclipse/mylyn/commons/ui/PlatformUiUtil.java
+++ b/org.eclipse.mylyn.commons.ui/src/org/eclipse/mylyn/commons/ui/PlatformUiUtil.java
@@ -33,6 +33,7 @@
 
 	private static class Eclipse36Checker {
 		public static final boolean result;
+
 		static {
 			boolean methodAvailable = false;
 			try {
@@ -62,7 +63,8 @@
 	}
 
 	public static int getToolTipXShift() {
-		if ("gtk".equals(SWT.getPlatform()) || "carbon".equals(SWT.getPlatform()) || "cocoa".equals(SWT.getPlatform())) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+		if ("gtk".equals(SWT.getPlatform()) || "carbon".equals(SWT.getPlatform()) //$NON-NLS-1$//$NON-NLS-2$
+				|| "cocoa".equals(SWT.getPlatform())) { //$NON-NLS-1$
 			return -26;
 		} else {
 			return -23;
@@ -127,7 +129,7 @@
 	 * If a a section does not use a toolbar as its text client the spacing between the section header and client will
 	 * be different from other sections. This method returns the value to set as the vertical spacing on those sections
 	 * to align the vertical position of section clients.
-	 * 
+	 *
 	 * @return value for {@link org.eclipse.ui.forms.widgets.Section#clientVerticalSpacing}
 	 */
 	public static int getToolbarSectionClientVerticalSpacing() {
@@ -204,4 +206,24 @@
 		return internalBrowserAvailable.booleanValue();
 	}
 
+	/**
+	 * @since 3.18
+	 */
+	public static boolean isNeonOrLater() {
+		Bundle bundle = Platform.getBundle("org.eclipse.platform"); //$NON-NLS-1$
+		if (bundle != null) {
+			String versionString = (String) bundle.getHeaders().get("Bundle-Version"); //$NON-NLS-1$
+			Version version = new Version(versionString);
+			return version.compareTo(new Version("4.6.0.v20151020-0800")) >= 0; //$NON-NLS-1$
+		}
+		bundle = Platform.getBundle("org.eclipse.swt"); //$NON-NLS-1$
+		if (bundle != null) {
+			String versionString = (String) bundle.getHeaders().get("Bundle-Version"); //$NON-NLS-1$
+			Version version = new Version(versionString);
+			return version.compareTo(new Version("3.105.0.v20151020-0634")) >= 0; //$NON-NLS-1$
+		}
+		return false;
+
+	}
+
 }