/**
 * Created on Oct 2, 2004
 * 
 * @author Alex Coleman
 *
 * Modified version of Adder2.java
 * User inputs radius of a circle
 * Area of the circle is outputted
 * 
 */

/*
 * @(#)Adder2.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 CircleArea extends DisplayPanel implements JPTConstants {
    
    // text fields
    protected TextFieldView aTFV   = new TextFieldView
        ("1", "Correct the value in field A:", "Double Input Error", "0", 200);
         
    protected TextFieldView sumTFV = new TextFieldView("",  200);
    
    // table panel for text fields
    protected TablePanel tfvPanel
        = new TablePanel(
            new Object[][] {
                { "Radius",   new Halo(aTFV)   },
                { "Area:", new Halo(sumTFV) }
            },
            5, 5, EAST);
    
    
    // action: find area of circle with radius of a
    protected Action sum 
        = new SimpleAction("Find Area") {
           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: find area of circle with radius a
    protected void sum() {
        try {
            double a = aTFV.requestDouble();
            sumTFV.setViewState("" + (a * a * 3.14159));
        }
        catch (CancelledException ex) {
            sumTFV.setViewState("");
        }
    }
    
    // method: clear all text fields to blank
    protected void clear() {
        aTFV.setViewState("");
        sumTFV.setViewState("");
    }
    
    /* reset() defined in class DisplayPanel */
    
    
    // constructor
    public CircleArea () { add(mainPanel); }
    
    
    // main program
    public static void main(String[] args) {
        JPTFrame.createQuickJPTFrame("Area of a Circle", new CircleArea ());            
    }
}


