<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Calculator</title> <style type="text/css"> .calc-btn{ width:30px; text-align: center; padding: 4px; font-family: sans-serif; font-size:12pt; } </style> </head> <body> <h2>Calculator</h2> <table> <tr> <td colspan="4"><WAF:Textbox id="txtInput" text="" style="font-family: sans-serif;font-size:130%;padding:5px;border:1px solid darkgrey;width:150px;"/></td> </tr> <tr> <td><WAF:Button id="btn1" text="1" class="calc-btn waf-basic-button"/></td> <td><WAF:Button id="btn2" text="2" class="calc-btn waf-basic-button"/></td> <td><WAF:Button id="btn3" text="3" class="calc-btn waf-basic-button"/></td> <td><WAF:Button id="btnPlus" text="+" class="calc-btn waf-basic-button"/></td> </tr> <tr> <td><WAF:Button id="btn4" text="4" class="calc-btn waf-basic-button"/></td> <td><WAF:Button id="btn5" text="5" class="calc-btn waf-basic-button"/></td> <td><WAF:Button id="btn6" text="6" class="calc-btn waf-basic-button"/></td> <td><WAF:Button id="btnMinus" text="-" class="calc-btn waf-basic-button"/></td> </tr> <tr> <td><WAF:Button id="btn7" text="7" class="calc-btn waf-basic-button"/></td> <td><WAF:Button id="btn8" text="8" class="calc-btn waf-basic-button"/></td> <td><WAF:Button id="btn9" text="9" class="calc-btn waf-basic-button"/></td> <td><WAF:Button id="btnMultiply" text="*" class="calc-btn waf-basic-button"/></td> </tr> <tr> <td><WAF:Button id="btnClear" text="C" class="calc-btn waf-basic-button"/></td> <td><WAF:Button id="btn0" text="0" class="calc-btn waf-basic-button"/></td> <td><WAF:Button id="btnEqual" text="=" class="calc-btn waf-basic-button"/></td> <td><WAF:Button id="btnDivide" text="/" class="calc-btn waf-basic-button"/></td> </tr> </table> </body> </html>
package controllers; import com.openwaf.client.event.dom.ClickEvent; import com.openwaf.client.event.dom.ClickHandler; import com.openwaf.client.ui.annotation.ViewElement; import com.openwaf.client.ui.basic.Button; import com.openwaf.client.ui.basic.Textbox; import com.openwaf.client.ui.panels.TabPanel; import com.openwaf.client.ui.panels.TabPanelElement; import com.openwaf.core.framework.WAFController; public class ExMiscCalc extends WAFController { @ViewElement private Textbox txtInput; @ViewElement private Button btn0; @ViewElement private Button btn1; @ViewElement private Button btn2; @ViewElement private Button btn3; @ViewElement private Button btn4; @ViewElement private Button btn5; @ViewElement private Button btn6; @ViewElement private Button btn7; @ViewElement private Button btn8; @ViewElement private Button btn9; @ViewElement private Button btnClear; @ViewElement private Button btnEqual; @ViewElement private Button btnPlus; @ViewElement private Button btnDivide; @ViewElement private Button btnMinus; @ViewElement private Button btnMultiply; private final static int OP_PLUS = 0; private final static int OP_MINUS = 1; private final static int OP_MULTIPLY = 2; private final static int OP_DIVIDE = 3; private final static int OP_NONE = 4; private int operation = OP_NONE; private float operand1 = 0; public ExMiscCalc() { btn0.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { digitPress(0); } }); btn1.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { digitPress(1); } }); btn2.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { digitPress(2); } }); btn3.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { digitPress(3); } }); btn4.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { digitPress(4); } }); btn5.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { digitPress(5); } }); btn6.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { digitPress(6); } }); btn7.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { digitPress(7); } }); btn8.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { digitPress(8); } }); btn9.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { digitPress(9); } }); btnPlus.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { operation = OP_PLUS; operand1 = Float.parseFloat(txtInput.getValue()); txtInput.setValue("0"); } }); btnMinus.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { operation = OP_MINUS; operand1 = Float.parseFloat(txtInput.getValue()); txtInput.setValue("0"); } }); btnMultiply.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { operation = OP_MULTIPLY; operand1 = Float.parseFloat(txtInput.getValue()); txtInput.setValue("0"); } }); btnDivide.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { operation = OP_DIVIDE; operand1 = Float.parseFloat(txtInput.getValue()); txtInput.setValue("0"); } }); btnEqual.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { float operand2 = Float.parseFloat(txtInput.getValue()); switch (operation) { case OP_DIVIDE: txtInput.setValue("" + (operand1 / operand2)); break; case OP_PLUS: txtInput.setValue("" + (operand1 + operand2)); break; case OP_MULTIPLY: txtInput.setValue("" + (operand1 * operand2)); break; case OP_MINUS: txtInput.setValue("" + (operand1 - operand2)); break; } } }); btnClear.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { txtInput.setValue("0"); operand1 = 0; operation = OP_NONE; } }); } private void digitPress(int value) { txtInput.setText(txtInput.getValue() + value); } }