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