Problem
Normally page initialisations are done by the enter() method (and often a RPC, called from there). This method will be called after the page window and its widgets are created (but still invisible). Unfortunately the invoke mechanism has not finished completely at this point so that a simple closeCancel() produces an unexpected behaviour (NullPointerException). Also for more tricky solutions like using Display.asyncExec() a safe, consistent system status can not be guaranteed.
Solution
A better way is to ensure the correct page initialisation before creating the page window with its widgets, e.g. starting the page invocation. Everything you do with pages and models on client and server side can be done immediate after constructing the page by new, only the GUI related stuff like widgets, dialog or the shell is not existing at this point.
So you can implement an initialisation method, for example init(), that can also call a RPC method for server side inits. Call this method before invoke(). The following demo code, based on the auto generated sample from the XMA project creation wizard, should show this in detail:
Sample
Let's assume, that a main page TestDlg in a component Example has an RPC called init(), that is called from the enter() client method. The relevant code fragments will look like this:
What we can get now is a NullPointerException because the inconsistent state caused by the exception thrown in enter().
Now we can simply change enter() to something called before invoke(), for example to init() (servers init() stays the same):
What happens now if an exception is thrown by init()? The invoke() will never be reached, so the page window will not be displayed. The (runtime) exception will be caught outside Example.invoke() by the XMA runtime and displayed in a generic message box.
Discussion
For some reasons it could also be an option to catch the exception by yourself to do something else (return a special component parameter value, show different or more styled user information...).
As described above, all the pages objects and their models on client and server are created with the constructors invoked by ''new''. But only if the property "YN Model Lazy Generated" for the page is set to NO in the GUI designer (this is the default). In case of "YN Model Lazy Generated" is YES you cannot use the models before invoke().
The widgets, related to the models are created by invoke() and synchronized automatically with them. So every kind of model initialisation can be done in the way suggested here. UI related stuff like gray-logic or error setting has to be done in enter() or stateChanged().
