Play Example

The Play domain is extensively described in Chapters 3 through 12 in Program Generators with XML and Java. In Chapter 12, example 12-26 is the basis for this example.

PlayPG.template

import java.awt.*;
import java.awt.event.*;

#  String name = context.evalString("//play/@name"); #
class #((name))#Play2 extends Frame {
    /* The Props for #((name))# ************/
#for  "//play/prop"#
    Button #"@name"#Prop 
              = new Button("#"trait"#");
#end#

    /* The Events in the #((name))# ********/

    class PropEvent implements ActionListener {
        public void actionPerformed(ActionEvent evt) {
            Object prop = evt.getSource();
#for  "//play/prop"#
           #.if "position()!=1"# } else #fi#
#.# if (prop.equals(#"@name"#Prop)) {
    #for  "script/trait"#
                #((getPropName(context)))#Prop.setLabel("#"."#");
    #end#
  #if "./script/@goto"#
                enterNewScene("#"script/@goto"#");
  #fi#
#end#
            } else {
                System.out.println("Invalid prop");
            }
        }
    }

    /* Creating and starting up the #((name))# ****/

    String currentScene;

    public #((name))#Play2() {
#  String title = context.evalString("//play/title");
  if (title.equals("")) title = "No Title";
#
        super("#((title))#");
        setSize(#"//play/@width"#, #"//play/@height"#);
        setLayout(new FlowLayout());

        // initialize props
        PropEvent a = new PropEvent();
#for  "//play/prop"#
        #"@name"#Prop.addActionListener(a);
#end#
        // start scene
        enterNewScene("#"//play/@start"#");
    }

    public void enterNewScene(String scene) {
        removeAll();  // remove previous scene
        currentScene = scene;
#for  "//play/scene"#
  #if "position()!=1"#
	  } else
  #fi# if (scene.equals("#"@name"#")) {
  #for a "addprop"#
                add(#"@name"#Prop);
    #for  "trait"#
                #((getPropName(context)))#Prop.setLabel("#"."#")
    #end#
  #end#
	          setBackground(Color.decode("#"@color"#"));
#end#
        } else {
            System.out.println("Invalid scene: "+scene);
        }
        show();
    }

    public static void main(String[] args) {
        new #((name))#Play2();
    }
}

#declarations 
/** gets appropriate prop name for a trait */
String getPropName(XPathContext c) throws Exception {
    String n = c.evalString("@prop");
    if (!n.equals("")) {
        return n;
    }
    return c.evalString("ancestor::addprop/@name | ancestor::prop/@name");
}
#