/*
 * @(#)Adder1.java    15 March 2004
 *
 * Copyright 2004
 * College of Computer Science
 * Northeastern University
 * Boston, MA  02115
 *
 * This software may be used for educational purposes as long as
 * this copyright notice is retained intact at the top of all files.
 *
 * Should this software be modified, the words "Modified from 
 * Original" must be included as a comment below this notice.
 *
 * All publication rights are retained.  This software or its 
 * documentation may not be published in any media either in whole
 * or in part without explicit permission.
 *
 * Contact information:
 *   Richard Rasala    rasala@ccs.neu.edu
 *   Viera Proulx      vkp@ccs.neu.edu
 * 
 * Telephone:          617-373-2462
 *
 * This software was created with support from Northeastern 
 * University and from NSF grant DUE-9950829.
 */

import edu.neu.ccs.gui.*;
import edu.neu.ccs.util.*;

import javax.swing.*;

public class Adder1 extends DisplayPanel implements JPTConstants {
    
    // text fields
    protected TextFieldView aTFV   = new TextFieldView("1", 200);
    protected TextFieldView bTFV   = new TextFieldView("2", 200);
    protected TextFieldView cTFV   = new TextFieldView("3", 200);
    protected TextFieldView dTFV   = new TextFieldView("4", 200);
    protected TextFieldView sumTFV = new TextFieldView("",  200);
    
    // table panel for text fields
    protected TablePanel tfvPanel
        = new TablePanel(
            new Object[][] {
                { "A:",   aTFV   },
                { "B:",   bTFV   },
                { "C:",   cTFV   },
                { "D:",   dTFV   },
                { "Sum:", sumTFV }
            },
            5, 5, EAST);
    
    
    // action: sum a + b + c + d
    protected Action sum 
        = new SimpleAction("Sum") {
           public void perform() { sum(); }
        };
    
    // action: clear all text fields to blank
    protected Action clear 
        = new SimpleAction("Clear") {
           public void perform() { clear(); }
        };
    
    // action: reset all text fields to original values
    protected Action reset
        = new SimpleAction("Reset") {
           public void perform() { reset(); }
        };
    
    // button panel
    protected ActionsPanel buttonPanel
        = new ActionsPanel(new Action[] { sum, clear, reset });
    
    
    // table panel to enclose text fields and actions
    protected TablePanel mainPanel
        = new TablePanel(
            new Object[] { tfvPanel, buttonPanel },
            VERTICAL, 10, 10, CENTER);
    
    
    // action methods

    // method: sum a + b + c + d
    protected void sum() {
        double a = aTFV.demandDouble();
        double b = bTFV.demandDouble();
        double c = cTFV.demandDouble();
        double d = dTFV.demandDouble();
        sumTFV.setViewState("" + (a + b + c + d));
    }
    
    // method: clear all text fields to blank
    protected void clear() {
        aTFV.setViewState("");
        bTFV.setViewState("");
        cTFV.setViewState("");
        dTFV.setViewState("");
        sumTFV.setViewState("");
    }
    
    /* reset() defined in class DisplayPanel */
    
    
    // constructor
    public Adder1() { add(mainPanel); }
    
    
    // main program
    public static void main(String[] args) {
        JPTFrame.createQuickJPTFrame("Adder 1", new Adder1());            
    }
}