Smalltalk and Object Orientation: an Introduction - Free
Smalltalk and Object Orientation: an Introduction - Free
Smalltalk and Object Orientation: an Introduction - Free
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
The aspect methods c<strong>an</strong> then be simplified to just return the contents of the associated inst<strong>an</strong>ce variable.<br />
28.3.5 The “actions” protocol<br />
Another set of methods defined by the User interface builder are the “actions” methods. These are in the<br />
“action” protocol. We have already seen examples of these in the VisualOrg<strong>an</strong>izer class. As you<br />
saw there, by default, these methods are defined to return the value of self. We therefore need to define<br />
them.<br />
We shall first look at the doExit method as this is the simplest method. This method will close the<br />
window. To do this we again use the inherited closeRequest method. We therefore merely need to<br />
send the message closeRequest to self.<br />
doExit<br />
"This method closes the associated window"<br />
self closeRequest.<br />
Next we will look at the doInput method. This method takes input from both the name input field<br />
<strong><strong>an</strong>d</strong> the address input field <strong><strong>an</strong>d</strong> stores them using the newAddress:for: method which we will<br />
define later. Notice that once again we use <strong>an</strong> intermediate updater method to actually access <strong>an</strong>y<br />
inst<strong>an</strong>ce variables (this is good <strong>Smalltalk</strong> style). The doInput method is presented below.<br />
doInput<br />
"The method is executed when the input button is pressed"<br />
| aName <strong>an</strong>Address |<br />
aName := name value.<br />
<strong>an</strong>Address := address value.<br />
self newAddress: <strong>an</strong>Address for: aName.<br />
Notice that we do not just access the name <strong><strong>an</strong>d</strong> address aspects (inst<strong>an</strong>ce variables) <strong><strong>an</strong>d</strong> save them<br />
into the temporary variables aName <strong><strong>an</strong>d</strong> <strong>an</strong>Address. Instead, we send the message value to each of<br />
the aspects first. This is because, if we just took the contents of name <strong><strong>an</strong>d</strong> address we would obtain a<br />
ValueHolder object. What we really w<strong>an</strong>t is the contents of the value holder. To get this we must<br />
send the message value to the value holder. Thus name value , will return the value held by the<br />
value holder in the inst<strong>an</strong>ce variable name.<br />
Remember when you type this method in <strong><strong>an</strong>d</strong> accept it, the system will notify you that the method<br />
newAddress:For: is undefined. We will define it later!<br />
Next we will examine the doQuery action method. This method accesses the name input field <strong><strong>an</strong>d</strong><br />
updates the address input/output field. Again we must use the message value to access the contents<br />
of the name value holder. We then use the addressFor: accessor method (which we will define<br />
below) to obtain the address assoc iated with the name. If the address retrieved is not nil then we put<br />
the address retrieved into the address input/output field. If it is nil we display <strong>an</strong> okay dialog with <strong>an</strong><br />
appropriate warning message.<br />
doQuery<br />
"The method is executed when the input button is pressed"<br />
| aName <strong>an</strong>Address |<br />
aName := name value.<br />
<strong>an</strong>Address := self addressFor: aName.<br />
<strong>an</strong>Address isNil<br />
ifTrue: [Dialog warn:'There is no address for ' , name value.]<br />
ifFalse: [address value: <strong>an</strong>Address asText.]<br />
We will ju st examine the last statement in the method for a moment. This time we used the message<br />
value: rather th<strong>an</strong> the message value with the value holder in address. This is because we were<br />
updating the contents of the value holder rather th<strong>an</strong> accessing it. Thus the address value holder is set to<br />
the value of the parameter sent with the message value:. This parameter was actually the string<br />
returned by the addressFor: method. However, to be displayed it must be converted into a Text<br />
object (which unde rst<strong><strong>an</strong>d</strong>s how to display strings). This is achieved by sending the string in<br />
<strong>an</strong>Address the message asText. This is necessary because the text editor field assumes that it will<br />
display a text (which has special properties such as font <strong><strong>an</strong>d</strong> style) rather th<strong>an</strong> j ust a plain string. When<br />
241