This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Feb 14th, 2012  |  syntax: None  |  size: 2.34 KB  |  views: 93  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package com.example.e4.extensionfactory;
  2.  
  3. import org.eclipse.core.runtime.CoreException;
  4. import org.eclipse.core.runtime.IConfigurationElement;
  5. import org.eclipse.core.runtime.IExecutableExtension;
  6. import org.eclipse.core.runtime.IExecutableExtensionFactory;
  7. import org.eclipse.core.runtime.IStatus;
  8. import org.eclipse.core.runtime.Status;
  9. import org.eclipse.e4.core.contexts.EclipseContextFactory;
  10. import org.eclipse.e4.core.contexts.IEclipseContext;
  11. import org.eclipse.e4.core.di.InjectionException;
  12. import org.eclipse.e4.core.services.contributions.IContributionFactory;
  13. import org.osgi.framework.Bundle;
  14. import org.osgi.framework.BundleContext;
  15. import org.osgi.framework.FrameworkUtil;
  16. import org.osgi.util.tracker.ServiceTracker;
  17.  
  18. public class MyExtensionFactory implements IExecutableExtensionFactory,
  19.                 IExecutableExtension {
  20.  
  21.         private ServiceTracker bundleTracker = null;
  22.         private IConfigurationElement config;
  23.         private String id;
  24.         private String propertyName;
  25.         private BundleContext context;
  26.  
  27.         @Override
  28.         public Object create() throws CoreException {
  29.                 Bundle bundle = FrameworkUtil.getBundle(getClass());
  30.                 BundleContext bundleContext = bundle.getBundleContext();
  31.                 IEclipseContext eclipseCtx = EclipseContextFactory
  32.                                 .getServiceContext(bundleContext);
  33.                 IContributionFactory contributionFactory = (IContributionFactory) eclipseCtx
  34.                                 .get(IContributionFactory.class.getName());
  35.  
  36.                 IEclipseContext localContext = EclipseContextFactory.create();
  37.                 Object object = null;
  38.                 try {
  39.                         System.out.println("This is the View id: " + id);
  40.                         context = FrameworkUtil.getBundle(this.getClass())
  41.                                         .getBundleContext();
  42.                         object = contributionFactory
  43.                                         .create("bundleclass:de.vogella.rcp.di.test/de.vogella.rcp.di.test.View",
  44.                                                         localContext);
  45.                 } catch (InjectionException e) {
  46.                         e.printStackTrace();
  47.                 }
  48.                 return object;
  49.         }
  50.  
  51.         public void setInitializationData(IConfigurationElement config,
  52.                         String propertyName, Object data) throws CoreException {
  53.                 System.out.println("Initialization is calleed");
  54.                 if (data instanceof String) {
  55.                         id = (String) data;
  56.                 } else {
  57.                         throw new CoreException(new Status(IStatus.ERROR,
  58.                                         "com.example.e4.extensionfactory", 0,
  59.                                         "Data argument must be a String for " + getClass(), null)); //$NON-NLS-1$
  60.                 }
  61.                 this.config = config;
  62.                 this.propertyName = propertyName;
  63.         }
  64.  
  65. }
clone this paste RAW Paste Data