Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Hooking into application startup after the UI has been constructed
Hooking into application startup after the UI has been constructed [message #641893] Mon, 29 November 2010 07:48 Go to next message
Ned Twigg is currently offline Ned TwiggFriend
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
Re: Hooking into application startup after the UI has been constructed [message #641981 is a reply to message #641893] Mon, 29 November 2010 12:46 Go to previous message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Ned Twigg wrote:
> I am trying to get certain UI parts of the Application.e4xmi, as well as
> certain services (namely EPartService) on application startup.

You have to be more specific about what you are targetting. That really
does make a difference. For example, are you trying to do something
when your MPart is instantiated? An arbitrary MPart? on MWindow
creation? Instantiation?

That will determine what kind of event handler to create.

For the error log, sure, add your stack trace (preferrable as an
attached file :-) to the bug.

Later,
PW

--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Platform_Expression_Framework
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse .platform.doc.isv/guide/workbench.htm


Previous Topic:EMF editor/generator with e4
Next Topic:How to properly center an e4 Application Window?
Goto Forum:
  


Current Time: Thu Apr 25 23:32:23 GMT 2024

Powered by FUDForum. Page generated in 0.03014 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top