home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 14 / IOPROG_14.ISO / soft / sdkjava / sdkjava.exe / SDKJava.cab / Samples / AFC / JNotepad / src / CommandFeature.java < prev    next >
Encoding:
Java Source  |  1998-03-05  |  6.9 KB  |  266 lines

  1. //
  2. // (c) 1998 Microsoft Corporation.  All rights reserved.
  3. //
  4. import com.ms.ui.*;
  5. import com.ms.ui.event.*;
  6. import com.ms.fx.*;
  7. import java.util.*;
  8.  
  9. /**
  10. *    Application message mapping module. This module intercepts all menu commands, toolbar
  11. *    commands, and accelerator key commands, identifies what command they are in one place,
  12. *    and routes them to the appropriate place. This allows the developer to put all
  13. *    code designed to identify user commands and put it in one spot. No need to duplicate
  14. *    menu item command routing in different parts of the code.
  15. *    <p>
  16. *    This module also implements ICommandFeedback, which allows parts of the app to enable
  17. *    and disable menu items/toolbar buttons. All the items which can send that command
  18. *    are disabled, even if they are on totally different controls. One simple call
  19. *    enables and disables all controls that can send a particular item.
  20. *    <p>
  21. *    This component is implemented as a plugin feature through the IComponentFeature
  22. *    interface. Subclass this and do your command processing in actionPerformed.
  23. *    <p>
  24. *    @see    IFeature
  25. *    @see    ICommandFeedback
  26. *    @see    IComponentFeature
  27. *
  28. *    @version    1.0, 8/6/97
  29. */
  30.  
  31. public abstract class CommandFeature implements IComponentFeature, ICommandFeedback, IUIActionListener
  32. {
  33.     /**
  34.     *    An object which handles text-related commands    
  35.     */
  36.     ITextOperationTarget textTarget;
  37.                 
  38.     /**
  39.     *    An object which handles file-related commands
  40.     */
  41.     IFileOperationTarget fileTarget;
  42.     
  43.     /**
  44.     *    Vector of components which generate commands
  45.     */
  46.     Vector talkerVector;
  47.     
  48.     
  49.     /**
  50.     *    Creates a new ComponentFeature.
  51.     */
  52.     public CommandFeature()
  53.     {
  54.         talkerVector = new Vector(4);
  55.     }
  56.     
  57.     /**
  58.     *    Public init function. Part of the IFeature interface. Sets the objects
  59.     *    which will receive the commands processed.
  60.     *
  61.     *    @param    tTarget    object to receive text-related commands
  62.     *    @param    fTarget    object to receive file-related commands
  63.     */
  64.     public void init(ITextOperationTarget tTarget, IFileOperationTarget fTarget)
  65.     {
  66.         textTarget = tTarget;
  67.         fileTarget = fTarget;                
  68.     }
  69.                 
  70.     /**
  71.     *    Adds a component to the list of objects that are listened to for
  72.     *    command events. These objects are referred to as "talkers".
  73.     *
  74.     *    @param    talker    Object to listen to for command events.
  75.     */
  76.     public void addTalker(IUIComponent talker)
  77.     {
  78.         if (talker != null)
  79.         {
  80.             talkerVector.addElement(talker);
  81.         }
  82.     }
  83.     
  84.     /**
  85.     *    Called to activate the feature.
  86.     *    Registers the feature as active and attaches listeners to the 
  87.     *    components which have been added as talkers. Part of the IFeature interface. 
  88.     *    Once this class has been registered, it is listening to the commands
  89.     *    performed on all of the elements in talkerVector.
  90.     */
  91.     public void register()
  92.     {
  93.         Enumeration enum = talkerVector.elements();
  94.         
  95.         while (enum.hasMoreElements())
  96.         {
  97.             Object comp = enum.nextElement();
  98.             
  99.             if (comp instanceof IUISelector)
  100.             {
  101.                 ((IUISelector)comp).addActionListener(this);
  102.             }
  103.             else if (comp instanceof IActionTalker)
  104.             {
  105.                 ((IActionTalker)comp).addActionListener(this);
  106.             }
  107.         }
  108.     }
  109.     
  110.     /**
  111.     *    Called when the feature is no longer
  112.     *    active on the current objects. Removes listeners from the talkers. 
  113.     *    Part of the IFeature interface. 
  114.     */
  115.     public void unregister()
  116.     {
  117.         Enumeration enum = talkerVector.elements();
  118.         
  119.         while (enum.hasMoreElements())
  120.         {
  121.             Object comp = (Object)enum.nextElement();
  122.             
  123.             if (comp instanceof IUISelector)
  124.             {
  125.                 ((IUISelector)comp).removeActionListener(this);
  126.             }
  127.             else if (comp instanceof IActionTalker)
  128.             {
  129.                 ((IActionTalker)comp).removeActionListener(this);
  130.             }
  131.         }
  132.     }
  133.     
  134.     /**
  135.     *    Called to reset the state of the 
  136.     *    feature after it is removed. There is no state to reset, so this
  137.     *    method does nothing. Part of the IFeature interface. 
  138.     */
  139.     public void reset()
  140.     {
  141.     }
  142.     
  143.     /**
  144.     *    Enables or disables a command. All talker objects which support the 
  145.     *    passed command will disable it (normally by graying it out).
  146.     *
  147.     *    @param    command    Name of command to disable or enable.
  148.     *    @param    on    true if the control is to be enabled, false if it is to be disabled
  149.     */
  150.     public void enableCommand(String command, boolean on)
  151.     {
  152.         Enumeration e = talkerVector.elements();
  153.         while (e.hasMoreElements())
  154.         {
  155.             Object obj = (Object)e.nextElement();
  156.             
  157.             // if the element supports ICommandFeedback, try to enable/disable
  158.             // this command
  159.             if (obj instanceof ICommandFeedback)
  160.             {
  161.                 ((ICommandFeedback)obj).enableCommand(command, on);                                    
  162.             }
  163.         }
  164.     }
  165.     
  166.     
  167.     /**
  168.     *    Checks whether a control is enabled or not.
  169.     *
  170.     *    @param    command    Name of command to check 
  171.     */
  172.     public boolean isEnabled(String command)
  173.     {
  174.         Enumeration e = talkerVector.elements();
  175.         while (e.hasMoreElements())
  176.         {
  177.             Object obj = (Object)e.nextElement();
  178.             
  179.             if (obj instanceof ICommandFeedback)
  180.             {
  181.                 if (!((ICommandFeedback)obj).isEnabled(command))
  182.                 {
  183.                     return false;
  184.                 }
  185.             }
  186.         }
  187.         
  188.         return true;
  189.     }
  190.     
  191.     /**
  192.     *    Checks or unchecks a check menu item or a check box button. All talker
  193.     *    controls will check or uncheck the item.
  194.     *
  195.     *    @param    command    Name of command to check or uncheck
  196.     *    @param    on    true if the command is to be checked, false if it is to be unchecked
  197.     */
  198.     public void setChecked(String command, boolean on)
  199.     {
  200.         Enumeration e = talkerVector.elements();
  201.         while (e.hasMoreElements())
  202.         {
  203.             Object obj = (Object)e.nextElement();
  204.             
  205.             if (obj instanceof ICommandFeedback)
  206.             {
  207.                 ((ICommandFeedback)obj).setChecked(command, on);
  208.             }
  209.         }
  210.     }
  211.     
  212.     
  213.     /**
  214.     *    Adds a file to the Most Recently Used (MRU) file list of all the
  215.     *    talker components.
  216.     *
  217.     *    @param    filename    Name of file to add
  218.     */
  219.     public void addToMRUList(String filename)
  220.     {
  221.         Enumeration e = talkerVector.elements();
  222.         while (e.hasMoreElements())
  223.         {
  224.             Object obj = (Object)e.nextElement();
  225.             
  226.             if (obj instanceof ICommandFeedback)
  227.             {
  228.                 ((ICommandFeedback)obj).addToMRUList(filename);
  229.             }
  230.         }
  231.     }
  232.     
  233.     /**
  234.     *    Called when a control in the system changes color. Notifies all
  235.     *    talker components of this change.
  236.     *
  237.     *    @param    comp    Component who had a color change
  238.     *    @param    color    Color that the component was changed to
  239.     *    @param    whichPart    Which part of the component had a color change. This can
  240.     *                        be either FOREGROUND or BACKGROUND.
  241.     */        
  242.     public void    notifyColorChange(Object comp, FxColor color, int whichPart)
  243.     {
  244.         Enumeration e = talkerVector.elements();
  245.         while (e.hasMoreElements())
  246.         {
  247.             Object obj = (Object)e.nextElement();
  248.             
  249.             if (obj instanceof ICommandFeedback)
  250.             {
  251.                 ((ICommandFeedback)obj).notifyColorChange(comp,color,whichPart);
  252.             }
  253.         }
  254.     }
  255.     
  256.     /**
  257.     *    Called whenever a command is performed. Override this in a subclass
  258.     *    to perform custom command processing for your application.
  259.     *
  260.     *    @param    evt    Action that was performed.    
  261.     */
  262.     public abstract void actionPerformed(UIActionEvent evt);
  263.     
  264. }
  265.  
  266.