CookSwt計算機

CookSwtの練習に電卓を作ってみた。整数しか扱えない。小数点とか入力のロジックが面倒なのでやめておいた。けっこういけるかも?画面レイアウトをJavaコードから追い出して、リスナーだけになるし、バインドも簡単。
あとはGUIビルダーがあれば。。。せめてDTDだけでも。。。


CSCalc.xml

<?xml version="1.0" encoding="UTF-8"?>
<display>
  <shell style="SHELL_TRIM" text="CookSwt Calculator" size="200,300">
    <gridlayout numcolumns="4">
      <griddata style="FILL_BOTH" horizontalspan="4">
        <textarea text="0" var="numWindow" style="BORDER" />
      </griddata>
      <griddata style="FILL_BOTH">
        <button text="Clr" selectionlistener="clearAction" />
      </griddata>
      <griddata style="FILL_BOTH">
        <button text="/" selectionlistener="operatorAction"/>
      </griddata>
      <griddata style="FILL_BOTH">
        <button text="*" selectionlistener="operatorAction" />
      </griddata>
      <griddata style="FILL_BOTH">
        <button text="-" selectionlistener="operatorAction" />
      </griddata>
      <griddata style="FILL_BOTH">
        <button text="7" selectionlistener="numAction" />
      </griddata>
      <griddata style="FILL_BOTH">
        <button text="8" selectionlistener="numAction" />
      </griddata>
      <griddata style="FILL_BOTH">
        <button text="9" selectionlistener="numAction" />
      </griddata>
      <griddata verticalspan="2" style="FILL_BOTH">
        <button text="+" selectionlistener="operatorAction" />
      </griddata>
      <griddata style="FILL_BOTH">
        <button text="4" selectionlistener="numAction" />
      </griddata>
      <griddata style="FILL_BOTH">
        <button text="5" selectionlistener="numAction" />
      </griddata>
      <griddata style="FILL_BOTH">
        <button text="6" selectionlistener="numAction" />
      </griddata>
      <griddata style="FILL_BOTH">
        <button text="1" selectionlistener="numAction" />
      </griddata>
      <griddata style="FILL_BOTH">
        <button text="2" selectionlistener="numAction" />
      </griddata>
      <griddata style="FILL_BOTH">
        <button text="3" selectionlistener="numAction" />
      </griddata>
      <griddata verticalspan="2" style="FILL_BOTH">
        <button text="=" selectionlistener="equalAction" />
      </griddata>
      <griddata horizontalspan="3" style="FILL_BOTH">
        <button text="0" selectionlistener="numAction" />
      </griddata>
    </gridlayout>
  </shell>
</display>


CSCalc.java

package faiz.calculator;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Text;

import cookxml.cookswt.CookSwt;
import cookxml.cookswt.util.SwtUtils;

public class CSCalc {

    private int currentNum;
    private int storedNum;
    private String operator = "=";

    public Text numWindow;

    public SelectionListener numAction = new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            Button b = (Button) e.getSource();
            currentNum = 10 * currentNum + Integer.parseInt(b.getText());
            showNumber();
        }
    };

    public SelectionListener operatorAction = new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            Button b = (Button) e.getSource();
            operator = b.getText();
            storedNum = currentNum;
            currentNum = 0;
            showNumber();
        }
    };

    public SelectionListener equalAction = new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            if (operator.equals("+"))
                currentNum = storedNum + currentNum;
            if (operator.equals("-"))
                currentNum = storedNum - currentNum;
            if (operator.equals("*"))
                currentNum = storedNum * currentNum;
            if (operator.equals("/"))
                currentNum = storedNum / currentNum;
            storedNum = currentNum;
            showNumber();
            operator = "=";
        }
    };

    public SelectionListener clearAction = new SelectionAdapter() {
        public void widgetSelected(SelectionEvent arg0) {
            currentNum = 0;
            storedNum = 0;
            operator = "=";
            showNumber();
        }
    };

    public static void main(String[] args) {
        CSCalc cs = new CSCalc();
        cs.show();
    }

    private void show() {
        CookSwt cookSwt = new CookSwt(this);
        SwtUtils.showDisplay((Display) cookSwt
                .xmlDecode("faiz/calculator/CSCalc.xml"));
    }

    private void showNumber() {
        numWindow.setText(Integer.toString(currentNum));
    }
}