001: package org.millstone.examples;
002:
003: import org.millstone.base.ui.*;
004:
005: /** <p>An example application implementing a simple web-based calculator.
006: * using the MillStone UI library. The application opens up a window and
007: * places the needed UI components (display label, buttons etc.) on it, and
008: * registers a button click listener for them.</p>
009: *
010: * <p>When any of the buttons are pressed the application finds out which
011: * button was pressed, figures what that button does, and updates the user
012: * interface accordingly.</p>
013: *
014: * @see org.millstone.base.Application
015: * @see org.millstone.base.ui.Button.ClickListener
016: */
017: public class Calc extends org.millstone.base.Application implements
018: Button.ClickListener {
019:
020: /** The label used as the display */
021: private Label display = null;
022:
023: /** Last completed result */
024: private double stored = 0.0;
025:
026: /** The number being currently edited. */
027: private double current = 0.0;
028:
029: /** Last activated operation. */
030: private String operation = "C";
031:
032: /** Button captions. */
033: private static String[] captions = // Captions for the buttons
034: { "7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0",
035: "=", "C", "+" };
036:
037: /** <p>Initializes the application. This is the only method a MillStone
038: * application is required to implement. It's called by the framework
039: * and it should perform whatever initialization tasks the application
040: * needs to perform.</p>
041: *
042: * <p>In this case we create the main window, the display, the grid to
043: * hold the buttons, and the buttons themselves.</p>
044: */
045: public void init() {
046:
047: /*
048: * Create a new {@link org.millstone.base.ui.GridLayout GridLayout}
049: * to hold the UI components needed by the calculator.
050: */
051: GridLayout layout = new GridLayout(4, 5);
052:
053: //Create a new label component for displaying the result
054: display = new Label(Double.toString(current));
055: display.setCaption("Result");
056:
057: // Place the label to the top of the previously created grid.
058: layout.addComponent(display, 0, 0, 3, 0);
059:
060: // Create the buttons and place them in the grid
061: for (int i = 0; i < captions.length; i++) {
062: layout.addComponent(new Button(captions[i], this ));
063: }
064:
065: /*
066: * Create the main window with a caption and add it to the
067: * application.
068: */
069: addWindow(new Window("MillStone calculator", layout));
070:
071: }
072:
073: /** <p>The button listener method called any time a button is pressed.
074: * This method catches all button presses, figures out what the user
075: * wanted the application to do, and updates the UI accordingly.</p>
076: *
077: * <p>The button click event passed to this method contains information
078: * about which button was pressed. If it was a number, the currently
079: * edited number is updated. If it was something else, the requested
080: * operation is performed. In either case the display label is updated
081: * to include the outcome of the button click.</p>
082: *
083: * @param event the button click event specifying which button was
084: * pressed
085: */
086: public void buttonClick(Button.ClickEvent event) {
087:
088: try {
089: // Number button pressed
090: current = current
091: * 10
092: + Double
093: .parseDouble(event.getButton().getCaption());
094: display.setValue(Double.toString(current));
095: } catch (java.lang.NumberFormatException e) {
096:
097: // Operation button pressed
098: if (operation.equals("+"))
099: stored += current;
100: if (operation.equals("-"))
101: stored -= current;
102: if (operation.equals("*"))
103: stored *= current;
104: if (operation.equals("/"))
105: stored /= current;
106: if (operation.equals("C"))
107: stored = current;
108: if (event.getButton().getCaption().equals("C"))
109: stored = 0.0;
110: operation = event.getButton().getCaption();
111: current = 0.0;
112: display.setValue(Double.toString(stored));
113: }
114: }
115: }
116:
117: /* This Millstone sample code is public domain. *
118: * For more information see www.millstone.org. */
|