Try OpenEdge Now
skip to main content
Application Developer's Guide
Using the Rule Wizard framework : How to write additional macros and editors : Sample macro implementation
 

Sample macro implementation

The following is a sample implementation of a simple macro, the boolean type. The source code containing the verbose comments is provided. The next section provides associated editor implementation for this type, using a checkbox GUI component.
package com.savvion.rulewizard.data;
import java.util.Hashtable;
/**
 * Macro class implementing the data strucure used by the "boolean" type.
 */
public class BooleanMacro extends DefaultMacroData {
    protected boolean boolValue;
    public static final String STRING_VALUE = "stringValue";
    
    /**
     * Get the boolean value of this macro.
     * This property will be used from Velocity programs and CheckBoxEditor.
     * @return value of boolValue.
     */
    public boolean getBoolValue() {
        return boolValue;
    }
    
    /**
     * Set the boolean value of this macro.
     * This property will be used from Velocity programs and
     * CheckBoxEditor. @param v Value to assign to boolValue.
    */
    public void setBoolValue(boolean v) {
        this.boolValue = v;
    }
    
    /**
     * @return a string representation of this macro's value ("true"
     * or "false" string)
     */
    public String getStringValue() {
        return (new Boolean(boolValue)).toString();
    }
    
    /**
     * Initializes this macro with the value represented by the string in
     * the parameter.
     * @param v a string representation of this macro's value ("true"
     * or "false" string)
     */
    public void setStringValue(String v) {
        this.boolValue = Boolean.valueOf(v).booleanValue();
    }
    
    /**
     * Although no validation is required we implement this method because
     * the base class (DefaultMacroData) does not.
     * @return always the boolean "true" because the data in this macro is
     * always valid
    */
    public boolean verify() {
        return true;
    }
    
    /**
     * Called by the persistence manager which saves the parameter values
     * once a rule template has been customized.
     * We have to save the value of this macro as a string so we use the
     * getStringValue() method. Based on the value returned by this method
    * the persistence manager will create a tag like:
     * <property name="stringValue">true</property>
     * inside the "macro" tag describing this macro in the rule template
     * parameters (.rtp) file.
     * @return a hashtable containing the input properties to be saved for
     * later use (in this case just one).
     */
    public Hashtable getPropertiesToSave() {
        Hashtable pts = new Hashtable();
        pts.put(STRING_VALUE, getStringValue());
        return pts;
    }
    
    /**
     * The counterpart of the getPropertiesToSave() method.
      * For each input property saved by the persistence manager a call to
     * this * method will be issued when reading a recustomizing a rule
     * template. The macro is responsible for recovering its state using
     * these properties. In other words the BooleanMacro has to obtain a
     * boolean value from the string in the "value" parameter.
     * @param name - name of the input property
     * @param value - value of the input property
     */
    public void addProperty(String name, String value) {
        if (name != null && name.equals(STRING_VALUE))
            setStringValue(value);
    }
    
    /**
     * We compose a long label from the macro's label concatenated with the
     * string representation of the boolean value. This long label will be
     * used in explanations about what the generated rules will do, etc.
     * @return the long label, plain English explanation of the macro's
     * value
    */
    public String getLongLabel() {
        if (label == null)
            return ""; // this should never happen
        return label + ": " + getStringValue();
    }
}