aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/itmill/toolkit/demo/Calc.java
blob: 7d57debc993d2c3d9dbd9bfcfd00a8a7fc10ce4e (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package com.itmill.toolkit.demo;

import com.itmill.toolkit.ui.*;

/**
 * <p>
 * An example application implementing a simple web-based calculator using IT
 * Mill Toolkit. The application opens up a window and places the needed UI
 * components (display label, buttons etc.) on it, and registers a button click
 * listener for them.
 * </p>
 * 
 * <p>
 * When any of the buttons are pressed the application finds out which button
 * was pressed, figures what that button does, and updates the user interface
 * accordingly.
 * </p>
 * 
 * @see com.itmill.toolkit.Application
 * @see com.itmill.toolkit.ui.Button.ClickListener
 */
public class Calc extends com.itmill.toolkit.Application implements
		Button.ClickListener {

	/** The label used as the display */
	private Label display = null;

	/** Last completed result */
	private double stored = 0.0;

	/** The number being currently edited. */
	private double current = 0.0;

	/** Last activated operation. */
	private String operation = "C";

	/** Button captions. */
	private static String[] captions = // Captions for the buttons
	{ "7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", "=",
			"C", "+" };

	/**
	 * <p>
	 * Initializes the application. This is the only method an application is
	 * required to implement. It's called by the framework and it should perform
	 * whatever initialization tasks the application needs to perform.
	 * </p>
	 * 
	 * <p>
	 * In this case we create the main window, the display, the grid to hold the
	 * buttons, and the buttons themselves.
	 * </p>
	 */
	public void init() {

		// Create a new layout for the components used by the calculator
		GridLayout layout = new GridLayout(4, 5);

		// Create a new label component for displaying the result
		display = new Label(Double.toString(current));
		display.setCaption("Result");

		// Place the label to the top of the previously created grid.
		layout.addComponent(display, 0, 0, 3, 0);

		// Create the buttons and place them in the grid
		for (int i = 0; i < captions.length; i++) {
			Button button = new Button(captions[i], this);
			layout.addComponent(button);
		}

		// Create the main window with a caption and add it to the application.
		addWindow(new Window("Calculator", layout));

	}

	/**
	 * <p>
	 * The button listener method called any time a button is pressed. This
	 * method catches all button presses, figures out what the user wanted the
	 * application to do, and updates the UI accordingly.
	 * </p>
	 * 
	 * <p>
	 * The button click event passed to this method contains information about
	 * which button was pressed. If it was a number, the currently edited number
	 * is updated. If it was something else, the requested operation is
	 * performed. In either case the display label is updated to include the
	 * outcome of the button click.
	 * </p>
	 * 
	 * @param event
	 *            the button click event specifying which button was pressed
	 */
	public void buttonClick(Button.ClickEvent event) {

		try {
			// Number button pressed
			current = current * 10
					+ Double.parseDouble(event.getButton().getCaption());
			display.setValue(Double.toString(current));
		} catch (java.lang.NumberFormatException e) {

			// Operation button pressed
			if (operation.equals("+"))
				stored += current;
			if (operation.equals("-"))
				stored -= current;
			if (operation.equals("*"))
				stored *= current;
			if (operation.equals("/"))
				stored /= current;
			if (operation.equals("C"))
				stored = current;
			if (event.getButton().getCaption().equals("C"))
				stored = 0.0;
			operation = event.getButton().getCaption();
			current = 0.0;
			display.setValue(Double.toString(stored));
		}
	}
}