Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Injecting IServiceConstants.ACTIVE_SELECTION to implement a selection listener
icon5.gif  Injecting IServiceConstants.ACTIVE_SELECTION to implement a selection listener [message #633992] Wed, 20 October 2010 08:28 Go to next message
No real name is currently offline No real nameFriend
Messages: 13
Registered: June 2010
Junior Member
Hello!

I was reading Tom Schindl´s tutorial (http://github.com/tomsontom/e4demo/raw/master/tutorial.pdf) and saw his class View (page 21):

public class View {

   @Inject
   void updateFolder(@Named(IServiceConstants.ACTIVE_SELECTION) IFolder folder) {
      // Do something when selection changes
   }
}


(Which btw is supposed to do the same as the following Eclipse 3.x code:

public class View extends ViewPart {
   public void createPartControl(Composite parent) {
      getSite().getSelectionProvider().addSelectionChangedListener(new ISelectionChangedListener() {
         public void selectionChanged(SelectionChangedEvent event) {
            if( event.getSelection() instanceof IStructuredSelection) {
               Object o = ((IstructuredSelection)event.getSelection()).getFirstElement();
               if( o instanceof IFolder ) {
                  updateFolder((IFolder) o);
               }
            }
         }
      });
   }
   void updateFolder(IFolder folder) {
      // Do something when selection changes
   }
}
)

Beautiful, I thought - a way of adding a selection listener without having access to the control that you want to add it to. (of course with high performance overhead since it is called everytime an object of the specified type is selected.)

I decided to write a toy example where this one method should be executed everytime I pushed a button. So I wrote the class:

public class ButtonListener {

   @Inject
   public void buttonSelected(@Named(IServiceConstants.ACTIVE_SELECTION) Button button) {
      System.out.println("Somebody pushed a button!");
   }
}


Now came the question: Where do I add ButtonListener to my Application.e4xmi so that buttonSelected(...) will be called? Allthough it does not hava an @Execute annotation, I decided to add it as a handler of a part which has a button. Mostly because I don´t know why a part has handlers...

When I ran the applictaion it complained about it not being able to find a button that it could inject. So I added the @Optional annotation to the Button parameter (and a button != null test before the print statement). Then it at least entered the method once but only at startup and not when I later, when the window was displayed, pressed any buttons. My questions are:

1) Why does it not enter the buttonSelected method when I press a button? The reason why I think it should is that since the buttonSelected is a method as opposed to a constructor, the value of the key IServiceConstants.ACTIVE_SELECTION should be reinjected every time it changes.
2) How can I with minimal change make my toy program work? Do I have to modify the value of the ACTIVE_SELECTION constant in the context myself whenever the button in pushed?
3) What are a Part´s handlers (in the Application.e4xmi file) used for?
4) Is there a better place to reference ButtonListener in the Application.e4xmi file?

Thanks!

Karin
Re: Injecting IServiceConstants.ACTIVE_SELECTION to implement a selection listener [message #634070 is a reply to message #633992] Wed, 20 October 2010 12:14 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

karins.spam@gmail.com wrote:

> public class View {
>
> @Inject
> void updateFolder(@Named(IServiceConstants.ACTIVE_SELECTION) IFolder
> folder) {
> // Do something when selection changes
> }
> }
>


We support both injection (@Inject, @PostConstruct, @PreDestroy,
@Optional, @Named) usually tied to instantiating contributions and we
support invocation (@Execute, @CanExecute) which we use when invoking a
method against the IEclipseContext. The difference between injection
and invocation is invocation is a one-shot deal and injection creates a
tracker that will re-inject any changed values.


@Inject
void updateFolder(@Optional @Named(IServiceConstants.ACTIVE_SELECTION)
IFolder folder)

So in your example, any time IServiceConstants.ACTIVE_SELECTION changes
in the visible set of IEclipseContexts, this method gets called.
Without the @Optional, you can't accept null and injection will fail (as
the selection is a product of the runtime, and can be nothing).

If you *were* going to use ACTIVE_SELECTION, you would be using the
ESelectionService, since the selection itself is more complicated than
just setting a value in your view. The selection is posted to the
MWindow level, so what good would it do to post a Button.

But you can do this with your own constant and your view
IEclipseContext, you don't need to use ACTIVE_SELECTION, if you wanted
to. However, for a button and button listener, I probably wouldn't use
the context (which is for passing data, not really for events).

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


Re: Injecting IServiceConstants.ACTIVE_SELECTION to implement a selection listener [message #1060958 is a reply to message #634070] Wed, 29 May 2013 10:08 Go to previous messageGo to next message
Ozgur Cagdas is currently offline Ozgur CagdasFriend
Messages: 29
Registered: May 2013
Junior Member
The post's been really educative for me in the sense of underlying the differences between plain injection and invocation injection.

However, I'm not quite clear about the 'injection creates a tracker that will re-inject any changed values.' statement. Is this also valid for the @PostConstruct annotation? I'd thought @PostConstruct was called only once but is it also tracked by this tracker mechanism and called again if required (i.e. one of the parameters is updated)?

Thanks in advance,

O.

[Updated on: Thu, 30 May 2013 11:29]

Report message to a moderator

Re: Injecting IServiceConstants.ACTIVE_SELECTION to implement a selection listener [message #1061707 is a reply to message #1060958] Mon, 03 June 2013 14:43 Go to previous message
Sopot Cela is currently offline Sopot CelaFriend
Messages: 597
Registered: December 2010
Senior Member

The @PostConstruct is not supposed to be recalled. Are you annotating the method *also* with @Inject (i.e. both @Inject and @PostConstruct)?
Previous Topic:Menu defined in the model look and feel
Next Topic:handlerService.executeHandler loops indefinitely
Goto Forum:
  


Current Time: Thu Dec 18 23:21:13 GMT 2014

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

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