Created
June 28, 2011 07:03
-
-
Save contextfw/1050647 to your computer and use it in GitHub Desktop.
Example: Math question
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MathQuestion extends Component { | |
| @Element | |
| private SimpleLabel answer = new SimpleLabel(); | |
| enum Operation { | |
| ADDITION("+") { | |
| @Override | |
| public long calculate(long val1, long val2) { | |
| return val1 + val2; | |
| } | |
| }, | |
| SUBTRACTION("-") { | |
| @Override | |
| public long calculate(long val1, long val2) { | |
| return val1 - val2; | |
| } | |
| }; | |
| private final String name; | |
| private Operation(String name) { | |
| this.name = name; | |
| } | |
| public abstract long calculate(long val1, long val2); | |
| } | |
| private Operation operation; | |
| @Attribute | |
| private long val1; | |
| @Attribute | |
| private long val2; | |
| @Attribute | |
| public String operation() { | |
| return operation.name; | |
| } | |
| public MathQuestion() { | |
| tryAgain(); | |
| } | |
| @Remoted | |
| public void tryAgain() { | |
| Random rand = new Random(); | |
| val1 = rand.nextInt(201) - 100; | |
| val2 = rand.nextInt(201) - 100; | |
| operation = Operation.values()[rand.nextInt(2)]; | |
| answer.setValue(""); | |
| refresh(); | |
| } | |
| @Remoted | |
| public void guess(Long guess) { | |
| if (guess != null) { | |
| if (guess == operation.calculate(val1, val2)) { | |
| answer.setValue("Your answer was correct :-)"); | |
| } else { | |
| answer.setValue("Your answer was incorrect :-("); | |
| } | |
| answer.refresh(); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="ISO-8859-1"?> | |
| <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" | |
| xmlns:data="http://example.com/2006/some-data" | |
| xmlns:txt="http://contextfw.net/ns/txt"> | |
| <xsl:template match="MathQuestion"> | |
| <div id="{@id}"><xsl:call-template name="MathQuestion" /></div> | |
| </xsl:template> | |
| <xsl:template match="MathQuestion.update"> | |
| <replaceInner id="{@id}"><xsl:call-template name="MathQuestion" /></replaceInner> | |
| </xsl:template> | |
| <xsl:template name="MathQuestion"> | |
| <xsl:value-of select="@val1" /> | |
|   | |
| <xsl:value-of select="@operation" /> | |
|   | |
| <xsl:value-of select="@val2" /> | |
|   =   | |
| <input type="text" size="3" id="{@id}_guess" /> | |
| <input type="button" class="button" value="Answer" | |
| onclick="contextfw.call('{@id}', 'guess', $('#{@id}_guess').val());" /> | |
|  <xsl:apply-templates select="answer" /> | |
| <br/> | |
| <input type="button" class="button" value="Try again" | |
| onclick="contextfw.call('{@id}', 'tryAgain');" /> | |
| </xsl:template> | |
| </xsl:stylesheet> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment