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

  1.  
  2. package como.commlet.userlist;
  3.  
  4. import java.awt.*;
  5. import java.util.*;
  6. import como.util.*;
  7. import como.awt.*;
  8. import como.sys.*;
  9. import como.commlet.*;
  10.  
  11. public class UserList extends WindowCommlet
  12.  
  13. {
  14.  
  15. UserPanel userpanel = null;
  16. Button kickbutton = null;
  17. List userlist = null;
  18. Hashtable users = null; // String listentry -> Integer id
  19. int idshown = -1;
  20. Image map;
  21.  
  22.     public UserList()
  23.     {
  24.     }
  25.  
  26.     public String getCommletName()
  27.     {
  28.         return("UserList");
  29.     }
  30.  
  31.     public String getCommletInfo()
  32.     {
  33.         return("UserList, V 1.0, Jan Kautz & Ulrich Gall");
  34.     }
  35.  
  36.     public void init()
  37.     {
  38.         super.init();
  39.         map = com.loadImage("worldmap");
  40.         if (map == null) new SmartFrame("UserList was unable to load a map image");
  41.         userpanel = null ;
  42.         userlist = new List();
  43.         setLayout( new BorderLayout() );
  44.  
  45.         kickbutton = new Button( "Kick User" );
  46.         if( !com.iAmMaster() )
  47.             kickbutton.disable();
  48.  
  49.         add( "West", userlist );
  50.         // add( "South", kickbutton );
  51.  
  52.         idshown = com.getMyID();
  53.         updateList();
  54.         updateUserPanel();
  55.         resize(400,400);    
  56.     }
  57.     
  58.     public boolean action(Event evt,Object arg) {
  59.         if (evt.arg instanceof User)
  60.             { 
  61.             User user = (User)evt.arg;
  62.             if (user.containsKey(User.ID))
  63.                 {
  64.                 if (((Integer)user.get(User.ID)).intValue() == com.getMyID())
  65.                     com.setLocalUser(user);
  66.                 }
  67.             return true;
  68.             }    
  69.         return false;
  70.         }
  71.  
  72.     public boolean handleEvent(Event evt) {
  73.         if (super.handleEvent(evt)) return true;
  74.  
  75.         if (evt.target == userlist) {
  76.             String sel = userlist.getSelectedItem();
  77.  
  78.             if (sel != null) {
  79.                 idshown = ((Integer)users.get(sel)).intValue();
  80.             }
  81.  
  82.             updateUserPanel();
  83.             return true;
  84.         }
  85.  
  86.         if( evt.target == kickbutton ) {
  87.             // TODO: do sth.
  88.         }
  89.  
  90.         return false;
  91.     }
  92.  
  93.     private void updateList() {
  94.         // Known Bug under Win 95: userlist.clear(); does not work correct!
  95.  
  96.         userlist.delItems( 0, userlist.countItems()-1 );
  97.  
  98.         Vector getusers = com.getUsers();
  99.         users = new Hashtable();
  100.         Enumeration userenum = getusers.elements();
  101.  
  102.         while (userenum.hasMoreElements()) {
  103.             String name;
  104.             User u = (User)userenum.nextElement();
  105.             name = u.getName()+" (#"+u.getID()+")";
  106.             userlist.addItem(name);
  107.             if (u.getID() == idshown) userlist.select(userlist.countItems()-1);
  108.             users.put(name,u.get(u.ID));
  109.         }
  110.     }
  111.  
  112.     private void updateUserPanel() {
  113.         if (idshown >= 0) 
  114.         {
  115.             String ok;
  116.             if (idshown == com.getMyID()) ok = "Send Update" ; else ok = "";
  117.  
  118.             // remove the old user-panel!
  119.             if( userpanel != null )
  120.                 remove( userpanel );
  121.  
  122.             userpanel = new UserPanel(com.getUser(idshown),ok,map);
  123.             add("Center",userpanel);
  124.             paintAll(getGraphics());
  125.         }
  126.         else {
  127.             userpanel = null;
  128.         }
  129.  
  130.     }
  131.  
  132.     public boolean handleMsg(Msg msg)
  133.     {
  134. /* Thought it is not a good idea to let every UserList kick people.
  135.         if (msg.type == Msg.NEW_MASTER) {
  136.             if( !com.iAmMaster() )
  137.                 kickbutton.disable();
  138.             else
  139.                 kickbutton.enable();
  140.         }
  141. */
  142.         if (msg.type == Msg.NEW_USER_INFO) {
  143.             if (idshown == ((User)msg.arg).getID()) updateUserPanel();
  144.             updateList();
  145.         }
  146.         else if (msg.type == Msg.USER_LEFT) {
  147.             if (idshown == ((Integer)msg.arg).intValue()) {
  148.                 idshown = com.getMyID();
  149.                 updateUserPanel();
  150.             }
  151.             updateList();
  152.         }
  153.         else if (msg.type == Msg.ADD_USER) {
  154.             updateList();
  155.         }
  156.         return false; // since this "handling" does not really count
  157.     }
  158. }
  159.