Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » E4 annotations in Dialogs(Can E4 annotations (@Inject, @PostConstruct, etc.) be used in dialogs?)
E4 annotations in Dialogs [message #1231920] Wed, 15 January 2014 18:50 Go to next message
Joseph Gagnon is currently offline Joseph Gagnon
Messages: 58
Registered: June 2013
Member
In my E4 application I have a search dialog that is launched from a menu command/handler. When the user fills in search criteria and clicks the search button, I want to be able to launch an asynchronous job to perform the search. Once the search completes, another part will be notified of this event and display the search results returned.

I'm running into a problem where the dialog logic does not seem to honor several of the E4 annotations, such as @Inject and @PostConstruct. I think this is due to the fact that I am creating the dialog object directly in the aforementioned handler. Is that a correct assumption? Is there a way to "inject" a dialog instead of instaniating it? Or, is there a way to have the dialog honor the annotations when it is directly instantiated?

I'd like to be able to inject an event broker in the dialog so that I can publish an event that some other subscriber will listen for. In the search handler I need to be able to initiate the search when the user clicks OK on the dialog.

Search dialog handler:

public class SearchDialogHandler {

  // @Inject
  // private IEventBroker eventBroker;

  @Execute
  public void openDialog(Stage stage) {
    MessageSearchDialog dialog = new MessageSearchDialog(stage, "Message Search");
    int response = dialog.open();

    if (response == Dialog.OK_BUTTON) {
      dialog.performSearch();
    }

    // if (response == Dialog.OK_BUTTON) {
    // SearchParameters searchParams = dialog.getModel().getSearchParams();
    //
    // Map<String, Object> params = new HashMap<String, Object>();
    // params.put(EventProperty.SEARCH_PARAMS, searchParams);
    //
    // eventBroker.send(EventTopic.PERFORM_MESSAGE_SEARCH, params);
    // }
  }

}


The MessageSearchDialog class is an extension of org.eclipse.fx.ui.dialogs.Dialog.
Re: E4 annotations in Dialogs [message #1231994 is a reply to message #1231920] Thu, 16 January 2014 00:31 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5320
Registered: July 2009
Senior Member
On 15.01.14 19:50, Joseph Gagnon wrote:
> In my E4 application I have a search dialog that is launched from a menu
> command/handler. When the user fills in search criteria and clicks the
> search button, I want to be able to launch an asynchronous job to
> perform the search. Once the search completes, another part will be
> notified of this event and display the search results returned.
>
> I'm running into a problem where the dialog logic does not seem to honor
> several of the E4 annotations, such as @Inject and @PostConstruct. I
> think this is due to the fact that I am creating the dialog object
> directly in the aforementioned handler. Is that a correct assumption? Is

Yes if you create stuff your own they are not controlled by DI unless
you create the instance through ContextInjectionFactory.make or use
ContextInjectionFactory.inject later on

> there a way to "inject" a dialog instead of instaniating it? Or, is
> there a way to have the dialog honor the annotations when it is directly
> instantiated?

In Luna you'll be able to model dialogs as part of the application model
but that's not yet done.

>
> I'd like to be able to inject an event broker in the dialog so that I
> can publish an event that some other subscriber will listen for. In the
> search handler I need to be able to initiate the search when the user
> clicks OK on the dialog.
>

You should not use @Inject in handlers. I'd suggest you modify the
openDialog to openDialog(Stage stage, IEventBroker eventBroker)

> Search dialog handler:
>
>
> public class SearchDialogHandler {
>
> // @Inject
> // private IEventBroker eventBroker;
>
> @Execute
> public void openDialog(Stage stage) {
> MessageSearchDialog dialog = new MessageSearchDialog(stage, "Message
> Search");
> int response = dialog.open();
>
> if (response == Dialog.OK_BUTTON) {
> dialog.performSearch();
> }
>
> // if (response == Dialog.OK_BUTTON) {
> // SearchParameters searchParams = dialog.getModel().getSearchParams();
> //
> // Map<String, Object> params = new HashMap<String, Object>();
> // params.put(EventProperty.SEARCH_PARAMS, searchParams);
> //
> // eventBroker.send(EventTopic.PERFORM_MESSAGE_SEARCH, params);
> // }
> }
>
> }
>
>
> The MessageSearchDialog class is an extension of
> org.eclipse.fx.ui.dialogs.Dialog.
Re: E4 annotations in Dialogs [message #1232088 is a reply to message #1231994] Thu, 16 January 2014 07:53 Go to previous messageGo to next message
Mihael Schmidt is currently offline Mihael Schmidt
Messages: 27
Registered: August 2010
Junior Member
Thomas Schindl wrote on Thu, 16 January 2014 01:31
On 15.01.14 19:50,
You should not use @Inject in handlers. I'd suggest you modify the
openDialog to openDialog(Stage stage, IEventBroker eventBroker)


What is the reason to not use @Inject in handlers?

I use it extensively to inject e4 stuff and my own services into handlers. And it seems to work fine.
Re: E4 annotations in Dialogs [message #1232094 is a reply to message #1232088] Thu, 16 January 2014 08:13 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk Fauth
Messages: 1284
Registered: July 2012
Senior Member
Because the handler instances are only created once using one context (I think it's the application context), but calling the command can be done from various contexts. So for the event broker this might not be an issue as usually there aren't several event broker in one application. But if you think of an event that is operating on the current part you will get issues.

That's why there is also no re-injection in handlers as explained here: http://wiki.eclipse.org/Eclipse4/RCP/FAQ#Why_aren.27t_my_handler_fields_being_re-injected.3F
Re: E4 annotations in Dialogs [message #1232112 is a reply to message #1232094] Thu, 16 January 2014 09:06 Go to previous messageGo to next message
Mihael Schmidt is currently offline Mihael Schmidt
Messages: 27
Registered: August 2010
Junior Member
Thanks for the info. Solved one of my still open puzzles. =)

Thanx
Re: E4 annotations in Dialogs [message #1232118 is a reply to message #1232094] Thu, 16 January 2014 09:13 Go to previous messageGo to next message
Mihael Schmidt is currently offline Mihael Schmidt
Messages: 27
Registered: August 2010
Junior Member
But services and components can be injected on the execute method of the handler, right?! So I will switch to this style of injection.

Thanx again.
Re: E4 annotations in Dialogs [message #1232125 is a reply to message #1232118] Thu, 16 January 2014 09:27 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk Fauth
Messages: 1284
Registered: July 2012
Senior Member
Quote:
But services and components can be injected on the execute method of the handler, right?


Yes, that's what Tom tried to say. You should always do the injection on the execute method of the handler. This is the only way to ensure that you'll get the correct instances. Field injection in handlers can cause serious issues that you might not find easily. Just think of replacing a service implementation at runtime. Your handlers would never get informed.
Re: E4 annotations in Dialogs [message #1232216 is a reply to message #1232125] Thu, 16 January 2014 12:58 Go to previous message
Joseph Gagnon is currently offline Joseph Gagnon
Messages: 58
Registered: June 2013
Member
OK, good to know. I'll try to remember to not use field injection in handlers. Thanks.
Previous Topic:Validate on saving files
Next Topic:Exception thrown if Part is closed PartStack within Area.
Goto Forum:
  


Current Time: Tue Sep 16 19:45:20 GMT 2014

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

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