I am writing a scout application, and I stumbled upon some problem. In my standard Outline I have more than one page. In page A I have some editable table with save button. What is in page B is not important for this discussion.
Outline
page A
page B
If page A is selected and I edit some data I would like to be notified if I click on page B that some data are not saved.
So before Outline switch between page A and B I would like to have control to not switch to page B because same data in A are not saved.
I have actually solve this problem with extending pages, but I an looking if there is some standard pre-defined way for this.
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);
}
}
};
}