Chapter 4 Using Activities in ADL

4.6 Creating Customized NROs

Note: Many AthenaMuse 2 users will not need to create NROs. This section can be skipped without any loss of continuity in the presentation.

Some advanced users of AM2 may find it useful to create their own, customized NRO classes. For example, in the user-defined ColorPalette class shown in Figure 4.9, we use the standard NRO that provides the usual three arguments (client data, the list of the names of the values returned and the corresponding list of values). Since all the target message method really needs is the name of the color selected, most of this information goes unused. You might develop a customized NRO that only provides a string containing the name of the selected color as its argument.

To create specialized NROs, it is necessary to understand how the standard Nro wrapped class works. This class has a special constructor named Create that stores its four arguments in members of the Nro class. These four members are declared as follows:

string mActivity; //  the activity name[13]
string mMethod;   //  the name of the method to receive a 
						 //  message when the activity is triggered
handle mClient;   //  handle to the client object to receive
						 //  a message when the activity is triggered
any mClientData;  //  any client data

A new class that inherits from the Nro class automatically has these four members.

A second key aspect of the Nro class is the method HandleActivity. This is the method that receives a message whenever an event triggers an activity. It always receives the list of value names and the corresponding list of values sent by the activity as its arguments. In the standard NRO, this method is as follows:

	on HandleActivity: list keys, list values
	{
		{mMethod, mClientData, keys, values} => mClient;
	}
The key to writing a new type of NRO is to define a new class that inherits from the standard NRO but provides a new version of the HandleActivity method, which sends the arguments of interest to the target method. Figure 4.13 shows a new NRO class called ColorNro.

Note that the ColorNro class has a special constructor named Create that appears to do nothing even though its base class, Nro, has the same special constructor. This is needed because special constructors are never inherited. The useful work done by this special constructor is accomplished by initializing the base class using ADL's base class initialization feature (the code block following the init keyword). In this case, the special constructor sends a base class initialization message to the Nro class. The HandleActivity method in the ColorNro class overrides the method with the same name in the Nro base class. In line 9, the client of the activity (mClient) receives a message invoking the target method (mMethod) with the only argument being the first element on the list of values. This element contains the name of the color.
Example of a Custom NRO
class ColorNro: Nro

{

upon Create: string act, handle cli, string mtd, string cd

init {{'Create, act, cli, mtd, cd} => Nro}

{ }

on HandleActivity: list keys, list values

{

{mMethod, at(1,values)} => mClient;

}

};

Figure 4.14 shows how the example program in Figure 4.10 can be modified to make use of the customized NRO.
Revised Example Using ColorPalette and Custom NRO
anonymous: XFtop

{

ColorPalette myPalette{height=45;};

ColorNro {'Create, 'ColorSelected, self, 'EchoColor, ""} =>

selectNro;

XFlabel colorLabel {x=10; y=50; height=50; width=200;

recomputeSize=FALSE; label="";};

XFbutton exitButton {y=110; x=10; height=50; width=100;

label="Exit";};

upon Construct

{

exitButton.Pressed = {'Exit, theApp};

{'Subscribe, &selectNro} => myPalette;

}

on EchoColor: string colorName

{

colorLabel.label = "Chosen color is" & colorName;

}

} myApplicaton {height=200; width=350;};


[13] The member mActivity is a "read only" attribute. It should never be changed after it is initialized.
AM2 Documentation - 19 NOV 1996

Generated with Harlequin WebMaker