• No results found

Examples of <behavior>, < rule>, <condition>, and <action> Elements

6   Interface Description

6.8 The &lt;behavior&gt; Element

6.8.1 Examples of &lt;behavior&gt;, &lt; rule&gt;, &lt;condition&gt;, and &lt;action&gt; Elements

UIML allows the following actions in a behavior to be specified:

Assign a value to a part’s property. The value can be any of the following: a constant value, a reference to a constant, the value of property, or the return value of a call.

<behavior>

<rule>

<condition>

<!--A-->

<event class="ButtonSelected" part-name="b1">

</condition>

<action>

<!--1-->

<property part-name="b1" name="color"/>blue</property>

<!--2-->

<property part-name="b2" name="color"/>

<reference constant-name="green"/>

</property>

<!--3-->

<property part-name="b2" name="color"/>

<property part-name="b1" name="color"/>

</property>

<!--4-->

<property part-name="b3" name="color">

<call component-id="serverObject" method-id="getColor"/>

</property>

</action>

</rule>

</behavior>

The <behavior> element above consists of one rule. The rule is executed whenever an event of class

"ButtonSelected" for part "b1" fires. Let’s assume that "b1," "b2," and "b3" are buttons (established in

<part> elements not shown), and "ButtonSelected" is a button click (established in a <peers> element not shown).

There is one <action> element associated with the rule. That action contains four elements that set properties in the interface, labeled 1 to 4 by comments.

The first action (labeled "<!--1-->") sets property "color" of the button to blue when button b1 is clicked.

The second action sets the color of button b2 to a constant named "green" (and defined in the

<content> element [see Section 6.7], not shown here).

The third action sets the color of button b2 to whatever color b1 currently has. Note that this action is executed after 1 and 2 above were executed, so button b2’s color is set to blue (because button b1’s color was set to blue in action 1 above).

The fourth action sets the color of button b3 to the return value of a call to a component called

"serverObject" and a method on that component called "getColor". The <d-component>

element in the <logic> element (discussed in Section 7.3) defines what "serverObject" and

"getColor" is mapped to -- for example a method called "getColor" that takes no parameters in an object instance named "serverObject." Note that no parameters are passed by the <call>

element, so method getColor either must take no arguments or must have default values for all its formal arguments. Note that the return value of the call is converted to a character string,

because the value of the <property part-name="b3" name="color"> element is a character string. Finally note that white space (spaces, tabs, line breaks) is significant in an XML document. Therefore the right angle bracket of the <property> tag must be immediately followed by the left angle bracket of the <call> tag, and similarly the right angle bracket of

<call> must be immediately followed by the left angle bracket of the </property> tag.

Call a method. The function or method call can take any number of arguments. Each argument to the call can be any of the following: a constant value, a reference to a constant, the value of property, or the return value of another call.

<behavior>

<rule>

<condition>

<!--B-->

<event class="ButtonSelected" part-name="b1">

</condition>

<action>

<!--5-->

<call component-id="m" methodid="storeData">

<param>5</param>

<param><reference constant-name="green"/></param>

</call>

<!--6-->

<call component-id="m" method-id="storeColor">

<param name="a3"><

property part-name="b1" name="color"/>

</param>

</call>

<!--7-->

<call component-id="n" method-id="DisplayData">

<param>

<call component-id="serverObject" method-id="getColor"/>

</param>

<param>

<call component=id="q" method-id="getParam">

<param>5</param>

</call>

</param>

</call>

</action>

</rule>

</behavior>

The <behavior> element above consists of one rule. The rule is executed whenever an event of class

"ButtonSelected" for part "b1" fires. Let’s assume that "b1" is a button (established in a <part> element not shown), and "ButtonSelected" is a button click (established in a <peers> element not shown).

There is one <action> element associated with the rule. That action contains three elements that set properties in the interface, labeled 1 to 3 by comments.

The first action (labeled "<!--1-->") calls a method named "storeData" on component "m" when button b1 is clicked. The method takes two arguments; the first is 5 and the second is the value of a constant named "green" (and defined in the <content> element [see Section 6.7], not shown here).

The second action is to call method "storeColor" on component "m" with one parameter, whose value is the current color of button b1. The attribute name="a3" in the <param> element is used in the following situation: "storeColor" has more than one formal parameter, and one formal parameter is named "a3", and all other formal parameters have default values.

The third action calls method "DisplayData" on component "n" with two parameters. The first is the return value of a call to method "getColor" in object "serverObject." The second is the return value of a call to method "getParam(5)" in object "g."

Fire an event. An event can be fired from the <action> element. An <action> element may contain at most one <event> element, and this <event> element must appear as the last child of <action>.

<behavior>

<rule>

<condition>

<!--C-->

<event class="ButtonSelected" part-name="b1">

</condition>

<action>

<!--8-->

<!--executed when b1 is clicked -->

<event class="ButtonSelected" part-name="b2"/>

</action>

</rule>

<rule>

<condition>

<!--D-->

<event class="ButtonSelected" part-name="b2">

</condition>

<action>

<!--9-->

<!--executed when b1 or b2 is clicked -->

<call component-id="f" method-id="1">

<param>

Assume that both "b1" and "b2" are rendered as buttons and "ButtonSelected" is mapped to the event that is fired when a button is pressed. Whenever the end-user clicks button "b1" then the first rule will evaluate to true (event labeled "C") and fire another event that will simulate the end-user pressing "b2"

(action labeled "8"). Then the rendering engine will evaluate the condition for all the rules again (due to the algorithm in A), and the second rule will evaluate to true (event labeled "D") and call method "1" on component "f" with the parameter "10" (action labeled "9").

This feature must be used with care, to avoid creating an infinite loop (e.g., if the second <action>

element was "<event class="ButtonSelected" part-name="b1"/>" to simulate a click on button b1, instead of the <call> element).