Strange behaviour with SelectionService [message #989448] |
Thu, 06 December 2012 09:33 |
Gaetan Pitteloud Messages: 17 Registered: September 2012 |
Junior Member |
|
|
My use case: A part displays a tree, and when I double-click on a node, an editor opens for that node (just like eclipse-ide).
I have added a listener on the treeViewer, and when invoked, the current node is set in ESelectionService by invoking selectionService.setSelection(item). I have created a command and a handler that is invoked on the command. The handler receives the current selection in its @Execute method, thanks to @Named(IServiceConstants.ACTIVE_SELECTION) on the method parameter.
The handler is invoked from the listener thanks to ECommandService and EHandlerService. Here are the successive calls in my listener:
logger.debug("Double click on {}", selectedItem);
selectionService.setSelection(selectedItem);
logger.debug("Item {} set as global selected item", selectedItem);
ParameterizedCommand command = commandService.createCommand("my.command.id", null);
handlerService.executeHandler(command);
logger.debug("Handler invoked");
And here's the code of my handler:
@Inject private EPartService partService;
@Inject private EModelService modelService;
@Inject private MApplication app;
@Execute
public void openNode(@Named(IServiceConstants.ACTIVE_SELECTION) AbstractEntityDefinition<?> selectedItem) {
logger.debug("Handler invoked with selected item {}", selectedItem);
String partId = createEditorPartIdFor(selectedItem);
MPart editorPart = partService.findPart(partId);
if (editorPart == null) {
logger.debug("No editor found with id={}", partId);
editorPart = createEditorFor(selectedItem);
MPartStack editorsStack = (MPartStack) modelService.find(Constants.EDITORS_STACK_NAME, app);
editorsStack.getChildren().add(editorPart);
logger.debug("Editor with id={} created and added to stack", partId);
} else {
logger.debug("Editor with id={} already exists", partId);
}
partService.activate(editorPart);
logger.debug("Editor with id={} activated", partId);
}
Most of the time, but not always, the editor does not open when I double-click on a node. I added some logs, and discovered that the selected item set in the SelectionService in the listener is not always the item I get as a parameter in my handler.
Actually, I sometimes receive the previous node I selected before. In fact, if I wait some time (i.e. a few seconds, sometimes even more) between successive double-clicks, the correct selection is received in my handler, but sometimes not. If I set a debug breakpoint in the handler, I always get the correct selection in my handler (making it hard to debug). It looks like ESelectionService.setSelection() asynchronously sets the item and the handler is invoked before the new selection could be injected (I even tried to sleep for a while between the call to setSelection() and the invocation of the handler, but without success).
Am I doing something wrong ? Should I use another API to pass the selected item to my handler ? Any help would be appreciated.
Here are some logs about the issue:
1. Working correctly (first double-click)
[10:00:29,010] DEBUG TreePart Double click on FP_1001.PA_10
[10:00:29,010] DEBUG TreePart Item FP_1001.PA_10 set as global selected item
[10:00:29,010] DEBUG OpenTreeNodeHandler Handler invoked with selected item FP_1001.PA_10
[10:00:29,010] DEBUG OpenTreeNodeHandler No editor found with id=editor.FP_1001.PA_10
[10:00:29,011] DEBUG OpenTreeNodeHandler Editor with id=editor.FP_1001.PA_10 created and added to stack
[10:00:29,027] DEBUG OpenTreeNodeHandler Editor with id=editor.FP_1001.PA_10 activated
[10:00:29,027] DEBUG TreePart Handler invoked
2. Not working correctly (double-click right after the previous successful one)
[10:00:30,493] DEBUG TreePart Double click on FP_1001.PA_16
[10:00:30,493] DEBUG TreePart Item FP_1001.PA_16 set as global selected item
[10:00:30,493] DEBUG OpenTreeNodeHandler Handler invoked with selected item FP_1001.PA_10
[10:00:30,493] DEBUG OpenTreeNodeHandler Editor with id=editor.FP_1001.PA_10 already exists
[10:00:30,493] DEBUG OpenTreeNodeHandler Editor with id=editor.FP_1001.PA_10 activated
[10:00:30,494] DEBUG TreePart Handler invoked
Line 3 in non-working log shows that the handler receives item from the previous selection.
[Update] I have tried the following 2 things:
1. In the handler, get the selection from the ESelectionService instead of getting it through injection --> same result
2. Before returning from the handler's execute method, set the selection to null --> I get null in the subsequent call to the handler.
[Updated on: Thu, 06 December 2012 09:43] Report message to a moderator
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04475 seconds