import java.awt.*; import java.awt.event.*; /** * Another version of the MVC pattern to display FunList. * In this version, all the controllers for the view of the list model are * created in a separate class called FLControl. The view, FLGUI, has hook * methods to install the necessary controllers. * * Copyright 1999, Dung X. Nguyen - All rights reserved. * @version 1.1 Oct. 08, 1999 * @author Dung X. Nguyen * @author Robert S. Cartwright * @dependency java.awt.event.ActionListener uses */ public class FLGUI { // Initialize all components: private TextField txtIn = new TextField("0", 4); // Input text for list and visitors' methods. private TextField txtOut = new TextField("0", 4); // Output text. private TextArea taListView = new TextArea (); // a TextArea view of the list model private TextField txtListView = new TextField (); // a TextField view of the list model // Buttons to manipulate the list model: private Button btnEmpty = new Button("Empty"); private Button btnCons = new Button("Cons"); private Button btnSum = new Button("Sum"); private Button btnGetNth = new Button("GetNth"); /** * Lays out GUI components, registers a listener for WindowClosing, and initializes the GUI to a well-defined state. */ public FLGUI() { // Set up a frame for the GUI components to be displayed. final Frame frame // MUST be final to be accessible by inner classes. = new Frame("FunList Framework GUI Application"); ((BorderLayout)(frame.getLayout ())).setHgap (10); ((BorderLayout)(frame.getLayout ())).setVgap (10); frame.add(makeViewPanel(), BorderLayout.CENTER); frame.add(makeUserInputPanel(), "West"); // Standard use of anonymous WindowAdapter class to free system's resources when closing. // This is a view property. frame.addWindowListener ( new WindowAdapter() { public void windowClosing(WindowEvent e) { frame.dispose(); // In order for this inner class to access frame, frame MUST be final. System.exit(0); } } ); frame.setSize(300, 300); frame.setVisible(true); txtIn.selectAll(); txtIn.requestFocus(); } private Panel makeViewPanel() { taListView.setEditable(false); 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; } public void updateView(FunList fl) { // Update the TextArea View: taListView.setText (""); fl.execute(new TextAreaPrint(taListView)); // Update the TextField view: txtListView.setText(fl.toString()); } 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: 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: 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; } public String getInput() { txtIn.selectAll(); txtIn.requestFocus(); return txtIn.getText(); } public void setOutput(String s) { txtOut.setText(s); } private Panel makeButtonPanel() { 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; } /** * Registers an ActionListener for the Empty button. */ public void addEmptyListener(ActionListener al) { btnEmpty.addActionListener(al); } /** * Registers an ActionListener for the Cons button. */ public void addConsListener(ActionListener al) { btnCons.addActionListener(al); } /** * Registers an ActionListener for the Sum button. */ public void addSumListener(ActionListener al) { btnSum.addActionListener(al); } /** * Registers an ActionListener for the GetNth button. */ public void addGetNthListener(ActionListener al) { btnGetNth.addActionListener(al); } }