home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / in4wjcxu / src / como / commlet / chat / chat.java
Encoding:
Java Source  |  1996-08-14  |  3.4 KB  |  156 lines

  1. package como.commlet.chat;
  2.  
  3. import java.awt.*;
  4. import java.util.Hashtable;
  5. import java.io.*;
  6. import java.util.*;
  7.  
  8. import como.sys.*;
  9. import como.util.*;
  10. import como.awt.*;
  11. import como.commlet.*;
  12.  
  13. public class Chat extends WindowCommlet
  14. {
  15.     TextArea dialog;
  16.     TextField input;
  17.     Hashtable usernames;
  18.  
  19.     public Chat() {
  20.         usernames = new Hashtable();
  21.     }
  22.  
  23.     public String getCommletName()
  24.     {
  25.         return "Basic Chat Commlet";
  26.     }
  27.  
  28.     public String getCommletInfo()
  29.     {
  30.         return
  31.         "(c) 1996 Ulrich Gall & Jan Kautz\n"+
  32.         "\n";
  33.     }
  34.  
  35.     public void init()
  36.     {
  37.         // always call super.init() here!!!
  38.         super.init();
  39.  
  40.         input = new TextField();
  41.         setLayout(new BorderLayout());
  42.         add("South",input);
  43.         Panel p = new Panel();
  44.         p.setLayout(new BorderLayout());
  45.         p.add("West",new Label("Current Topic: "));
  46.         p.add("Center",getTopicTextField());
  47.         add("North",p);
  48.         dialog = new TextArea();
  49.         dialog.setEditable(false);
  50.         add("Center",dialog);
  51.         pack();
  52.  
  53.         print(getCommletName());
  54.         print(getCommletInfo());       
  55.         print("Ready, waiting for messages...");
  56.     }
  57.  
  58.     /** new user handling
  59.      */
  60.     public void addUser(int TheNewUser)
  61.     {
  62.         print(com.getUserName(TheNewUser) + " has joined the conversation.");
  63.         usernames.put(new Integer(TheNewUser),com.getUserName(TheNewUser));
  64.     }
  65.  
  66.     /** a userLeft
  67.      */
  68.     public void userLeft(int RankWhoLeft)
  69.     {
  70.         print(com.getUserName(RankWhoLeft) + " has left the conversation.");
  71.     }
  72.  
  73.     /** if you override this, you have to call super.stop() at the end!
  74.      */
  75.     public void stop()
  76.     {
  77.         com.sendToAll(new Msg(Msg.CHAT_DIALOG_STRING,"bye bye..."));
  78.         super.stop();
  79.     }
  80.  
  81.     void print(String s)
  82.     {
  83.         int width = dialog.size().width;
  84.         Font font = dialog.getFont();
  85.         FontMetrics fm = null;
  86.  
  87.         if( font != null ) fm = dialog.getFontMetrics(font);
  88.  
  89.         if( fm != null ) {
  90.             width -= fm.stringWidth( "MMMM" );    // just like that.
  91.  
  92.             StringTokenizer st = new StringTokenizer( s, " " );
  93.             StringBuffer line = new StringBuffer();
  94.     
  95.             while( st.hasMoreTokens() ) {
  96.                 String next = st.nextToken();
  97.  
  98.                 if( fm.stringWidth( line.toString()+next+" " ) < width )
  99.                     line.append( " "+next );
  100.                 else
  101.                 {
  102.                     dialog.insertText(line+"\n",dialog.getText().length());
  103.                     line = new StringBuffer( "   "+next );
  104.                 }
  105.             }
  106.             dialog.insertText(line+"\n",dialog.getText().length());
  107.         }
  108.         else
  109.             dialog.insertText(s+"\n",dialog.getText().length());
  110.  
  111.         // move the textfield down!
  112.         int l = dialog.getText().length()-2;
  113.         dialog.select( l, l );
  114.     }
  115.  
  116.     /** This gets called if a message arrives.
  117.      */
  118.     public boolean handleMsg(Msg msg)
  119.     {
  120.         if (super.handleMsg(msg)) return true;
  121.  
  122.         if (msg.arg == null) msg.arg = "";
  123.         if (msg.type == Msg.NEW_USER_INFO)
  124.         {
  125.             String newname = com.getUserName(msg.from);
  126.             User newuser = com.getUser(msg.from);
  127.             if (!usernames.get(new Integer(msg.from)).equals(newname))
  128.             {
  129.                 print(usernames.get(new Integer(msg.from)) + " is now known as "+newname);
  130.                 usernames.put( new Integer(msg.from), newname );
  131.             }
  132.             if (((User)newuser).containsKey(User.COMMENT)) 
  133.                 if (((User)newuser).get(User.COMMENT).toString().length() < 1)
  134.                     print(newname + "'s new comment: "+((User)newuser).get(User.COMMENT));
  135.             return true;
  136.         }
  137.         if (msg.type == Msg.CHAT_DIALOG_STRING)
  138.         {
  139.             print(com.getUserName(msg.from) + "> " + msg.arg);
  140.             return true;
  141.         }
  142.         return false;
  143.     }
  144.  
  145.     public boolean action(Event evt,Object what)
  146.     {
  147.         if (evt.target == input)
  148.         {
  149.             com.sendToAll(new Msg(Msg.CHAT_DIALOG_STRING,input.getText()) );
  150.             ((TextField)evt.target).setText("");
  151.             return true;
  152.         }
  153.         return false;
  154.     }
  155. }
  156.