}
public E4Workbench createE4Workbench(IApplicationContext applicationContext, Application jfxApplication, Stage primaryStage) {
args = (String[]) applicationContext.getArguments().get(IApplicationContext.APPLICATION_ARGS);
IEclipseContext appContext = createDefaultContext();
//FIXME We need to fix this later on see ticket 256
// ContextInjectionFactory.setDefault(appContext);
try {
Method m = ContextInjectionFactory.class.getMethod("setDefault", IEclipseContext.class);
m.invoke(null, appContext);
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e1) {
System.err.println("WARNING: You are running on an old and buggy DI container which is fixed in 4.2.2 builds. Consider upgradeing.");
}
appContext.set(Application.class, jfxApplication);
appContext.set("primaryStage", primaryStage);
// appContext.set(Realm.class, SWTObservables.getRealm(display));
appContext.set(UISynchronize.class, new UISynchronize() {
public void syncExec(final Runnable runnable) {
if (javafx.application.Platform.isFxApplicationThread()) {
runnable.run();
} else {
RunnableFuture<?> task = new FutureTask<Void>(runnable, null);
javafx.application.Platform.runLater(task);
try {
// wait for task to complete
task.get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
task.cancel(true);
}
}
}
public void asyncExec(Runnable runnable) {
javafx.application.Platform.runLater(runnable);
}
});
appContext.set(IApplicationContext.class, applicationContext);
appContext.set(IResourceUtilities.class, new IResourceUtilities<Image>() {
private WeakHashMap<URI, WeakReference<Image>> imageCache = new WeakHashMap<URI, WeakReference<Image>>();
public Image imageDescriptorFromURI(URI iconPath) {
WeakReference<Image> r = imageCache.get(iconPath);
Image img = null;
if( r != null ) {
img = r.get();
}
if( img == null ) {
try {
InputStream in = new URL(iconPath.toString()).openStream();
img = new Image(in);
in.close();
imageCache.put(iconPath, new WeakReference<Image>(img));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
logger.warning("could not find icon at: " + iconPath,e);
}
}
return img;
}
});
// Check if DS is running
if (!appContext.containsKey("org.eclipse.e4.ui.workbench.modeling.EModelService")) {
throw new IllegalStateException("Core services not available. Please make sure that a declarative service implementation (such as the bundle 'org.eclipse.equinox.ds') is available!");
}
// Get the factory to create DI instances with
IContributionFactory factory = (IContributionFactory) appContext.get(IContributionFactory.class.getName());
// Install the life-cycle manager for this session if there's one
// defined
String lifeCycleURI = getArgValue(E4Workbench.LIFE_CYCLE_URI_ARG, applicationContext, false);
if (lifeCycleURI != null) {
lcManager = factory.create(lifeCycleURI, appContext);
if (lcManager != null) {
// Let the manager manipulate the appContext if desired
Boolean rv = (Boolean) ContextInjectionFactory.invoke(lcManager, PostContextCreate.class, appContext, Boolean.TRUE);
if( rv != null && ! rv.booleanValue() ) {
return null;
}
}
}
// Create the app model and its context
MApplication appModel = loadApplicationModel(applicationContext, appContext);
appModel.setContext(appContext);
// Set the app's context after adding itself
appContext.set(MApplication.class.getName(), appModel);
// let the life cycle manager add to the model
if (lcManager != null) {
ContextInjectionFactory.invoke(lcManager, ProcessAdditions.class, appContext, null);
ContextInjectionFactory.invoke(lcManager, ProcessRemovals.class, appContext, null);
}
// Create the addons
IEclipseContext addonStaticContext = EclipseContextFactory.create();
for (MAddon addon : appModel.getAddons()) {
addonStaticContext.set(MAddon.class, addon);
Object obj = factory.create(addon.getContributionURI(), appContext, addonStaticContext);
addon.setObject(obj);
}
// Parse out parameters from both the command line and/or the product