Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Using ESelectionService.setSelection(null) for DI?
Using ESelectionService.setSelection(null) for DI? [message #896733] Thu, 19 July 2012 14:12 Go to next message
Marcel Bruch is currently offline Marcel Bruch
Messages: 230
Registered: July 2009
Senior Member
Hi,

Is ESelectionService able to propagate null selection, i.e., inform selection listeners that a selection is gone?

I've the following snippet to illustrate my problem:


public class TodoDetailsPart {

	@Inject
	ESelectionService s;

	@Inject
	public void updateSelection(@Optional
			@Named(IServiceConstants.ACTIVE_SELECTION) Person p) {
		System.out.println(" value " + p);
	}

	@PostConstruct
	public void createPartControls(Composite parent) {
		parent.setLayout(new GridLayout(1, false));

		btnLoadData = new Button(parent, SWT.NONE);
		btnLoadData.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				System.out.println("sending null selection");
                                // set new selection to NULL -> no event propagated to #updateSelection().
				s.setSelection(null);
			}
		});
       ...
}}


I expected updateSelection() to be called with null every time I click on my btnLoadData. However, this does not happen. Is this behavior intended?


tw: @MarcelBruch
tw: @Recommenders
Re: Using ESelectionService.setSelection(null) for DI? [message #896738 is a reply to message #896733] Thu, 19 July 2012 14:25 Go to previous messageGo to next message
Sopot Cela is currently offline Sopot Cela
Messages: 597
Registered: December 2010
Senior Member

Internally the setSelection sets a value on an IEclipseContext and a property of IEC's is that if value trying to be set is not different from the actual value the setting does not occur and thus no re-injection -> no method call.

Maybe in your case selection is initially null and you try to set it null? Try setting it to a non null value first and then set it null.
Re: Using ESelectionService.setSelection(null) for DI? [message #896785 is a reply to message #896733] Thu, 19 July 2012 18:34 Go to previous messageGo to next message
Joseph Carroll is currently offline Joseph Carroll
Messages: 174
Registered: May 2012
Location: Milwaukee, WI
Senior Member

I would just set a global "null" variable, something like:
public class MySelectionConstants {
    
    /**
     * Use this object to remove the current selection.
     */ 
    public static final Object NULL_OBJECT = new Object();
}


Then when you receive selection updates, do something like:
    @Inject
    public void updateSelection(@Optional
            @Named(IServiceConstants.ACTIVE_SELECTION) Object sel) {
        
        if (sel == MySelectionConstants.NULL_OBJECT)
            ; // Handle the removal of the active selection

        // Type check!
        else if (sel instanceof Person) {
            // You business logic
            Person p = (Person) sel;
            System.out.println(" value " + p);
        }
    }


Using this approach will cause a great deal more injection events as the selection changes. If it really becomes a problem, in the same way the initial "NULL_OBJECT" was created just create a "NULL_PERSON" or what have you. Then you are always guaranteed to be type safe and will only update when the selected "Person" changes.
    ...
    public static final Person NULL_PERSON = new Person();
}


    @Inject
    public void updateSelection(@Optional
            @Named(IServiceConstants.ACTIVE_SELECTION) Person sel) {
        
        if (sel == MySelectionConstants.NULL_PERSON)
            ; // Handle the removal of the active selection

        // No type check
        else {
            ...


Take care,

JD
Re: Using ESelectionService.setSelection(null) for DI? [message #896858 is a reply to message #896785] Fri, 20 July 2012 07:35 Go to previous messageGo to next message
Christoph Keimel is currently offline Christoph Keimel
Messages: 367
Registered: December 2010
Location: Germany
Senior Member
I haven't tried this, but another option would be to always use arrays as input. An empty selection would therefore look like this:
selectionService.setSelection(new Person[] {});

This way you continue to be type safe and are open to allow multiple selected Persons in the future.
Re: Using ESelectionService.setSelection(null) for DI? [message #896927 is a reply to message #896858] Fri, 20 July 2012 13:10 Go to previous message
Joseph Carroll is currently offline Joseph Carroll
Messages: 174
Registered: May 2012
Location: Milwaukee, WI
Senior Member

Good point, or List<Person>, either way would work. I would just stay away from using any null's whether as the argument or element in the list. Opens the door to too many issues. Smile

JD
Previous Topic:Re: Save reference of input of a Part
Next Topic:(Announce) I created an Eclipse 4 Theme
Goto Forum:
  


Current Time: Thu Oct 23 00:50:21 GMT 2014

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

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