Hooking into application startup after the UI has been constructed [message #641893] |
Mon, 29 November 2010 07:48 |
Ned Twigg Messages: 3 Registered: October 2010 |
Junior Member |
|
|
I am trying to get certain UI parts of the Application.e4xmi, as well as certain services (namely EPartService) on application startup.
Addons are created before the GUI and some services. So if you try to look for parts of your Application.e4xmi inside the @PostConstruct of your Addon, you're not going to find them.
So, in my Addon, I register with the event handler for this:
public class MyAddon {
//determine when to hook it all up
@Inject
private IEventBroker eventBroker;
@Inject
private IEclipseContext eclipseContext;
private EventHandler handler;
private int numEventsHandled;
private static final int NUM_TO_HANDLE = 11;
@PostConstruct
void hookListeners() {
String topic = UIEvents.buildTopic(UIEvents.UIElement.TOPIC, UIEvents.UIElement.WIDGET);
handler = new EventHandler(){
@Override
public void handleEvent(Event event) {
if(++numEventsHandled == NUM_TO_HANDLE){
eventBroker.unsubscribe(handler);
init(eclipseContext);
}
}
};
eventBroker.subscribe(topic, handler);
}
//hook it all up
void init(IEclipseContext context){
staticPartService = (EPartService) context.get(EPartService.class);
staticApplication = (MApplication) context.get(MApplication.class);
staticModelService = (EModelService) context.get(EModelService.class);
if(staticPartService == null){
System.err.println("Cannot find EPartService");
}
if(staticApplication == null){
System.err.println("Cannot find MApplication");
}
if(staticModelService == null){
System.err.println("Cannot find EModelService");
}
}
private static EPartService staticPartService;
private static MApplication staticApplication;
private static EModelService staticModelService;
public static EPartService getPartService(){
return staticPartService;
}
public static MApplication getApplication() {
return staticApplication;
}
public static EModelService getModelService() {
return staticModelService;
}
}
So this is clearly very brittle, because whenever I change my UI, that changes what NUM_TO_HANDLE needs to be. So what is the best way to subscribe to an event for "UI is done"?
Also, my code creates / exposes a strange bug. Using the above code, when I make a new Shell visible under any circumstances, I get the following error:
!ENTRY org.eclipse.e4.ui.workbench 4 0 2010-11-28 23:16:53.327
!MESSAGE Internal error, please post the trace to bug 315270
!STACK 0
java.lang.Exception
at org.eclipse.e4.ui.internal.workbench.swt.E4Application$2.log(E4Application.java:369)
at org.eclipse.e4.ui.internal.workbench.swt.E4Application$2.compute(E4Application.java:427)
at org.eclipse.e4.core.internal.contexts.ValueComputation.get(ValueComputation.java:122)
...
I get the error whether I'm calling open() on a JFace Dialog, or setVisible(true) on an SWT Shell (but I can create a Shell / Dialog without error, so long as I don't open it). But, if you comment out the
staticPartService = (EPartService) context.get(EPartService.class);
line, then the error goes away (regardless of whether I ever use that EPartService or not).
Should I contribute this to bug 315270, or is this something new or an artifact of my misuse?
Thanks!
-Ned
|
|
|
|
Powered by
FUDForum. Page generated in 0.03933 seconds