/* * @(#)TFVdoubleSample.java 1.0 14 July 2001 * * Copyright 2001 * 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 * Jeff Raab jmr@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.*; import edu.neu.ccs.gui.*; import edu.neu.ccs.util.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; /** * Simple illustration of four TextFieldView GUI components and * a simple action - addition of four integers * * @author Viera K. Proulx * @author Jennifer McDonald * @author Richard Rasala * @version 26 May 2001 */ public class TFVdoubleSample extends DisplayCollection implements JPTConstants { //////////////////// // View Constants // //////////////////// // constants: used to set and reset text fields private static final String XVALUE = "10.5"; private static final String YVALUE = "2*pi"; private static final String ZVALUE = "e"; private static final String WVALUE = "5.2/2"; ///////////////// // Member Data // ///////////////// ////////////////// // View Section // ////////////////// // input text fields // xTFVC will be used to input an optional double input private TextFieldView xTFV = new TextFieldView( XVALUE, // initial value displayed in the TFV "X has to be a double:", // prompt for correcting the input "Incorrect input"); // title for the error dialog box // yTFVC will be used to input an optional double input // later, we will add a suggestion - to will be used with error prompt private TextFieldView yTFV = new TextFieldView( YVALUE, // initial value displayed in the TFV "Y has to be a double:", // prompt for correcting the input "Incorrect input"); // title for the error dialog box // zTFVC will be used to input a required double input private TextFieldView zTFV = new TextFieldView( ZVALUE, // initial value displayed in the TFV "Z has to be a double:", // prompt for correcting the input "Incorrect input"); // title for the error dialog box // wTFVC will be used to input a required double input // later, we will add a suggestion - to will be used with error prompt private TextFieldView wTFV = new TextFieldView( WVALUE, // initial value displayed in the TFV "W has to be a double:", // prompt for correcting the input "Incorrect input"); // title for the error dialog box // output text field // sumTFV will only be used to display the result // Last two parameters are never used // they pertain to the modal error dialog box - not needed for output private TextFieldView sumTFV = new TextFieldView( "", // no value is displayed until // the computation is completed "No fix needed", // prompt for correction - not needed "Output only"); // title for error dialog - not needed //////////////////// // Action Section // //////////////////// // the actions for the actions panel /* * the add action is constructed using * the String "Add" to name its button * and specifying the add method to be performed when pressed */ private Action add = new SimpleAction("Add") { public void perform(){ add(); } }; /* * the clear action is constructed using * the String "Clear" to name its button * and specifying the clear method to be performed when pressed */ private Action clear = new SimpleAction("Clear") { public void perform(){ clear(); } }; // list of actions to be included in the actions panel /* * array of actions is constructed */ private Action[] actionList = {add, clear}; // actions panel /* * actions panel is constructed * with the actions it will contain supplied as parameter */ private ActionsPanel actions = new ActionsPanel(actionList); ///////////////// // GUI Section // ///////////////// // collection of the text field views for the GUI private DisplayCollection dataFields = new DisplayCollection(); ////////////////// // Main Program // ////////////////// /* * create a window * titled "Simple Adder" * whose contents are defined by the TFVsample constructor */ public static void main(String[] args) { JPTFrame.createQuickJPTFrame("Simple Double Adder", new TFVdoubleSample()); } ///////////////// // Constructor // ///////////////// /* * constructor for the graphical user interface of the application * installs four input views and one output view * installs an action panel with two action buttons - add and clear * sets the preferred width for all text field views * sets the suggestion for two of the four input views * wraps and titles the data fields and actions * and adds them to the main panel */ public TFVdoubleSample() { ////////////////// // View Section // ////////////////// // set preferred width for the text fields xTFV.setPreferredWidth(150); yTFV.setPreferredWidth(150); zTFV.setPreferredWidth(150); wTFV.setPreferredWidth(150); sumTFV.setPreferredWidth(132); // set suggestions yTFV.getInputProperties().setSuggestion("10.0"); wTFV.getInputProperties().setSuggestion("20.0"); ///////////////// // GUI Section // ///////////////// // add text fields and actions panel to the dataFields DisplayCollection // add xTFV to dataFields dataFields.add( new DisplayWrapper( new Display(xTFV, "X:", null))); // add yTFV to dataFields dataFields.add( new DisplayWrapper( new Display(yTFV, "Y:", null))); // add zTFV to dataFields dataFields.add( new DisplayWrapper( new Display(zTFV, "Z:", null))); // add wTFV to dataFields dataFields.add( new DisplayWrapper( new Display(wTFV, "W:", null))); // add sumTFV to dataFields dataFields.add( new DisplayWrapper( new Display(sumTFV, "Sum:", null))); // build two titled displays and add them to the main panel // wrap dataFields into a titled display Display dataFieldsDisplay = new Display(dataFields, null, "Data Fields"); // wrap actions into a titled display Display actionsDisplay = new Display(actions, null, "Actions"); // add data field display and actions display to the main panel add(dataFieldsDisplay); add(actionsDisplay); } //////////////////////// // Action Definitions // //////////////////////// /* * add action * * add the value of the integers whose values are extracted from the input * text field views and display the result in the sumTextFieldView */ public void add() { double x; double y; double z; double w; double sum; ////////////////////////////////// // optional input using request // ////////////////////////////////// // try-catch is needed since user may cancel the input operation // note that no suggestion is specified for this text field view try { x = xTFV.requestDouble(); } catch (CancelledException ex) { // user input is invalid, so invalidate the sum view and return sumTFV.setViewState(""); return; } ////////////////////////////////// // optional input using request // ////////////////////////////////// // try-catch is needed since user may cancel the input operation // note that this text field view has a suggestion for error recovery try { y = yTFV.requestDouble(); } catch (CancelledException ex) { // user input is invalid, so invalidate the sum view and return sumTFV.setViewState(""); return; } ////////////////////////////////// // mandatory input using demand // ////////////////////////////////// // note that no suggestion is specified for this text field view z = zTFV.demandDouble(); ////////////////////////////////// // mandatory input using demand // ////////////////////////////////// // note that this text field view has a suggestion for error recovery w = wTFV.demandDouble(); // compute the sum sum = x + y + z + w; // update the sum text field view to show the result sumTFV.setViewState(sum + ""); } /* * clear action * * set the four input views to display the values specified originally * in the constructor, and clear the sum text field view */ public void clear() { // reset all view states to those used in the constructor xTFV.setViewState(XVALUE); yTFV.setViewState(YVALUE); zTFV.setViewState(ZVALUE); wTFV.setViewState(WVALUE); sumTFV.setViewState(""); } }