summaryrefslogtreecommitdiffstats
path: root/src/com/itmill/toolkit/demo/Calc.java
blob: 0955f34d1f5938a3dad0de7bbf99f6506e7b506c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package com.itmill.toolkit.demo;

import com.itmill.toolkit.ui.Button;
import com.itmill.toolkit.ui.GridLayout;
import com.itmill.toolkit.ui.Label;
import com.itmill.toolkit.ui.Window;

// Calculator is created by extending Application-class. Application is
// deployed by adding ApplicationServlet to web.xml and this class as
// "application" parameter to the servlet.
public class Calc extends com.itmill.toolkit.Application {

    // Calculation data model is automatically stored in the user session
    private double current = 0.0;
    private double stored = 0.0;
    private char lastOperationRequested = 'C';

    // User interface components
    private final Label display = new Label("0.0");
    private final GridLayout layout = new GridLayout(4, 5);

    // Application initialization creates UI and connects it to business logic
    @Override
    public void init() {

        // Place the layout to a floating dialog inside the browser main window
        setMainWindow(new Window("Calculator Application"));
        getMainWindow().addWindow(new Window("Calc", layout));

        // Create and add the components to the layout
        layout.addComponent(display, 0, 0, 3, 0);
        for (String caption : new String[] { "7", "8", "9", "/", "4", "5", "6",
                "*", "1", "2", "3", "-", "0", "=", "C", "+" }) {
            Button button = new Button(caption, new Button.ClickListener() {
                public void buttonClick(Button.ClickEvent event) {

                    // On button click, calculate and show the result
                    display.setValue(calculate(event.getButton()));
                }
            });
            layout.addComponent(button);
        }
    }

    // Calculator "business logic" implemented here to keep the example minimal
    private double calculate(Button buttonClicked) {
        char requestedOperation = buttonClicked.getCaption().charAt(0);
        if ('0' <= requestedOperation && requestedOperation <= '9') {
            current = current * 10
                    + Double.parseDouble("" + requestedOperation);
            return current;
        }
        switch (lastOperationRequested) {
        case '+':
            stored += current;
            break;
        case '-':
            stored -= current;
            break;
        case '/':
            stored /= current;
            break;
        case '*':
            stored *= current;
            break;
        case 'C':
            stored = current;
            break;
        }
        lastOperationRequested = requestedOperation;
        current = 0.0;
        if (requestedOperation == 'C') {
            stored = 0.0;
        }
        return stored;
    }
}