package com.example.e4.extensionfactory;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExecutableExtension;
import org.eclipse.core.runtime.IExecutableExtensionFactory;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.e4.core.contexts.EclipseContextFactory;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.core.di.InjectionException;
import org.eclipse.e4.core.services.contributions.IContributionFactory;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.util.tracker.ServiceTracker;
public class MyExtensionFactory implements IExecutableExtensionFactory,
IExecutableExtension {
private ServiceTracker bundleTracker = null;
private IConfigurationElement config;
private String id;
private String propertyName;
private BundleContext context;
@Override
public Object create() throws CoreException {
Bundle bundle = FrameworkUtil.getBundle(getClass());
BundleContext bundleContext = bundle.getBundleContext();
IEclipseContext eclipseCtx = EclipseContextFactory
.getServiceContext(bundleContext);
IContributionFactory contributionFactory = (IContributionFactory) eclipseCtx
.get(IContributionFactory.class.getName());
IEclipseContext localContext = EclipseContextFactory.create();
Object object = null;
try {
System.out.println("This is the View id: " + id);
context = FrameworkUtil.getBundle(this.getClass())
.getBundleContext();
object = contributionFactory
.create("bundleclass:de.vogella.rcp.di.test/de.vogella.rcp.di.test.View",
localContext);
} catch (InjectionException e) {
e.printStackTrace();
}
return object;
}
public void setInitializationData(IConfigurationElement config,
String propertyName, Object data) throws CoreException {
System.out.println("Initialization is calleed");
if (data instanceof String) {
id = (String) data;
} else {
throw new CoreException(new Status(IStatus.ERROR,
"com.example.e4.extensionfactory", 0,
"Data argument must be a String for " + getClass(), null)); //$NON-NLS-1$
}
this.config = config;
this.propertyName = propertyName;
}
}