Reputation: 11
I am creating eclipse RCP 4.x application. Application consist of multiple perspectives. I want to open default perspective programmatically depending on some condition. Below code is capable of loading perspective.
@Execute
public void execute(MApplication app, EPartService partService,
EModelService modelService) {
MPerspective element =
(MPerspective) modelService.find("com.sanyotechnologyindia.desktop.app.perspective.enduser", app);
// now switch perspective
partService.switchPerspective(element);
}
But I can not put this code in method which is annotated with @PostContextCreate. Can you suggest any solution for this?
================ As per solution suggested by Greg, I tried following code in Application Lifecycle class.
@ProcessAdditions
void processAdditions(MApplication app, EPartService partService,
EModelService modelService){
MPerspective element =
(MPerspective) modelService.find("com.sanyotechnologyindia.desktop.app.perspective.usermanagement", app);
// now switch perspective
partService.switchPerspective(element);
}
Now I am getting following error at line partService.switchPerspective(element);
java.lang.IllegalStateException: Application does not have an active window
================Update:================== Added org.eclipse.osgi.services plugin to dependencies.
@PostContextCreate
public void postContextContext(IEventBroker eventBroker)
{
eventBroker.subscribe(UIEvents.UILifeCycle.APP_STARTUP_COMPLETE,
new AppStartupCompleteEventHandler());
}
private class AppStartupCompleteEventHandler implements EventHandler
{
@Inject private MApplication app;
@Inject private EPartService partService;
@Inject private EModelService modelService;
@Override
public void handleEvent(Event arg0) {
MPerspective element =
(MPerspective) modelService.find("com.sanyotechnologyindia.desktop.app.perspective.usermanagement", app);
partService.switchPerspective(element);
}
}
However now framework not able to inject MApplication,EPartService and EModelService in AppStartupCompleteEventHandler instance.
Upvotes: 1
Views: 1956
Reputation: 111142
If you only want to do this in your life cycle class try putting it in a @ProcessAdditions
method rather than @PostContextCreate
. @ProcessAdditions
runs later in the life cycle just before the model is rendered.
Update:
Even @PostAdditions
is too early to do some UI operations. You need to wait for the application start complete event. You can subscribe to this event using the event broker in the @PostContextCreate
method:
@PostContextCreate
public void postContextContext(IEventBroker eventBroker)
{
eventBroker.subscribe(UIEvents.UILifeCycle.APP_STARTUP_COMPLETE,
new AppStartupCompleteEventHandler());
}
private class AppStartupCompleteEventHandler implements EventHandler
{
@Override
public void handleEvent(final Event event)
{
// TODO do UI operations here
}
}
EventHandler
is org.osgi.service.event.EventHandler
.
Update: If you want to use injection in the event handler you must create the handler using `ContextInjectionFactory':
EventHandler handler = ContextInjectionFactory.make(AppStartupCompleteEventHandler.class, context);
where context
is the IEclipseContext
.
Note: You can't use this for a non-static inner class, instead use:
EventHandler handler = new AppStartupCompleteEventHandler();
ContextInjectionFactory.inject(handler, context);
This method does not support injection on the constructor.
Upvotes: 1