blob: 238cba5a800b11baca5cc8cc24a89d57f0f36ec2 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2013, 2014 Obeo and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.emf.compare.ide.ui.internal.util;
import org.eclipse.compare.IResourceProvider;
import org.eclipse.compare.ITypedElement;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.Platform;
/**
* This provides access to commonly used functions for platform elements, such as adaptation or file lookup.
*
* @author <a href="mailto:laurent.goubet@obeo.fr">Laurent Goubet</a>
*/
public class PlatformElementUtil {
/**
* Try and determine the resource of the given element.
*
* @param element
* The element for which we need an {@link IResource}.
* @return The resource corresponding to the given {@code element} if we could find it, <code>null</code>
* otherwise.
*/
public static IFile findFile(ITypedElement element) {
if (element == null) {
return null;
}
// Can we adapt it directly?
IResource resource = adaptAs(element, IResource.class);
if (resource == null) {
// We know about some types ...
if (element instanceof IResourceProvider) {
resource = ((IResourceProvider)element).getResource();
}
}
if (resource instanceof IFile) {
return (IFile)resource;
}
// Try with IFile in case adapter only checked for class equality
return adaptAs(element, IFile.class);
}
/**
* Tries and adapt the given <em>object</em> to an instance of the given class.
*
* @param <T>
* Type to which we need to adapt <em>object</em>.
* @param object
* The object we need to coerce to a given {@link Class}.
* @param clazz
* Class to which we are to adapt <em>object</em>.
* @return <em>object</em> cast to type <em>T</em> if possible, <code>null</code> if not.
*/
public static <T> T adaptAs(Object object, Class<T> clazz) {
if (object == null) {
return null;
}
T result = null;
if (clazz.isInstance(object)) {
result = clazz.cast(object);
} else if (object instanceof IAdaptable) {
result = clazz.cast(((IAdaptable)object).getAdapter(clazz));
}
if (result == null) {
result = clazz.cast(Platform.getAdapterManager().loadAdapter(object, clazz.getName()));
}
return result;
}
}