Portable notebook events
In this section we'll explore using the events for the PM Notebook and Windows Notebook. The following example uses the CustomerInformationPM notebook in the InvestmentApp application. However, if you wish to substitute the CustomerInformationWin notebook you can do so.
Writing the scripts
Begin by opening the Composition Editor on the CustomerInformationPM part. When the Composition Editor opens, switch to the Script Editor.
When the Account Summary page of the notebook becomes visible you might want to retrieve the customer's cash balance from a database, compute the total portfolio's current value, and compute the portfolio's performance over the year. The aboutToBeSwitchedTo event provides a convenient trigger to perform this type of processing. We won't actually do anything too complex here, but we will initialize the Cash balance text part to illustrate the point. Create the following script to initialize the cash balance whenever the Account Summary page becomes visible.
aboutToBeSwitchedTo
"Handle about to be switched to event for the Account page.
Perform any initialization for the page here."
Transcript cr; show: 'About to switch to the Accounts page'.
(self subpartNamed: 'Text1') object: '$1,000,000.00'
Let's suppose that the customer wants to withdraw a portion of the cash balance. You might want to issue a check automatically whenever the cash balance field changes. Alternatively, you might have a requirement that the customer maintain a minimum cash balance in their account. The aboutToBeSwitchedFrom event provides a convenient trigger to perform this type of processing. Furthermore, you can prevent the user from turning the page when, say, the cash balance drops below some minimum. Again, we won't do anything so complex here, but we will prevent the user from turning the page if the Cash balance text part does not contain at least four characters. Create the following script to perform this processing:
aboutToBeSwitchedFrom: aCwConfirmationCallback
"Handle about to be switched from event for the Account page.
If the page is invalid, prompt the user and prevent the
switch from the current (Account) page."
| cash |
cash := (self subpartNamed: 'Text1') object.
(cash size < 4)
ifTrue: [aCwConfirmationCallback doit: false. "Prevent the switch"
CwMessagePrompter new
title: 'Error - aboutToBeSwitchedFrom:';
buttonType: XmOK;
iconType: XmICONERROR;
messageString:'Enter a monetary value in the format 0.00';
prompt].
Transcript cr; show: 'About to leave the Accounts page'
 
Tip icon
You cannot prevent a notebook page switch with the OS/2 - Windows Notebook part. If you need to do post-processing that prevents a page change, use a PM Notebook or Windows Notebook.
So far we have covered events that occur for notebook pages. They provide a convenient mechanism for processing data that is unique to a particular page. Sometimes you will have data that is presented on every page of a notebook. Create the following script which writes a message to the System Transcript window whenever the current page changes.
currentPageChanged: aPageView
"Handle current page changed event.
This is a convenient point to perform any initialization common
to all pages in the notebook."
Transcript cr; show: aPageView name.
Similarly, there might be common post-processing that needs to be done whenever the user fills in any page of a notebook. The aboutToChangePages occurs whenever the user is changing to a new notebook page, but before the current notebook page is removed from the window. You can also prevent the user from turning the page should the need arise. Create the following script which prevents a page turn when the Cash balance text part on the Account Summary page is empty:
aboutToChangePages: aCwPageChangeCallbackData
"Handle about to change pages event for the notebook.
This is a convenient point to perform any validation
that is common to all notebook pages. Prevent the page
from being changed if it is invalid."
| cash prompter |
cash := (self subpartNamed: 'Text1') object.
(cash = '')ifTrue:
[ aCwPageChangeCallbackData doit: false. "Prevent page change"
CwMessagePrompter new
title: 'Error - aboutToChangePages:';
buttonType: XmOK;
iconType: XmICONERROR;
messageString: 'Enter a monetary value for "Cash balance"';
prompt].
Making the connections
Now that the scripts are complete, let's make the event-to-script connections. Switch to the Composition Editor and make the connections as follows:
Connect the notebook's currentPage event to the currentPageChanged: script.
Connect the notebook's aboutToChangePages event to the aboutToChangePages: script.
Bring the Account Summary notebook page to the top and connect its aboutToBeSwitchedFrom event to the aboutToBeSwitchedFrom: script.
Connect the Account Summary page's aboutToBeSwitchedTo event to the aboutToBeSwitchedTo script.
When you finish, the connections should look like the following:
Investment connections
Testing your work
Save the part and select Test iconto test it.
You should see $1,000,000.00 in the Cash balance field. Now, take a look at your System Transcript window. You should see the following messages there:
Notebook Page1
About to switch to the Accounts page
Notice that the Account Summary page's aboutToBeSwitchedTo event occurred when the notebook was first opened.
Now, try selecting each page in sequence and when you get to the last page, select the Account tab. You should see the following sequence of messages in your System Transcript window.
About to leave the Accounts page
Notebook Page2
Notebook Page3
Notebook Page4
Notebook Page5
Notebook Page1
About to switch to the Accounts page
Notice that the notebook page events occur only for the Accounts Summary page because it is the only page that has the appropriate event-to-script connections. To process the aboutToBeSwitchedTo and aboutToBeSwitchedFrom page events, you must make the appropropriate event-to-script connections for each individual page you wish to process.
Now, delete all the text in the Cash balance text part and try to change to another page. You should see a message prompter with Error - aboutToChangePages: in the title bar. Select OK and the notebook will still be on the Account Summary page.
Now, enter .00 in the Cash balance text part and try to change to another page. You should see another message prompter with Error - aboutToBeSwitchedFrom: in the title bar. Select OK and the notebook will still be on the Account Summary page.
Now, enter 0.00 in the Cash balance text part and try to change to another page. You should now be able to do so.
Select the Account tab. The Account Summary page will appear and the Cash balance text part will contain $1,000,000.00 because the aboutToBeSwitchedTo script always initializes this field whenever the page appears.
Last modified date: 07/01/2020