import java.awt.*; import java.awt.event.*; /** * GUI to display FunListFW. * I use this to explain: * 1. rudiments of AWT using the layout manager as a "strategy" for layout GUI components * 2. rudiments of event handlers also as strategy for listening and responding to events. * 3. the Model-View-Controller (MVC) paradigm. * Copyright 1999, Dung X. Nguyen - All rights reserved. * * @version 1.01 Sept. 30, 1999 * @author Dung X. Nguyen */ public class FunFWGUI { /************************************************************* * (private) Attributes of the GUI: what the GUI has. * They consist of two groups: one is for the GUI, and the other * is for the FunListFW itself. */ private Frame _frame; // The frame for the GUI. private TextField _txtIn; // Input text for list methods list visitors' methods. private TextField _txtOut; // Output text. private TextArea _taListView; // a TextArea view of the list model private TextField _txtListView; // a TextField view of the list model /** * The list model. * @SBGen Variable (,model,controller-view,64) */ private FunListFW _list; /************************************************************** * Initializes the GUI to a well-defined state. * Need to carry out the following tasks: * 1. initialize the internal state of the soda machine, * 2. add the desired GUI components, and * 3. register events fired by the active components with appropriate event listeners. */ public FunFWGUI() { Panel pnlView = makeViewPanel (); Panel pnlUserInput = makeUserInputPanel (); _frame = new Frame("FunList Framework GUI"); ((BorderLayout)(_frame.getLayout ())).setHgap (10); ((BorderLayout)(_frame.getLayout ())).setVgap (10); _frame.add (pnlView, BorderLayout.CENTER); _frame.add (pnlUserInput, "West"); _frame.addWindowListener (new CloseWindowListener()); _frame.setSize (240, 300); _frame.setVisible (true); _txtIn.selectAll (); _txtIn.requestFocus (); } private Panel makeViewPanel () { _list = EmptyFW.Singleton (); _taListView = new TextArea (); _taListView.setEditable (false); _txtListView = new TextField (_list.toString ()); _txtListView.setEditable (false); Panel pnl = new Panel (); pnl.setLayout (new BorderLayout (10, 10)); pnl.add (new Label ("List Display"), "North"); pnl.add (_taListView, BorderLayout.CENTER); pnl.add (_txtListView, "South"); return pnl; } private Panel makeUserInputPanel () { Panel pnlInOut = makeInOutPanel (); Panel pnlButtons = makeButtonPanel (); Panel pnlInput = new Panel (); pnlInput.setLayout (new BorderLayout (10, 10)); pnlInput.add (pnlInOut, "North"); pnlInput.add (pnlButtons, "South"); return pnlInput; } private Panel makeInOutPanel () { // Make a panel for input text: _txtIn = new TextField("0", 4); Panel pnlIn = new Panel (); pnlIn.setLayout (new BorderLayout ()); pnlIn.add (new Label ("Input Integer:"), "North"); pnlIn.add (_txtIn, "South"); // Make a panel for output text: _txtOut = new TextField("0", 4); Panel pnlOut = new Panel (); pnlOut.setLayout (new BorderLayout ()); pnlOut.add (new Label ("Output:"), "North"); pnlOut.add (_txtOut, "South"); // Make a panel for both input and output texts and return this panel: Panel pnlInOut = new Panel (); pnlInOut.setLayout (new BorderLayout ()); pnlInOut.add (pnlIn, "North"); pnlInOut.add (pnlOut, "South"); return pnlInOut; } private Panel makeButtonPanel () { Button btnEmpty = new Button ("Empty"); btnEmpty.addActionListener (new EmptyListener ()); Button btnCons = new Button ("Cons"); btnCons.addActionListener (new ConsListener ()); Button btnSum = new Button ("Sum"); btnSum.addActionListener (new SumListener ()); Button btnGetNth = new Button ("GetNth"); btnGetNth.addActionListener (new GetNthListener ()); Panel pnlButtons = new Panel (); pnlButtons.setLayout (new GridLayout (4, 1, 10, 10)); pnlButtons.add (btnEmpty); pnlButtons.add (btnCons); pnlButtons.add (btnSum); pnlButtons.add (btnGetNth); return pnlButtons; } /************************************************************* * Clears the text area and sets the list model to Empty. */ class EmptyListener implements ActionListener { public void actionPerformed(ActionEvent e) { _list = EmptyFW.Singleton (); _taListView.setText (""); _txtListView.setText (_list.toString ()); } } /************************************************************* * Inserts to the front of the list model and displays it in the text area. */ class ConsListener implements ActionListener { public void actionPerformed(ActionEvent e) { try { // For debugging and illustration purpose: System.out.println ("ConsListener.actionPerformed: input = " + _txtIn.getText ()); int i = Integer.parseInt(_txtIn.getText ()); _list = new ConsFW (new Integer (i), _list); _taListView.setText (""); _list.execute (TextAreaPrint.Singleton (), _taListView); _txtListView.setText (_list.toString ()); } catch (NumberFormatException x) { // For debugging and illustration purpose: System.out.println ("ConsListener.actionPerformed: input " + _txtIn.getText () + " is not a number!"); } _txtIn.selectAll (); _txtIn.requestFocus (); } } /************************************************************* * Sums the integer elements in the list model and displays result in output text field. */ class SumListener implements ActionListener { public void actionPerformed(ActionEvent e) { Integer sumInt = (Integer)_list.execute (SumAlgo.Singleton (), null); _txtOut.setText (sumInt.toString ()); } } /************************************************************* * */ class GetNthListener implements ActionListener { public void actionPerformed(ActionEvent e) { int n = Integer.parseInt(_txtIn.getText ()); Integer nthInt = (Integer)_list.execute (GetNth.Singleton (), new Integer (n)); _txtOut.setText (nthInt.toString ()); } } /************************************************************* * Standard way to free system's resources for Window class when closing. */ class CloseWindowListener extends WindowAdapter { public void windowClosing(WindowEvent e) { _frame.dispose (); //frees system's resources. System.exit(0); } } /************************************************************** * Entry point into the program * @param args The parameters for the entry. They are not used here. */ public static void main(String[] args) { new FunFWGUI(); } }