package factOrFiction.extension;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.osgi.framework.Bundle;
public class ExtensionHandler<T> {
String extensionPointID;
IExtensionPoint extensionPoint;
public ExtensionHandler(String extensionPointID) {
this.extensionPointID = extensionPointID;
extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(extensionPointID);
}
@SuppressWarnings("unchecked")
public T createExecutableFromBundle(String symbolicBundleName, String className) {
Bundle bundle = Platform.getBundle(symbolicBundleName);
if(bundle != null) {
Class<T> clazz = null;
try {
clazz = bundle.loadClass(className);
return clazz.newInstance();
} catch (ClassNotFoundException e) {
e.getMessage();
} catch (InstantiationException e) {
e.getMessage();
} catch (IllegalAccessException e) {
e.getMessage();
}
}
return null;
}
@SuppressWarnings("unchecked")
private T createExecutable(IConfigurationElement element) {
try {
Object clazz = element.createExecutableExtension("class");
return (T)clazz;
} catch (CoreException e) {
System.err.println(e.getMessage());
}
return null;
}
public T createExecutable(String extensionId) {
T clazz = null;
if (extensionPoint != null) {
IExtension extension = extensionPoint.getExtension(extensionId);
if(extension != null) {
IConfigurationElement[] elements = extension.getConfigurationElements();
if(elements.length>0)
clazz = createExecutable(elements[0]);
}
}
return clazz;
}
public List<T> createExecutables() {
if (extensionPoint != null) {
IExtension[] extensions = extensionPoint.getExtensions();
if(extensions.length>0) {
List<T> list = new ArrayList<T>();
for (int i = 0; i < extensions.length; i++) {
IConfigurationElement[] elements = extensions[i].getConfigurationElements();
if (elements.length > 0) {
T temp = createExecutable(elements[0]);
if (temp != null)
list.add(temp);
}
}
return list;
}
}
return null;
}
public T createExecutableByValue(String extensionId, String attribute, String value) {
T clazz = null;
if (extensionPoint != null) {
IExtension extension = extensionPoint.getExtension(extensionId);
if(extension != null) {
IConfigurationElement[] elements = extension.getConfigurationElements();
if(elements.length>0) {
for (int i = 0; i < elements.length; i++) {
IConfigurationElement configurationElement = elements[i];
String ceValue = configurationElement.getAttribute(attribute);
if(ceValue.equals(value)) {
return createExecutable(configurationElement);
}
}
}
}
}
return clazz;
}
/**
*
*/
public void dumpExtensions() {
if (extensionPoint != null) {
IExtension[] extensions = extensionPoint.getExtensions();
for (int i = 0; i < extensions.length; i++) {
IExtension temp = extensions[i];
System.out.println("Extension#"+i+" "+temp.getSimpleIdentifier() + "/" + temp.getUniqueIdentifier());
}
}
}
public void scan(IProgressMonitor monitor, String attribute, IExtensionCallback<T> callback) {
if (extensionPoint != null) {
IExtension[] extensions = extensionPoint.getExtensions();
for (int i = 0; i < extensions.length; i++) {
IExtension extension = extensions[i];
if (extension.getLabel() != null && monitor != null)
monitor.setTaskName(extension.getLabel());
IConfigurationElement[] elements = extension.getConfigurationElements();
for (IConfigurationElement element : elements) {
String name = element.getAttribute(attribute);
if (monitor != null)
monitor.subTask(name);
T clazz = createExecutable(element);
if (clazz != null) {
boolean abort = callback.created(extension.getNamespaceIdentifier(), name, clazz);
if (abort)
break;
}
}
if (monitor != null)
monitor.worked(1);
}
}
if(monitor != null)
monitor.done();
}
}