////////////////////////////////////////////////////////////////////// // TalkFrame.java // // --Stand alone GUI for communication on SPON-- // // // // Supervised Peer-to-peer Overlay Network (SPON) Simulator // // // // // // Computer Science M.S.E. Project // // Programmed by: Joey Chau // // Copyright: February, 2003 // // // // Research Advisor: Christian Scheideler // // Computer Science // // Johns Hopkins University // ////////////////////////////////////////////////////////////////////// import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.text.*; import java.util.*; /** TalkFrame provides a basic graphical communication interface to be used by the SPON Network to simulate dialog between machines. */ class TalkFrame extends JFrame { public static final int WIDTH = 400; public static final int HEIGHT = 320; private JTextField myField; private JTextPane outArea; private JButton joinButton; private JButton leaveButton; private String inputStuff; private boolean gtg; private boolean closeNow; private boolean joinNow; private boolean leaveNow; private JFrame joinFrame; private String joinName; private String joinDest; private JoinFrame joinF; /** Creation of communication frame consisting of chat dialog, dialog display, and join and leave buttons. */ public TalkFrame() { setTitle("TextTest"); setSize(WIDTH, HEIGHT); Container contentPane = getContentPane(); ActionListener actListener = new TextActionListener(); ActionListener joinListener = new JoinActionListener(); ActionListener leaveListener = new LeaveActionListener(); WindowListener winListener = new CloseWindowListener(); JPanel bpanel = new JPanel(); JPanel fpanel = new JPanel(); JPanel apanel = new JPanel(); joinButton = new JButton( "JOIN" ); leaveButton = new JButton( "LEAVE" ); leaveButton.setEnabled(false); myField = new JTextField( 35 ); outArea = new JTextPane(); outArea.setEditable(false); JScrollPane scrollPane = new JScrollPane(outArea); scrollPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS ); scrollPane.setPreferredSize( new Dimension(380,200) ); fpanel.add(scrollPane); apanel.add(myField); bpanel.add( joinButton, BorderLayout.EAST ); bpanel.add( leaveButton, BorderLayout.WEST ); addWindowListener( winListener ); myField.addActionListener( actListener ); joinButton.addActionListener( joinListener ); leaveButton.addActionListener( leaveListener ); contentPane.add(apanel, BorderLayout.SOUTH); contentPane.add(fpanel, BorderLayout.CENTER); contentPane.add(bpanel, BorderLayout.NORTH); inputStuff = new String(); gtg = false; closeNow = false; joinNow = false; leaveNow = false; } /** Diagnositc printout function. */ private void writeOut( String tString ) { System.out.println( tString + ":" + myField.getText() ); } /** Helper function used to extract dialog text and mark the frame as having text to send. */ private void submit( ) { inputStuff = myField.getText(); System.out.println( "Submitting:" + inputStuff ); myField.setText( "" ); gtg = true; } /** Helper function used to mark the frame as wanting to exit. */ private void closeMe() { closeNow = true; System.out.println( "Closing" ); } /** operation to determine whether frame is ready to be closed. @return boolean - true if window is ready to close */ public boolean close() { return closeNow; } /** updates the text dialog with new dialog being communicated and also sets the proper color anmd text properties. @param incoming - String of dialog */ public void update( String incoming ) { StringTokenizer tok = new StringTokenizer( incoming,":" ); String sTitle = tok.nextToken(); String sMessage = new String(); while( tok.hasMoreTokens() ) { sMessage = sMessage + tok.nextToken(); } if( sTitle.equals( "[SUPERVISOR]" ) ) { try { Style StyBack = StyleContext.getDefaultStyleContext(). getStyle( StyleContext.DEFAULT_STYLE ); StyleConstants.setForeground(StyBack, Color.GREEN ); StyleConstants.setBackground(StyBack, Color.BLACK ); StyleConstants.setBold( StyBack, true ); StyleConstants.setItalic( StyBack, false ); Document doc = outArea.getDocument(); doc.insertString( doc.getLength(), sTitle+":", StyBack ); Style StyMess = StyleContext.getDefaultStyleContext(). getStyle( StyleContext.DEFAULT_STYLE ); StyleConstants.setForeground(StyMess, Color.WHITE ); doc = outArea.getDocument(); doc.insertString( doc.getLength(), sMessage+"\n", StyMess ); } catch( BadLocationException e ) { // No text printed } } else { try { Style StyRed = StyleContext.getDefaultStyleContext(). getStyle( StyleContext.DEFAULT_STYLE ); if( sTitle.equals( joinName ) ) { StyleConstants.setForeground(StyRed, Color.BLUE ); } else { StyleConstants.setForeground(StyRed, Color.RED ); } StyleConstants.setBackground(StyRed, Color.WHITE ); StyleConstants.setBold( StyRed, false ); StyleConstants.setItalic( StyRed, false ); Document doc = outArea.getDocument(); doc.insertString( doc.getLength(), sTitle+": ", StyRed ); Style StyBlack = StyleContext.getDefaultStyleContext(). getStyle( StyleContext.DEFAULT_STYLE ); StyleConstants.setForeground(StyBlack, Color.BLACK ); StyleConstants.setItalic(StyBlack, true ); doc = outArea.getDocument(); doc.insertString( doc.getLength(), sMessage+"\n", StyBlack ); } catch( BadLocationException e ) { // No text printed } } } /** operation to determine whether new dialog input is available from the user. @return boolean - true if frame has new dialog available */ public boolean ready() { return gtg; } /** operation to determine if the user is ready to join a communication group. @return boolean - true if frame has is ready to join communication group */ public boolean readyToJoin() { return joinNow; } /** operation to determin if the user is ready to leave a communication group. @return boolean - true if the frame is ready to leave a communication group */ public boolean readyToLeave() { return leaveNow; } /** function which resets the state of the frame after the calling state has exited communication. */ public void leaveMe() { if( leaveNow ) { leaveNow = false; joinButton.setEnabled( true ); leaveButton.setEnabled( false ); } } /** method supplying the caller with a String array consisting of first the user name, and then the connecting destination. @return String[] - a size 2 array consisting of {user name, communication destination} */ public String[] getJoinInfo() { if( joinNow ) { String[] returnString = {joinName, joinDest}; joinNow = false; return returnString; } return null; } /** access to the dialog entered by the user in the input text field. @return String - a String containing the user dialog input */ public String getInfo() { if( gtg ) { gtg = false; return inputStuff; } return null; } /** The Action listener used to listen for user dialog input */ private class TextActionListener implements ActionListener { public void actionPerformed( ActionEvent e ) { submit(); } } /** The Window listener used to listen to when the user wishes to exit the Communication. */ private class CloseWindowListener implements WindowListener { public void windowClosing( WindowEvent e ) {closeMe();} public void windowClosed( WindowEvent e ) {} public void windowOpened( WindowEvent e ) {} public void windowIconified( WindowEvent e ) {} public void windowDeiconified( WindowEvent e ) {} public void windowActivated( WindowEvent e ) {} public void windowDeactivated( WindowEvent e ) {} } /** The Window listener used to listen to when the user has finished submitting join information. */ private class JoinCloseListener implements WindowListener { public void windowClosing( WindowEvent e ) { System.out.println( "connecting to "+joinF.destString+" as "+joinF.nameString ); } public void windowClosed( WindowEvent e ) { System.out.println( "connecting to "+joinF.destString+" as "+joinF.nameString ); if( !joinF.destString.equals("") && !joinF.nameString.equals("") ) { joinName = joinF.nameString; joinDest = joinF.destString; joinNow = true; joinButton.setEnabled(false); leaveButton.setEnabled(true); } } public void windowOpened( WindowEvent e ) {} public void windowIconified( WindowEvent e ) {} public void windowDeiconified( WindowEvent e ) {} public void windowActivated( WindowEvent e ) {} public void windowDeactivated( WindowEvent e ) { } } /** The Action listener used to listen to when the Join button has been activated. */ private class JoinActionListener implements ActionListener { public void actionPerformed( ActionEvent e ) { joinF = new JoinFrame(); joinF.show(); } } /** The Action listener used to listen to when the LEave button has been activated */ private class LeaveActionListener implements ActionListener { public void actionPerformed( ActionEvent e ) { leaveNow = true; } } /** The frame displayed when the user wishes to join a communication; input prompts for user name and communication destination. */ private class JoinFrame extends JFrame { public static final int WIDTH = 300; public static final int HEIGHT = 200; private JTextField nameField; private JTextField destField; private JLabel nameLabel; private JLabel destLabel; public String nameString; public String destString; public JoinFrame() { setTitle("Join SPON"); setSize(WIDTH, HEIGHT); Container contentPane = getContentPane(); ActionListener nameListener = new NameActionListener(); ActionListener destListener = new DestActionListener(); WindowListener winListener = new JoinCloseListener(); JPanel namePanel = new JPanel(); JPanel destPanel = new JPanel(); nameField = new JTextField( 20 ); nameField.setEditable( true ); nameField.addActionListener( nameListener ); destField = new JTextField( 20 ); destField.setEditable( false ); destField.addActionListener( destListener ); nameLabel = new JLabel( "User Name:" ); destLabel = new JLabel( "Destination:" ); namePanel.add( nameLabel ); namePanel.add( nameField ); namePanel.add( destLabel ); namePanel.add( destField ); addWindowListener( winListener ); contentPane.add(namePanel, BorderLayout.CENTER); nameField.requestFocus(); } /** Listener which monitors when the user name has been inputed. */ private class NameActionListener implements ActionListener { public void actionPerformed( ActionEvent e ) { nameString = nameField.getText(); nameField.setEditable( false ); destField.setEditable( true ); destField.requestFocus(); } } /** Listener which monitors when the communication destination has been inputed. */ private class DestActionListener implements ActionListener { public void actionPerformed( ActionEvent e ) { destString = destField.getText(); destField.setEditable( false ); dispose(); } } } }