Try OpenEdge Now
skip to main content
Application Developer's Guide
Using the Rule Wizard framework : Collecting macros
 

Collecting macros

Another frequent requirement encountered when building rule templates is the collection of macros. To support this, the <macro-type> tag defines a new type of macro with pre-existing components. An example follows:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE template SYSTEM "resources/ruleTemplate.dtd" >
<template name="ex5">
    <macro-type name="my-collection"
    class="com.savvion.rulewizard.data.AggregateData">
        <macro-component name="list" label="List"
        editor="editor_list"/>
        <macro-component name="text"
        label="Text" editor="editor_text"/>
    </macro-type>
    
    <editor name="editor_list" macro="list" label="List"/>
    <editor name="editor_text" macro="text" label="Text"/>
    
    <screen number="10">
        <editor class="com.savvion.rulewizard.gui.AggregateEditor"
        label="Heterogenous collection" macro="heterogenous"
            show-label="false">
        <constraints fill="BOTH" insets="5,5,5,5" weightx="2" gridx="0"
            gridy="0" anchor="CENTER" weighty="1"/>
        </editor>
    </screen>
    <macro name="list" type="combo-box">
        <vector-property name="listLabels">
            <vector-elem>Option A</vector-elem>
            <vector-elem>Option B</vector-elem>
            <vector-elem>Option C</vector-elem>
        </vector-property>
        <vector-property name="listValues">
            <vector-elem>option_a</vector-elem>
            <vector-elem>option_b</vector-elem>
            <vector-elem>option_c</vector-elem>
        </vector-property>
        <property name="listSelectedValue">option_b</property>
    </macro>
    
    <macro name="text" type="varname"/>
    
    <macro name="heterogenous" type="my-collection"/>
    
<text>
#foreach($single_macro in ${heterogenous.getComponents()})
#if ($single_macro.type == "varname")
Value of text is $single_macro.
#else
Value of list is $single_macro.selectedValue.
#end
#end
</text>
</template>
The <macro-type> tag defines a heterogeneous collection of macros. Each component is described in a <macro-component> tag containing references to macro names (text and list) and editor names (editor_text and editor_list). The macros and editors are defined as ordinary, except that editors have a supplementary name attribute.
This rule template unit generates a drop-down list, as shown in Figure 27.
Figure 27. Rule wizard example 5
With this wizard, the user can add list and text macros to the collection. Dialog boxes appear allowing the user to enter values for these macros. The standard behavior allows the user to add values no matter how many components exist in the collection. Write a Java class derived from com.savvion.rulewizard.data.AggregateData when another policy is required. All the references passed to a collection macro are passed to its components.
After adding a few components of different types to the collection, click Finish to view the results. Notice the foreach Velocity construct used to display the results. Iterate through the collection obtained by calling the collection macro’s getComponents method. Because collection components contain different types, find a component’s type by accessing the type property. Based on this type, you decide what properties to use.