Unfortunately, there is no way to prevent the node selection from really happening. As you mentioned, you can listen for activation and deactivation events in your Page by overriding the methods execPageActivated
and execPageDeactivated
, respectively. But by using this approach, you cannot take control over node switching.
A bit more of control you get by providing your own implementation of createPageChangeStrategy
in your Outline class by injecting a custom DefaultPageChangeStrategy
. So you get informed every time a node change happens with a respective pageChange event. As long as your page is invalid, you prevent the page switching from happening and restore the origin tree selection.
Please take a look at the following example:
@Override
IPageChangeStrategy createPageChangeStrategy() {
return new DefaultPageChangeStrategy() {
@Override
public void pageChanged(IOutline outline, IPage deselectedPage, IPage selectedPage) {
if (deselectedPage instanceof APage && !((APage) deselectedPage).isValid()) { // #isValid is your check method for validity.
// Do not propagate the PageChangeEvent and restore the selection of the invalid page.
// Uninstall the PageChangeStrategy to ignore the event of restoring the selection.
final IPageChangeStrategy pageChangeStrategy = this;
setPageChangeStrategy(null);
// Restore the selection and install the PageChangeStrategy anew.
new ClientSyncJob("Restore node selection", ClientSession.get()) {
@Override
protected void runVoid(IProgressMonitor monitor) throws Throwable {
YourOutline.this.selectNode(deselectedPage);
setPageChangeStrategy(pageChangeStrategy);
}
}.schedule();
}
else {
super.pageChanged(outline, deselectedPage, selectedPage);
}
}
};
}