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

  1. /*
  2. * @(#)WindowCommlet.java    1.0 95/11/09 Ulrich Gall & Jan Kautz
  3. *
  4. * Copyright (c) 1996 Ulrich Gall & Jan Kautz 
  5. * uhgall@cip.informatik.uni-erlangen.de
  6. * Hofmannstr. 48, D-91052 Erlangen, Germany, Fax: +49-9131-201358
  7. *
  8. * Permission to use, copy, and distribute this software
  9. * and its documentation for NON-COMMERCIAL purposes and without
  10. * fee is hereby granted provided that this copyright notice
  11. * appears in all copies. Please contact us for  further copyright 
  12. * and licensing information.
  13. *
  14. * WE MAKE NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  15. * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  16. * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. WE SHALL NOT BE LIABLE FOR
  18. * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  19. * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  20. */
  21. package como.commlet;
  22.  
  23. import java.awt.*;
  24. import java.util.Date;
  25. import como.sys.*;
  26. import como.util.*;
  27.  
  28. /**
  29.  * Any Commlets that might be used within other commlets should be derived
  30.  * from WindowCommlet. This is a Frame that implements Commlet.
  31.  * 
  32.  * Also, it can be put into a MetaCommlet. 
  33.  */
  34. public class WindowCommlet extends Frame implements Commlet 
  35. {
  36.     protected ComObj com;
  37.     private MetaCommlet metacommlet = null;
  38.  
  39.     /**
  40.     * To make it possible to set a topic, simply place this TextField anywhere in this Frame.
  41.     * If WindowCommlet.handleEvent receives an Action from it, it sets a new topic. If a new topiv
  42.     * is set, WindowCommlet.handleMsg() automatically updates the TextField.
  43.     */
  44.     private  TextField topicTF;
  45.  
  46.     public String getSessionName()
  47.     {
  48.         return(getCommletName() + " started on " + (new Date()).toString());
  49.     }
  50.  
  51.     public String getCommletName()
  52.     {
  53.         return("Base class Windowcommlet");
  54.     }
  55.  
  56.     public String getCommletInfo()
  57.     {
  58.         return("Part of the native commlets, V 1.0, Jan Kautz & Ulrich Gall");
  59.     }
  60.  
  61.     /**
  62.      * Returns a TextField where the user may type in the topic. If an action
  63.      * occurs within it, the text is taken over as a new topic and sent to the other
  64.      * commlet sessions, which then upate the contents of their topic TextField.
  65.      */
  66.     protected TextField getTopicTextField() {
  67.         return topicTF;
  68.         }
  69.  
  70.     /**
  71.      * This also takes care of a the menu bar of its MetaCommlet, if it exists.
  72.      * If it's not a metacommlet calls stop(). Else it only hides the frame.
  73.      */
  74.     public void hideCommlet()
  75.     {
  76.         hide();
  77.         if (metacommlet != null)
  78.         {
  79.             metacommlet.setStateOfMenuItemBelongingTo(this,false);
  80.         }
  81.         else stop();
  82.     }
  83.  
  84.     /**
  85.      * Shows the commlet.
  86.      * This also takes care of a the menu bar of its MetaCommlet, if it exists.
  87.      */
  88.     public void showCommlet()
  89.     {
  90.         show();
  91.         if (metacommlet != null)
  92.         {
  93.             metacommlet.setStateOfMenuItemBelongingTo(this,true);
  94.         }
  95.     }
  96.  
  97.     public void setMetaCommlet(MetaCommlet m)
  98.     {
  99.         metacommlet = m;
  100.     }
  101.  
  102.     public void setCom(ComObj c)
  103.     {
  104.         com = c;
  105.     }
  106.  
  107.     public void init()
  108.     {
  109.         setTitle(getSessionName());    
  110.         topicTF = new TextField("No topic set");
  111.     }
  112.  
  113.     /**
  114.      * This destroys the local Commlet session. Automatically
  115.      * called. If you ovveride this, then you have to call
  116.      * super.stop() at the end of your method!
  117.      */
  118.     public void stop()
  119.     {
  120.         if (metacommlet == null) {
  121.             com.logout();
  122.         }
  123.         dispose();
  124.     }
  125.  
  126.     /**
  127.      * Override this for new behaviour!
  128.      */
  129.     public boolean isUserAdmitted(User u)
  130.     {
  131.         return true;
  132.     }
  133.  
  134.     /**
  135.      * If a user leaves, this method will be called.
  136.      * @param who the id of the user.
  137.      */
  138.     public void userLeft(int who) {}
  139.  
  140.     /**
  141.      * If a user comes, this method will be called.
  142.      * @param who the id of the user.
  143.      */
  144.     public void addUser(int who) {}
  145.  
  146.     /**
  147.      * This is a default handleMsg() that calles the helper-methods
  148.      * userLeft() and addUser(). And it takes care of the
  149.      * NEW_TOPIC-message. At the moment you can be sure, that this
  150.      * method is not called more than once at a time. But in future
  151.      * this might change! If you need this behaviour then
  152.      * make it synchronized!
  153.      */
  154.     public boolean handleMsg(Msg msg)
  155.     {
  156.         if (msg.type == Msg.ADD_USER) {
  157.             addUser(((Integer)msg.arg).intValue());
  158.             return true;
  159.         }
  160.         if (msg.type == Msg.USER_LEFT) {
  161.             userLeft(((Integer)msg.arg).intValue());
  162.             return true;
  163.         }
  164.         if (msg.type == Msg.NEW_TOPIC) {
  165.             topicTF.setText(msg.arg.toString());
  166.             return true;
  167.         }
  168.         return false;
  169.     }
  170.  
  171.     /**
  172.      * If you close the window, it will call automatically hide() or
  173.      * dispose the commlet (depending if you are in a metacommlet or not).
  174.      * It also sets the topic (and send a NEW_TOPIC message), if the topic
  175.      * is changed in the topic-TextField.
  176.      */
  177.     public boolean handleEvent(Event evt)
  178.     {
  179.         if (evt.id == Event.WINDOW_DESTROY)
  180.         {
  181.             hideCommlet();
  182.             return true;
  183.         }
  184.         if ((evt.id == Event.ACTION_EVENT) && (evt.target == topicTF)) {
  185.             com.setNewTopic(topicTF.getText());
  186.         }
  187.         return super.handleEvent(evt);
  188.     }
  189. }
  190.