import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.applet.*; import javax.swing.*; /** * TalkApplet - Provides the necessary code to display the program in * an applet. * * @author Paul Kronenwetter * @author Jayme Manning * *

* Paul Kronenwetter
* Jayme Manning
* Trade Secrets and/or Confidential
* Commercial or Financial Information
* Exempt From Disclosure Under the Freedom
* of Information Act (5 USC 552(b)(3)
* and 5 USC 552(b)(4)) and Under 18 USC 1905
*

*

* Proprietary Information Notice
* * This document contains information proprietary to, and is the * sole property of, Paul Kronenwetter and Jayme Manning. It shall not * be reproduced, used or disclosed in any manner or for any purpose * not authorized in writing by both Paul Kronenwetter and * Jayme Manning, and except, as retention may be so authorized, it * shall be returned to Paul Kronenwetter or Jayme Manning upon * request.

*

© 1998 by Paul Kronenwetter and Jayme * Manning
All Rights Reserved

* * @version * $Revision: 1.10 $
* $Date: 1998/12/18 02:30:26 $

* *

 * $Log: TalkApplet.java,v $
 * Revision 1.10  1998/12/18 02:30:26  cps606
 * * Fixed a commenting out oops..
 *
 * Revision 1.9  1998/12/18 02:23:42  cps606
 * * Grafted the ClientNetwork code.
 * * Added action listener for the UserListPanel.
 *
 * Revision 1.8  1998/12/17 17:15:31  cps606
 * Debug methods in the event handler.
 *
 * Revision 1.7  1998/12/17 15:56:46  cps606
 * Added code to implement button listeners on TextEntry Pane.
 *
 * Revision 1.6  1998/12/15 22:56:04  cps606
 * * Manually edited the comments to correct formatting.
 *
 * Revision 1.5  1998/12/15 05:07:29  cps606
 * * Added JMenuItem - Login.
 * * Added JFrame - dialogRoot.
 * * Modified ActionHandler - Added support for JButtons in the
 *   LoginDialog.
 * * Modified ActionHandler - Now discern between object classes.
 *
 * Revision 1.4  1998/12/15 00:08:00  cps606
 * * Cleaned up and added documentation.  Rearranged methods a little
 *   as seemed appropriate. 
 *
 * Revision 1.3  1998/12/13 21:39:30  cps606
 * * Modified to remove panel duplications, adjust layout
 *   performance.
 *
 * Revision 1.2  1998/12/13 20:10:37  cps606
 * * Corrected behavior of menu.
 *
 * Revision 1.1  1998/12/12 03:22:12  cps606
 * * First CVS checkin.
 *
 * 
*/ public class TalkApplet extends JApplet { private final String rcsId="$Id: TalkApplet.java,v 1.10 1998/12/18 02:30:26 cps606 Exp $"; private JLabel myLabel; private JPanel southPanel,eastPanel,westPanel; private JButton exitButton,resetButton; private JButton allUsersButton,highlightedUsersButton; private JButton thisMonthButton; private ActionHandler myActionHandler; private JMenuBar menuBar; private JMenu fileMenu; private JMenuItem exitMenuItem; private JMenuItem loginMenuItem; private JMenuItem sendMenuItem; private UserListPane userListPane; private TextEntryPane textEntryPane; private MessagesPane messagesPane; private Container fooContainer; private JFrame dialogRoot; private LoginDialog loginDialog; private String userName; private String machineName; private String portNumber; private final String button1Text="All Users"; private final String button2Text="Highlighted"; private final String exitText="Exit"; private final String fileText="File"; private NetworkClient foo; /** * Constructor creates an ActionHandler to deal with button * presses. */ public TalkApplet() { myActionHandler=new ActionHandler(); } /** * Initialization method. Populates the panels and buttons. */ public void init() { // Force SwingApplet to come up in the System L&F String laf = UIManager.getSystemLookAndFeelClassName(); try { UIManager.setLookAndFeel(laf); } catch (UnsupportedLookAndFeelException exc) { System.err.println("Warning: UnsupportedLookAndFeel: " + laf); } catch (Exception exc) { System.err.println("Error loading " + laf + ": " + exc); } // Create the border panels and use the default layout manager (Flow) southPanel = new MessagesPane(); eastPanel = new UserListPane(); westPanel = new TextEntryPane(); // Create Frame dialogRoot = new JFrame(); loginDialog = new LoginDialog(dialogRoot); // Add and populate the fileMenu menuBar = new JMenuBar(); fileMenu = new JMenu(fileText); exitMenuItem = new JMenuItem(exitText); loginMenuItem = new JMenuItem("Login"); sendMenuItem = new JMenuItem("Send User List"); fileMenu.add(loginMenuItem); fileMenu.addSeparator(); fileMenu.add(sendMenuItem); fileMenu.addSeparator(); fileMenu.add(exitMenuItem); menuBar.add(fileMenu); // Add the menu area to the applet. setJMenuBar(menuBar); // Allow the exit item on the menu to work. exitMenuItem.addActionListener(myActionHandler); // Allow the login item on the menu to work. loginMenuItem.addActionListener(myActionHandler); // Allow the send item on the menu to work. sendMenuItem.addActionListener(myActionHandler); // Register the loginDialog box with the action listener loginDialog.addActionListener(myActionHandler); // Register the textEntryPane buttons with the action listener ((TextEntryPane)westPanel).addActionListener(myActionHandler); // Register the userListPane buttons with the action listener ((UserListPane)eastPanel).addActionListener(myActionHandler); // Add the panels to the content frame. getContentPane().setLayout(new BorderLayout(2,2)); getContentPane().add(southPanel,BorderLayout.SOUTH); getContentPane().add(eastPanel,BorderLayout.EAST); getContentPane().add(westPanel,BorderLayout.WEST); } /** * NetworkClient - Class used to connect the applet to the ServerProcess, * and work in conjunction with the event handlers to process messages * and display as appropriate. */ public class NetworkClient extends Thread { String userName; Socket clientSocket; BufferedReader inReader; PrintWriter outWriter; /** * NetworkClient - General constructor used to initiate contact * between this client, and an available chat server. * * @param machine The name of the machine the server is running on. * @param port The port number of the machine the server is running on. * @param user The name of the user that is connecting to the server. */ public NetworkClient(String machine,int port,String user) { try { // Attach to the server clientSocket = new Socket(machine,port); userName = new String(user); outWriter = new PrintWriter( clientSocket.getOutputStream()); inReader = new BufferedReader(new InputStreamReader( clientSocket.getInputStream())); // Report who we are sendMessage(userName); } catch(IOException ioe) { System.out.println("Error openening socket."); } } /** * run - This method overides the default method from class Thread. * The purpose of this method is to run the main event loop, and * process messages received from the server; as well as sending * messages to the server. */ public void run() { String temp; // Main event loop try { while((temp = inReader.readLine()) != null) { processMessage(temp); } } catch(IOException ioe1) { System.out.println("Error Reading From Server"); } } /** * sendMessage - Method used to send a message to an Individual * client. This is called by the enclosing class in order * to send a message to the client. * * @param message String containing the message for the client. */ public void sendMessage(String message) { outWriter.println(message); outWriter.flush(); } } /** * processMessage - Method used to process messages recieved from the * chat server. * * @param message The string that needs to be interpreted. */ public void processMessage(String message) { if(message.startsWith("COMMAND: CLEARUSERS")) { ((UserListPane)eastPanel).removeAllUsers(); } else if(message.startsWith("COMMAND: RMUSER:")) { String t1 = message.substring(17); ((UserListPane)eastPanel).removeUser(t1); } else if(message.startsWith("COMMAND: USER:")) { String t1 = message.substring(15); ((UserListPane)eastPanel).addUser(t1); } else if(message.startsWith("MESSAGE:")) { String t1 = message.substring(9); ((MessagesPane)southPanel).addMessage( GenTextPanel.genBroadcastMessage(t1)); } else if(message.startsWith("MESSAGETO:")) { String t1 = message.substring(11); ((MessagesPane)southPanel).addMessage( GenTextPanel.genDirectMessage(t1)); } } /** * Inner class to handle button pushes. */ public class ActionHandler implements ActionListener { /** * Figures out what happened and what should be done about it. */ public void actionPerformed (ActionEvent e) { Object self = e.getSource(); String s = e.getActionCommand(); if(self instanceof JMenuItem) { if ((((JMenuItem)self).getText()).equals(exitText)) { System.exit(0); } else if ((((JMenuItem)self).getText()).equals("Login")) { loginDialog.setVisible(true); } else if ((((JMenuItem)self).getText()).equals("Send User List")) { if(userName == null) { loginDialog.resetFields(); loginDialog.setVisible(true); } else { foo.sendMessage("COMMAND: SENDUSERS"); } } } else if(self instanceof JButton) { if ((((JButton)self).getText()).equals("Submit")) { if(loginDialog.isAvailable()) { int port; loginDialog.setVisible(false); String[] x = loginDialog.getFields(); userName = x[0]; machineName = x[1]; portNumber = x[2]; port = Integer.valueOf(portNumber).intValue(); foo = new NetworkClient(machineName,port,userName); foo.start(); } } else if ((((JButton)self).getText()).equals("Cancel")) { loginDialog.resetFields(); loginDialog.setVisible(false); } else if ((((JButton)self).getText()).equals("All Users")) { if(userName == null) { loginDialog.resetFields(); loginDialog.setVisible(true); } else { String x = ((TextEntryPane)westPanel).getText(); ((MessagesPane)southPanel).addMessage( GenTextPanel.genUserMessage(x)); foo.sendMessage("MESSAGE: " + x); } } else if ((((JButton)self).getText()).equals("Highlighted")) { if(userName == null) { loginDialog.resetFields(); loginDialog.setVisible(true); } else { String x = ((TextEntryPane)westPanel).getText(); if(((UserListPane)eastPanel).isAvailable()) { ((MessagesPane)southPanel).addMessage( GenTextPanel.genUserMessage(x)); String[] z = ((UserListPane)eastPanel). getSelections(); for(int i = 0; i < z.length; i++) { foo.sendMessage("MESSAGETO: " + z[i] + ": " + x); } } } } else if ((((JButton)self).getText()).equals("Sign Off")) { foo.stop(); foo = null; userName = null; } } } } }