/* Author: Jeff Dalton * Updated: Sun Dec 2 00:19:01 2001 by Jeff Dalton * Copyright: (c) 2001, AIAI, University of Edinburgh */ package ix.ichat; import javax.swing.*; import java.awt.Container; import java.awt.Component; import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.event.*; import java.util.*; import ix.icore.IXAgent; import ix.iface.util.TranscriptTextArea; import ix.iface.util.Reporting; import ix.iface.util.ComboChoice; import ix.util.*; import ix.util.lisp.*; /** * A frame for chat dialogues. */ public class ChatFrame extends JFrame implements ActionListener { protected Container contentPane = getContentPane(); IXAgent agent; TranscriptPanel transcriptPanel = new TranscriptPanel(); SendPanel sendPanel = new SendPanel(); public ChatFrame(IXAgent agent, String title) { super(title); this.agent = agent; setJMenuBar(makeMenuBar()); contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); contentPane.add(transcriptPanel); contentPane.add(sendPanel); pack(); } /** * Creates the menu bar */ protected JMenuBar makeMenuBar() { JMenuBar bar = new JMenuBar(); JMenu fileMenu = new JMenu("File"); bar.add(fileMenu); if (agent instanceof IChat) // Standalone I-Chat fileMenu.add(makeMenuItem("Exit")); else // Part of another agent fileMenu.add(makeMenuItem("Close")); return bar; } // /\/: Must find a better way to do this rather than have // this method everywhere. protected JMenuItem makeMenuItem(String text) { JMenuItem item = new JMenuItem(text); item.addActionListener(this); return item; } /** * Action interpreter */ public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); Debug.noteln("ChatFrame action:", command); if (command.equals("Close")) { setVisible(false); } else if (command.equals("Exit")) { System.exit(0); } else Debug.noteln("Nothing to do for", command); } public void newMessage(ChatMessage message) { transcriptPanel.newMessage(message); sendPanel.noteSender(message); } class TranscriptPanel extends JPanel { TranscriptTextArea transcript = new TranscriptTextArea(15, 40); TranscriptPanel() { setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); setBorder(BorderFactory.createTitledBorder("Transcript")); transcript.setEditable(false); add(new JScrollPane(transcript)); } public void newMessage(ChatMessage message) { transcript.appendLine(message.getSenderId() + " at " + Reporting.dateString() + ":"); transcript.appendIndentedLines(3, message.getText()); } } class SendPanel extends JPanel implements ActionListener { JTextArea sendText = new JTextArea(3, 40); ComboChoice destinationChoice = new ComboChoice(); SendPanel() { setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); setBorder(BorderFactory.createTitledBorder("Send")); add(new JScrollPane(sendText)); JButton sendButton = new JButton("Send to"); sendButton.addActionListener(this); JPanel sendControls = new JPanel(); sendControls.add(sendButton); sendControls.add(destinationChoice); add(sendControls); } public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); Debug.noteln("I-Chat send action:", command); if (command.equals("Send to")) { sendTo(destinationChoice.getSelectedString()); } else Debug.noteln("Nothing to do for", command); } void sendTo(String destination) { Debug.noteln("Sending chat message to", destination); String text = sendText.getText(); String sender = (String)agent.getAgentIPCName(); ChatMessage message = new ChatMessage(text, sender); try { IPC.sendObject(destination, message); ChatFrame.this.transcriptPanel.newMessage(message); sendText.setText(""); } catch (Exception e) { Debug.noteln("Exception while sending to", destination); Debug.noteException(e); JOptionPane.showMessageDialog(ChatFrame.this, new Object[] {"Problem sending chat message", e.getMessage()}, "Problem sending chat message", JOptionPane.ERROR_MESSAGE); } } void noteSender(ChatMessage message) { // This method lets us remember anyone who sent to us. String sender = message.getSenderId(); if (!destinationChoice.hasItem(sender)) { Debug.noteln("New chat participant", sender); destinationChoice.addItem(sender); } } } }