Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Starting command at startup
Starting command at startup [message #902496] Fri, 17 August 2012 18:48 Go to next message
Markus Oley is currently offline Markus Oley
Messages: 253
Registered: July 2009
Location: Germany
Senior Member
Hi again,

I want to call a command at startup of application, which should open a specific shell anytime I'm starting my e4-application with a special command line argument.
Reading the arguments I've succeded, but where should I call the command itself?

The command is not implemented in the plugin, where I have defined my product in and I don't want any dependency from product-plugin to plugin which includes the command. Are there any lifecycle hooks available in this constellation?

Can you give me any best practice for this scenario?

Thanks by now
Markus
Re: Starting command at startup [message #902534 is a reply to message #902496] Sat, 18 August 2012 04:41 Go to previous messageGo to next message
Sopot Cela is currently offline Sopot Cela
Messages: 597
Registered: December 2010
Senior Member

Sure. The framework has @PostContextCreate which denotes the moment when the main application context is created (which is among the first things in the lifecycle). See here for details how to apply it. Also see the related bug in the comments. http://www.toedter.com/blog/?p=896
Re: Starting command at startup [message #902613 is a reply to message #902534] Sat, 18 August 2012 18:57 Go to previous messageGo to next message
Markus Oley is currently offline Markus Oley
Messages: 253
Registered: July 2009
Location: Germany
Senior Member
Hi Sopot,

thank you for your answer.
The main problem, I still have, is, that there is no active shell available in this method. I tried to inject the active shell, but this is always null.

Do you have other suggestions?

Best regards

[Updated on: Sat, 18 August 2012 19:13]

Report message to a moderator

Re: Starting command at startup [message #902792 is a reply to message #902613] Mon, 20 August 2012 09:23 Go to previous messageGo to next message
Joseph Carroll is currently offline Joseph Carroll
Messages: 174
Registered: May 2012
Location: Milwaukee, WI
Senior Member

The main problem I see is that you want to be aware of something that the model doesn't know about (there isn't a "shell" model object). The workbench is aware of the shell, so once you are operating within the workbench you can have a shell injected, however, this is purely convenience.

The way you can accomplish this (albeit a hack), is to listen for the creation of a workbench window. You would still want to use the life-cycle hook @PostContextCreate (or @ProcessAdittions doesn't matter), but only you would register an event handler with the event admin on the topic UIEvents.Window.TOPIC_ALL and look for the create event (testable through the method isCREATE). Then you would be able to get the active shell (atleast in theory). Once you have executed your code, I would recommend removing your listener from the event admin service.

I'll double check later today to make sure this works. The one issue you may run into is the selection service may not have been populated at this point. IF that is the case, I would believe that to be a bug, but as I said that would be the one caveat that might creep up.

I'll try to model this myself later today and post.

JD
Re: Starting command at startup [message #902807 is a reply to message #902792] Mon, 20 August 2012 10:36 Go to previous messageGo to next message
Christoph Keimel is currently offline Christoph Keimel
Messages: 347
Registered: December 2010
Location: Germany
Senior Member
Another possibility is to create an AddOn. We have an Addon that maximizes the shell when a MWindow gets created. The EventHandler is only added when a specific Argument is passed to the application:

public class MaximizeAddon {
	@SuppressWarnings("unused")
	private Logger logger = LoggerFactory.getLogger(MaximizeAddon.class);
	
	public static final String APP_ARG_MAXIMIZE = "-maximize";
	
	private EventHandler handler = new EventHandler() {
		@Override
		public void handleEvent(Event event) {
			if (!UIEvents.isSET(event))
				return;
			
			Object objElement = event.getProperty(UIEvents.EventTags.ELEMENT);
			if (!(objElement instanceof MWindow))
				return;

			MWindow windowModel = (MWindow) objElement;
			windowModel.getTags().add("shellMaximized");
		}
	};

	@PostConstruct
	public void init(IEventBroker eventBroker, IEclipseContext context) {
		if (Arrays.asList(Platform.getApplicationArgs()).contains(APP_ARG_MAXIMIZE)) {
			eventBroker.subscribe(UIEvents.UIElement.TOPIC_WIDGET, handler);
		}
	}
	
	@PreDestroy
	public void dispose(IEventBroker eventBroker) {
		if (Arrays.asList(Platform.getApplicationArgs()).contains(APP_ARG_MAXIMIZE)) {
			eventBroker.unsubscribe(handler);
		}
	}
}


Greetings
Christoph

[Updated on: Fri, 24 August 2012 05:53]

Report message to a moderator

Re: Starting command at startup [message #903550 is a reply to message #902807] Fri, 24 August 2012 05:10 Go to previous messageGo to next message
Lars Vogel is currently offline Lars Vogel
Messages: 1049
Registered: July 2009
Senior Member

Hi Christoph,

just for reference, if you always want to maximize you could use the tag "shellMaximized" on the Window application model element.

I assume you know this already, but wanted to add this is someone else reads this thread.

Best regards, Lars
Re: Starting command at startup [message #903556 is a reply to message #903550] Fri, 24 August 2012 05:50 Go to previous messageGo to next message
Christoph Keimel is currently offline Christoph Keimel
Messages: 347
Registered: December 2010
Location: Germany
Senior Member
Hi Lars,

no, I didn't know that, so thank you very much for the hint!

I modified my Handler, so it uses the tag because I still want to controll the behavior with an application argument. (Changes to the code are included in the original post.)

Smile,
Christoph

[Updated on: Fri, 24 August 2012 05:54]

Report message to a moderator

Re: Starting command at startup [message #908463 is a reply to message #902496] Wed, 05 September 2012 08:28 Go to previous messageGo to next message
Markus Oley is currently offline Markus Oley
Messages: 253
Registered: July 2009
Location: Germany
Senior Member
Hi Lars,

didn't know this either,
thanks so far, this simplifies my application. Smile

Best regards
Markus
Re: Starting command at startup [message #908469 is a reply to message #908463] Wed, 05 September 2012 08:39 Go to previous messageGo to next message
Sopot Cela is currently offline Sopot Cela
Messages: 597
Registered: December 2010
Senior Member

It may look complicated but taking a look at the e4 renderers will prove really useful. Investing an afternoon looking at them will save a lot of unnecessary pain.
I myself have seen this thing first with Lars when looking at the renderers' code. The e4 renderers (esp. Stack and Part) have lots of behavior tags that you can easily specify to the model and save yourself a lot of trouble. The bad thing is that they are poorly documented, or to better say they are not documented anywhere but in the code. Also Eric the guy who wrote most of the renderers doesn't hang out on the forums.

So my humble advice is to keep org.eclipse.e4.ui.workbench.renderers.swt in some remote place just for reference if something looks complicated. Truth is most of things are easy to achieve in e4 but we haven't had time to document them yet (except Lars' epic tutorials Smile ).
Re: Starting command at startup [message #946782 is a reply to message #902496] Tue, 16 October 2012 09:58 Go to previous messageGo to next message
Istvan Meszaros is currently offline Istvan Meszaros
Messages: 43
Registered: October 2009
Member
Adding the
shellmaximized
tag either by an AddOn or to the tags of my window in the application XMI does not cause my window to open maximized.

To be specific, i have a Trimmed Window instead of a normal Window. Does that matter?
Re: Starting command at startup [message #946962 is a reply to message #946782] Tue, 16 October 2012 13:25 Go to previous messageGo to next message
Sopot Cela is currently offline Sopot Cela
Messages: 597
Registered: December 2010
Senior Member

Try the case-sensitive version.
Re: Starting command at startup [message #948560 is a reply to message #946962] Thu, 18 October 2012 02:24 Go to previous message
Istvan Meszaros is currently offline Istvan Meszaros
Messages: 43
Registered: October 2009
Member
With "shellMaximized" it works. Thanks.
Previous Topic:Porting RAP to Eclipse4
Next Topic:Creating menu items at runtime
Goto Forum:
  


Current Time: Sat Aug 30 02:39:30 EDT 2014

Powered by FUDForum. Page generated in 0.04915 seconds