/* * @(#)TFVsampleAlternate.java 1.0 14 July 2001 * * Alternate design that maximizes the use of array initializers * * 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 27 April 2001 */ public class TFVsampleAlternate extends DisplayCollection implements JPTConstants { //////////////////// // View Constants // //////////////////// // constants: used to set and reset text fields private static final String XVALUE = "10"; private static final String YVALUE = "15"; private static final String ZVALUE = "20"; private static final String WVALUE = "25"; ///////////////// // Member Data // ///////////////// ////////////////// // View Section // ////////////////// // input text fields // xTFVC will be used to input an optional integer input private TextFieldView xTFV = new TextFieldView( XVALUE, // initial value displayed in the TFV "X has to be an integer:", // prompt for correcting the input "Incorrect input"); // title for the error dialog box // yTFVC will be used to input an optional integer 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 an integer:", // prompt for correcting the input "Incorrect input"); // title for the error dialog box // zTFVC will be used to input a required integer input private TextFieldView zTFV = new TextFieldView( ZVALUE, // initial value displayed in the TFV "Z has to be an integer:", // prompt for correcting the input "Incorrect input"); // title for the error dialog box // wTFVC will be used to input a required integer 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 an integer:", // 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 // ///////////////// // array of wrapped text field views private Displayable[] tfvList = { new DisplayWrapper(new Display(xTFV, "X:", null)), new DisplayWrapper(new Display(yTFV, "Y:", null)), new DisplayWrapper(new Display(zTFV, "Z:", null)), new DisplayWrapper(new Display(wTFV, "W:", null)), new DisplayWrapper(new Display(sumTFV, "Sum:", null)) }; // display collection of the wrapped text field views private DisplayCollection dataFields = new DisplayCollection(tfvList); // display to wrap the wrapped text field view collection private Display dataFieldsDisplay = new Display(dataFields, null, "Data Fields"); // display to wrap the actions panel private Display actionsDisplay = new Display(actions, null, "Actions"); ////////////////// // Main Program // ////////////////// /* * create a window * titled "Simple Adder" * whose contents are defined by the TFVsampleAlternate constructor */ public static void main(String[] args) { JPTFrame.createQuickJPTFrame("Simple Adder", new TFVsampleAlternate()); } ///////////////// // 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 */ public TFVsampleAlternate() { ////////////////// // 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("100"); wTFV.getInputProperties().setSuggestion("200"); // enable return key action xTFV.addActionListener(add); yTFV.addActionListener(add); zTFV.addActionListener(add); wTFV.addActionListener(add); ///////////////// // GUI Section // ///////////////// 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() { int x; int y; int z; int w; int 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.requestInt(); } 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.requestInt(); } 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.demandInt(); ////////////////////////////////// // mandatory input using demand // ////////////////////////////////// // note that this text field view has a suggestion for error recovery w = wTFV.demandInt(); // 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(""); } }