home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / afc11 / JNotepad / src / JNoteContextMenu.java < prev    next >
Encoding:
Java Source  |  2000-05-04  |  6.4 KB  |  239 lines

  1. //
  2. // (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  3. //
  4. import com.ms.ui.*;
  5. import com.ms.ui.event.*;
  6. import java.util.*;
  7. import java.awt.*;
  8. import com.ms.fx.*;
  9.  
  10. /**
  11. *    Right click Context menu for JNotepad. Sames as UIContextMenu, except
  12. *    it adds support for loading from resources. 
  13. *    This class works with CommandFeature to send user commands to the right place.
  14. *    Since UIContextMenu (the class from which this class descends) has no support
  15. *    for action listeners, we implement the IActionTalker interface, which provides us
  16. *    with equivalent functionality.
  17. *
  18. *    @version    1.0, 8/4/97
  19. *
  20. *    @see        CommandFeature
  21. *    @see        IActionTalker
  22. */
  23.  
  24. public class JNoteContextMenu extends UIContextMenu implements IActionTalker, ICommandFeedback
  25. {
  26. /**
  27. *    Hashtable that stores objects that are listening to our commands    
  28.     */
  29.     private Hashtable actionListenerTable;
  30.     
  31.     /**
  32.     *    Hashtable that stores the menu items that we hold.
  33.     */
  34.     protected Hashtable menuItemTable;
  35.     
  36.     
  37.     /**
  38.     *    Creates a new JNoteContextMenu.
  39.     *
  40.     *    @param    menulist    UIMenuList which contains the items in the context menu
  41.     */
  42.     public JNoteContextMenu(UIMenuList menulist)
  43.     {
  44.         super(menulist);
  45.         init(menulist);
  46.     }
  47.     
  48.     /**
  49.     *    Creates a new JNoteContextMenu.
  50.     *
  51.     *    @param    menulist    UIMenuList which contains the items in the context menu
  52.     *    @param    comp        The component to be displayed in the control
  53.     */
  54.     public JNoteContextMenu(IUIComponent comp, UIMenuList menulist)
  55.     {
  56.         super(comp, menulist);
  57.         init(menulist);
  58.     }
  59.     
  60.     /**
  61.     *    Private init function. Initializes the actionListenerTable hashtable.
  62.     */
  63.     private void init(UIMenuList menulist)
  64.     {
  65.         actionListenerTable = new Hashtable(5);
  66.         menuItemTable = new Hashtable(5);
  67.         addMenuListToTable(menulist);
  68.     }
  69.     
  70.     /**
  71.     *    Adds the elements in the UIMenuList to a private hashtable. This allows
  72.     *    us to retrieve the elements quickly later and grab them by name. Used
  73.     *    mainly for enabling and disabling menu items.
  74.     *    
  75.     *    @param    comp    Component to add to the menu
  76.     */
  77.     private void addMenuListToTable(UIMenuList ml)
  78.     {
  79.         // iterate through all the items in the UIMenuList and add them
  80.         // to the menuItemTable hash table.
  81.         int iNumItems = ml.getComponentCount();
  82.         for (int i=0; i < iNumItems; ++i)
  83.         {
  84.             IUIComponent comp = ml.getComponent(i);
  85.             
  86.             // we want to add just menu items, not UILine separators
  87.             if (comp instanceof UIMenuItem)
  88.             {
  89.                 menuItemTable.put(comp.getName(), comp);
  90.             }
  91.         }
  92.     }
  93.     
  94.     /**
  95.     *    Adds a Action listener object (one which implements IUIActionListener)
  96.     *    to the ITextOperationTargetExt object.
  97.     *
  98.     *    @param    l    The object which will listen to Action events from this object.
  99.     */
  100.     public void addActionListener(IUIActionListener l)
  101.     {
  102.         // add listener to table. We only care about the listener object, 
  103.         // so we give it a dummy value to put into the table.
  104.         actionListenerTable.put(l, new Object());
  105.     }
  106.     
  107.     /**
  108.     *    Removes the Action listener object from the ITextOperationTargetExt object.
  109.     *
  110.     *    @param    l    The object to remove an Action listener from.
  111.     */
  112.     public void removeActionListener(IUIActionListener l)
  113.     {
  114.         // remove listener from table. Quick and easy with a hashtable.
  115.         if (l != null)
  116.         {
  117.             actionListenerTable.remove(l);
  118.         }
  119.     }
  120.     
  121.     /**
  122.     *    Handles events that happen in the context menu -- selection of commands
  123.     *    by the user. Tells all of the listeners in the actionListenerTable that
  124.     *    an event has occurred.
  125.     *    
  126.     *    @param    evt    Action Event that occurred.
  127.     *    @param    what    Object it occurred to (i.e. the UIMenuItem that was selected)
  128.     *    @returns    true if the event was handled, false if not
  129.     */
  130.     public boolean action(Event evt, Object what)
  131.     {
  132.         
  133.         if (!(what instanceof IUIComponent))
  134.         {
  135.             return false;
  136.         }
  137.         
  138.         // grab all the items in the table, go through them, and call the
  139.         // received an action event method in the listener object.
  140.         // We adhere to the listener method names and interface for maximum
  141.         // compatibility.
  142.  
  143.         Enumeration e = actionListenerTable.keys();
  144.         while (e.hasMoreElements())
  145.         {
  146.             IUIActionListener l = (IUIActionListener)e.nextElement();
  147.             
  148.             // we call the actionPerformed method directly, rather than implictly
  149.             // via a listener.
  150.             l.actionPerformed(new UIActionEvent(this, UIActionEvent.ACTION_PERFORMED,
  151.                 (IUIComponent)what, ((UIComponent)what).getName()));
  152.         }
  153.         
  154.         return true;
  155.     }
  156.     
  157.     /**
  158.     *    Called when a menu item needs to be enabled    or disabled. Finds the 
  159.     *    item and changes its state. Part of the ICommandFeedback interface. 
  160.     *
  161.     *    @param    command    Name of menu item to enable or disable
  162.     *    @param    on    true if menu item is to be enabled, false if not
  163.     */
  164.     public void enableCommand(String command, boolean on)
  165.     {
  166.         Object m = menuItemTable.get(command);
  167.         
  168.         if (m != null)
  169.         {
  170.             ((UIComponent)m).setEnabled(on);
  171.         }
  172.     }
  173.     
  174.     
  175.     /**
  176.     *    Queries the enabled state of a menu item. Part of the ICommandFeedback interface.
  177.     *
  178.     *    @param    command    Name of menu item to check
  179.     *    @returns    true if menu item is enabled, false if it is not
  180.     */
  181.     public boolean isEnabled(String command)
  182.     {
  183.         Object m = menuItemTable.get(command);
  184.         
  185.         if (m != null)
  186.         {
  187.             return ((UIComponent)m).isEnabled();
  188.         }
  189.         else
  190.         {
  191.             return true;
  192.         }
  193.     }
  194.     
  195.     /**
  196.     *    Sets a checkbutton to be checked or unchecked. Part of the ICommandFeedback interface.
  197.     *    
  198.     *    @param    command    Name of menu item to check or uncheck
  199.     *    @param    on    true if button is to be checked, false if it is to be unchecked
  200.     */
  201.     public void setChecked(String command, boolean on)
  202.     {
  203.         Object m = menuItemTable.get(command);
  204.         
  205.         if ((m != null) && (m instanceof UICheckButton))
  206.         {
  207.             ((UICheckButton)m).setChecked(on);
  208.         }
  209.     }
  210.     
  211.     /**
  212.     *    Adds a filename to the Most Recently Used (MRU) submenu. Since this
  213.     *    class doesn't maintain an MRU list, this does nothing.
  214.     *
  215.     *    @param    filename    Name of the file to add to the MRU list.
  216.     */
  217.     public void addToMRUList(String filename)
  218.     {
  219.         // do nothing since we don't keep an MRU list
  220.     }
  221.     
  222.     /**
  223.     *    Called when a control in the system changes color. We don't care about
  224.     *    color changes, so we do nothing.
  225.     *
  226.     *    @param    comp    Component who had a color change
  227.     *    @param    color    Color that the component was changed to
  228.     *    @param    whichPart    Which part of the component had a color change. This can
  229.     *                        be either FOREGROUND or BACKGROUND.
  230.     */        
  231.     public void    notifyColorChange(Object comp, FxColor color, int whichPart)
  232.     {
  233.         // do nothing
  234.     }
  235.     
  236. }
  237.  
  238.  
  239.