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

  1. /*
  2. * @(#)MetaCommlet.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.util.*;
  24. import java.awt.*;
  25. import java.applet.Applet;
  26. import java.io.*;
  27. import java.awt.*;
  28. import java.util.Date;
  29. import como.sys.*;
  30. import como.util.*;
  31.  
  32. /**
  33.  * This is a WindowCommlet that has a list of commlets. These commlets
  34.  * are all started and added to the menu bar. All messages are send to
  35.  * all commlets in the list.
  36.  *
  37.  * This should be subclassed, so that the commlets can be explicitly added
  38.  * in the init() method after super.init() has been called.
  39.  * Refer to MultiComm for an example.
  40.  */
  41. public abstract class MetaCommlet extends WindowCommlet
  42. {
  43. Vector            myCommlets;     // int -> Commlet
  44. MenuBar           myMenuBar;
  45. Menu              myCommletMenu;
  46. Hashtable         myTargetTable; // CheckBoxMenuItem -> Commlet
  47.  
  48. static String MB_COMMLET = "View";
  49.  
  50. public MetaCommlet()
  51. {
  52.        myCommlets = new Vector();
  53.        myTargetTable = new Hashtable();
  54.        myCommletMenu= new Menu(MB_COMMLET);
  55. }
  56.  
  57. public String getCommletName()
  58. {
  59.        return("Base class MetaCommlet");
  60. }
  61.  
  62. public void init()
  63. {
  64.      super.init();
  65.      myMenuBar = new MenuBar();
  66.      myMenuBar.add(myCommletMenu);
  67.      setMenuBar(myMenuBar);
  68.      resize(300,100);
  69.      pack();
  70. }
  71.  
  72. /**
  73.  * To add a Commlet to your MetaCommlet subclass, call this in your init() method
  74.  * after instantiating the Commlet.
  75.  */
  76. public void addCommlet(Commlet commlet, String name)
  77. {
  78.     commlet.setCom(com);
  79.     commlet.setMetaCommlet(this);
  80.     commlet.init();
  81.     commlet.hideCommlet();
  82.     myCommlets.addElement(commlet);
  83.     MenuItem nm = new CheckboxMenuItem(name);
  84.     myCommletMenu.add(nm);
  85.     myTargetTable.put(nm,commlet);
  86. }
  87.  
  88. protected void setStateOfMenuItemBelongingTo(Commlet c,boolean newstate)
  89. {
  90.               // We need to setstate() the CheckBoxMenuItem for this
  91.               Enumeration e = myTargetTable.keys();
  92.               while (e.hasMoreElements())
  93.               {
  94.                     Object key = e.nextElement();
  95.                     if (c == myTargetTable.get(key))
  96.                     {
  97.                        ((CheckboxMenuItem)key).setState(newstate);
  98.                     }
  99.               }
  100. }
  101.  
  102. public void stop()
  103. {                                         
  104.      Enumeration e = myCommlets.elements();
  105.      while (e.hasMoreElements())
  106.      {    
  107.            Object o = e.nextElement();
  108.            if (o instanceof Commlet)
  109.            {
  110.               ((Commlet)o).stop();
  111.            }
  112.      }
  113.      super.stop();
  114. }
  115.  
  116. /**
  117.  * If you override this in your subclass, call this first. It returns true if one of
  118.  * the sub Commlets has handled the Event. A MessageLog does not return true
  119.  * on any events; it just looks at them.
  120.  */
  121. public boolean handleMsg(Msg msg)
  122.    {
  123.    boolean hasbeenhandled = super.handleMsg(msg);
  124.      Enumeration e = myCommlets.elements();
  125.      while (e.hasMoreElements())
  126.            {
  127.            Object c;
  128.            if ((c = e.nextElement()) instanceof Commlet)
  129.               {
  130.               if (((Commlet)c).handleMsg(msg)) hasbeenhandled = true;
  131.               }
  132.            }
  133.    return hasbeenhandled;
  134.    }
  135.  
  136. public boolean handleEvent(Event evt)
  137. {
  138.    if (myTargetTable.containsKey(evt.target))
  139.    {
  140.       CheckboxMenuItem m = (CheckboxMenuItem) evt.target;
  141.       WindowCommlet c = (WindowCommlet) myTargetTable.get(evt.target);
  142.  
  143.       if (m.getState())
  144.          c.showCommlet();
  145.       else
  146.          c.hideCommlet();
  147.  
  148.       return true;
  149.    }
  150.    return super.handleEvent(evt);
  151. }
  152. }
  153.