Using ESelectionService.setSelection(null) for DI? [message #896733] |
Thu, 19 July 2012 14:12 |
|
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?
|
|
|
Re: Using ESelectionService.setSelection(null) for DI? [message #896738 is a reply to message #896733] |
Thu, 19 July 2012 14:25 |
Eclipse User |
|
|
|
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 |
|
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
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04168 seconds