How can I get the current shell from inside a handler (e.g. to pass into a MessageBox)

688 views
Skip to first unread message

Java Dev

unread,
Oct 7, 2012, 10:34:55 AM10/7/12
to vog...@googlegroups.com
Hi,

In page 193 of your book you mention examples of SWT system dialogs which all take a shell as parameter. E.g.
new MessageBox(shell, SWT.ICON_QUESTION | SWT.OK | SWT.CANCEL);

But there are other occusions where a shell is needed.
What I don't understand is which shell you pass in.
E.g. if in a Handler I want to call a Wizard how can I get the current shell to use?
I tried:
Display.getCurrent().getActiveShell();
But failed.
I also tried:

@Inject
Shell theShell

In my Handler and also failed.

Am I supposed to do "new Shell()" each time? If not how can I get the current shell?

Thank you

Lars Vogel

unread,
Oct 7, 2012, 4:58:24 PM10/7/12
to vog...@googlegroups.com

Hi,

@Inject Shell shell;

Should work in application model elements.

Best regards, Lars

--
You received this message because you are subscribed to the Google Groups "vogella" group.
To view this discussion on the web visit https://groups.google.com/d/msg/vogella/-/GrxEWydGZY0J.
To post to this group, send email to vog...@googlegroups.com.
To unsubscribe from this group, send email to vogella+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/vogella?hl=en.

Java Dev

unread,
Oct 7, 2012, 5:05:04 PM10/7/12
to vog...@googlegroups.com
Hi,

I tried that (DI) in a Handler "attached" to a Command.
I got:

org.eclipse.e4.core.di.InjectionException: Unable to process "NewTodoHandler.shell": no actual value was found for the argument "Shell".

Aren't Handlers attached to Commands application model elements? If not what should I do instead?
 

Lars Vogel

unread,
Oct 7, 2012, 5:07:16 PM10/7/12
to vog...@googlegroups.com

For a handler put the parameter into the @Execute method of the handler or use @Optional for fields.

At initialization Shell is not yet available

To view this discussion on the web visit https://groups.google.com/d/msg/vogella/-/sKM3hFrCw1sJ.

Java Dev

unread,
Oct 13, 2012, 4:22:31 AM10/13/12
to vog...@googlegroups.com
Hi,

Using a parameter in the handler works.
But using:
@Inject
@Optional
Shell currentShell;

In my handler does not work and I get RuntimeException.



Lars Vogel

unread,
Oct 15, 2012, 3:40:46 AM10/15/12
to vog...@googlegroups.com
Hi,

I think that should also work.

Can you please create a bug report for that and attach a small example
project to the bug report? Once done please send me the bug number.

https://bugs.eclipse.org/bugs/ Component Platform/UI

Best regards, Lars

2012/10/13 Java Dev <flying...@yahoo.com>:
> https://groups.google.com/d/msg/vogella/-/odx-xstSU7sJ.

Java Dev

unread,
Oct 20, 2012, 7:13:29 AM10/20/12
to vog...@googlegroups.com
Hi,
I created a trivial eclipse application to demostrate the bug, but it is not reproducable!
I am not sure what is the difference in calling the handler in the trivial application and my real application....
Will look into this more.
Thank you for the moment. If from your experience you suspect something on this scenario please let me know.

Java Dev

unread,
Oct 20, 2012, 7:24:15 AM10/20/12
to vog...@googlegroups.com
Found it!
The currrentShell from inject is null but the
MessageDialog.openInformation(currentShell, "Dialog opens!", "Dialog opens!!!");
does not throw an exception and tricked me!
Will open a bug and let you know!

Java Dev

unread,
Oct 20, 2012, 8:15:19 AM10/20/12
to vog...@googlegroups.com

Java Dev

unread,
Oct 23, 2012, 4:56:42 PM10/23/12
to vog...@googlegroups.com
Hi,
Concerning this, the answer for the bug ticket was:
You can't use @Inject on handlers at this time.  Anything you want to use during @CanExecute or @Execute you must request as a parameter.
Paul Webster

Lars Vogel

unread,
Oct 23, 2012, 4:59:49 PM10/23/12
to vog...@googlegroups.com

Bug Number?

To view this discussion on the web visit https://groups.google.com/d/msg/vogella/-/_L8tpJfMTeMJ.

Java Dev

unread,
Oct 23, 2012, 5:09:38 PM10/23/12
to vog...@googlegroups.com

Lars Vogel

unread,
Oct 24, 2012, 5:04:20 AM10/24/12
to vog...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages