/* Author: Jeff Dalton * Updated: Sun Dec 2 00:13:22 2001 by Jeff Dalton * Copyright: (c) 2001, AIAI, University of Edinburgh */ package ix.util; import javax.swing.*; import java.awt.Container; import java.awt.BorderLayout; import java.awt.event.*; import java.util.*; import ix.util.*; import ix.util.lisp.*; import ix.iface.util.TranscriptTextArea; /** * A TextArea that has its own frame and some buttons. */ public class TextAreaFrame extends TranscriptTextArea { TFrame frame; List listeners = new LinkedList(); public TextAreaFrame(String title) { this(title, new String[] {}); } public TextAreaFrame(String title, String[] buttons) { // The rows and cols would be right only with a fixed-width font. /\/ super(10, 60); // 10 rows, 60 columns frame = new TFrame(title, buttons); frame.pack(); frame.setVisible(true); } public TFrame getFrame() { // just in case return frame; } static public interface TListener { public void buttonPressed(String actionCommand); } public void addListener(TListener listener) { this.listeners.add(listener); } public void fireButtonPressed(String actionCommand) { for (Iterator i = listeners.iterator(); i.hasNext();) { TListener listener = (TListener)i.next(); listener.buttonPressed(actionCommand); } } // Modify some JTextArea methods and provide some frame-like ones. public void setText(String text) { super.setText(text); frame.validate(); } public void append(String text) { super.append(text); frame.validate(); } public void setVisible(boolean b) { frame.setVisible(b); } // Our frame class class TFrame extends JFrame { Container contentPane; TFrame(String title, String[] buttons) { super(title); // setSize(400, 100); contentPane = getContentPane(); // default is equiv to setLayout(new BorderLayout()); contentPane.add(new JScrollPane(TextAreaFrame.this), BorderLayout.CENTER); if (buttons.length > 0) contentPane.add(makeButtonPanel(buttons), BorderLayout.SOUTH); } protected JPanel makeButtonPanel(String[] buttons) { JPanel panel = new JPanel(); // Stick with default flowlayout for now. for (int i = 0; i < buttons.length; i++) { String actionCommand = buttons[i]; panel.add(makeButton(actionCommand)); } return panel; } protected JButton makeButton(String command) { JButton b = new JButton(command); b.addActionListener(buttonListener); return b; } protected ActionListener buttonListener = new ActionListener() { public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); TextAreaFrame.this.fireButtonPressed(command); if (command.equals("Close")) { TFrame.this.setVisible(false); } } }; } } // Issues: // * Consider moving this class to package ix.iface.util.