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

  1. //
  2. // (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  3. //
  4.  
  5. import com.ms.ui.event.*;
  6.  
  7. /**
  8. *    Feature plug in which handles the enabling and disabling of basic menu
  9. *    items. It does this by listening to a text control and setting the
  10. *    state of the commands accordingly. Descends from KeyFeature, which implements
  11. *    listening on keystrokes. We also add support for mouse listening in this
  12. *    class via the IUIMouseListener interface.
  13. *
  14. *    @see    ITextFeature
  15. *    @see    KeyListener
  16. *
  17. *    @version    1.0, 7/16/97
  18. */
  19.  
  20. public class EnablerFeature extends KeyFeature implements IUIMouseListener
  21. {
  22.     /**
  23.     *    Command feedback object. We send all requests for command enabling/disabling to this object.    
  24.     */
  25.     protected ICommandFeedback commandObj;
  26.     
  27.     /**
  28.     *    Creates a new EnablerFeature.
  29.     *
  30.     *    @param    commandobj    Command feedback object
  31.     */
  32.     public EnablerFeature(ICommandFeedback commandobj)
  33.     {
  34.         textTarget = null;
  35.         commandObj = commandobj;
  36.     }
  37.     
  38.     /**
  39.     *    Called to register the feature with a control. Sets up listeners    
  40.     *    so any incoming mouse events or key events can be detected. 
  41.     *    Part of the IFeature interface.
  42.     */
  43.     public void register()
  44.     {
  45.         super.register();    // this adds keyboard listeners
  46.         textTarget.addMouseListener(this);            
  47.     }
  48.     
  49.     /**
  50.     *    Called to unregister the feature. Removes all listeners on the edit control.
  51.     *    Part of the IFeature interface.
  52.     */
  53.     public void unregister()
  54.     {
  55.         super.unregister();
  56.         textTarget.removeMouseListener(this);            
  57.     }
  58.     
  59.     /**
  60.     *    Called to reset the state of this feature. Since we don't hold any state,
  61.     *    this method does nothing. Part of the IFeature interface.
  62.     */
  63.     public void reset()
  64.     {
  65.     }
  66.     
  67.     /**
  68.     *    Checks the state of the edit control we're attached to and determines
  69.     *    which commands should be enabled and which should be disabled.
  70.     */
  71.     public void checkCommandStatus()
  72.     {
  73.         boolean bCutCopyDelete = false;
  74.         boolean bSelectFindReplace = false;
  75.         boolean bPaste = true;
  76.         boolean bUndo = false;
  77.         boolean bRedo = false;
  78.     
  79.         if(textTarget==null)
  80.             return;
  81.  
  82.         // if no text is selected, disable cut, copy, and delete. Otherwise,
  83.         // enable them
  84.         if (textTarget.getSelectionStart() == textTarget.getSelectionEnd()) 
  85.         {
  86.             bCutCopyDelete = false;
  87.         }
  88.         else
  89.         {
  90.             bCutCopyDelete = true;
  91.         }
  92.         
  93.         if (textTarget.isReadOnly())
  94.         {
  95.             bPaste = false;
  96.             bCutCopyDelete = false;
  97.             bUndo = bRedo = false;
  98.         }
  99.         
  100.         // !! check if we can make this algorithm nicer
  101.         // check if we can undo or redo
  102.         if (textTarget.isUndoable())
  103.         {
  104.             bUndo = true;
  105.         }
  106.         else
  107.         {
  108.             bUndo = false;
  109.         }
  110.         
  111.         if (textTarget.isRedoable())
  112.         {
  113.             bRedo = true;
  114.         }
  115.         else
  116.         {
  117.             bRedo = false;
  118.         }
  119.         
  120.         // if there's no text in the control, disable find/replace and
  121.         // select all. Otherwise enable them.
  122.         if (textTarget.getText().length() == 0)
  123.         {
  124.             bSelectFindReplace = false;
  125.         }
  126.         else
  127.         {
  128.             bSelectFindReplace = true;
  129.         }
  130.         
  131.         // enable and disable the controls
  132.         commandObj.enableCommand(JNotePad.loadString(ResourceIDs.IDS_CUT), bCutCopyDelete);
  133.         commandObj.enableCommand(JNotePad.loadString(ResourceIDs.IDS_COPY), bCutCopyDelete);
  134.         commandObj.enableCommand(JNotePad.loadString(ResourceIDs.IDS_PASTE), bPaste);
  135.         commandObj.enableCommand(JNotePad.loadString(ResourceIDs.IDS_DELETE), bCutCopyDelete);
  136.         commandObj.enableCommand(JNotePad.loadString(ResourceIDs.IDS_SELECT_ALL), bSelectFindReplace);
  137.         commandObj.enableCommand(JNotePad.loadString(ResourceIDs.IDS_FIND), bSelectFindReplace);
  138.         commandObj.enableCommand(JNotePad.loadString(ResourceIDs.IDS_REPLACE), bSelectFindReplace);
  139.         commandObj.enableCommand(JNotePad.loadString(ResourceIDs.IDS_UNDO), bUndo);
  140.         commandObj.enableCommand(JNotePad.loadString(ResourceIDs.IDS_REDO), bRedo);    
  141.         
  142.     }
  143.     
  144.     /**
  145.     *    Called when a key is pressed but before it
  146.     *    is released. Does nothing; we do all processing in keyReleased(). Part of IUIKeyListener. 
  147.     *
  148.     *    @param    ke    Key event being processed.
  149.     */
  150.     public synchronized void keyPressed(UIKeyEvent ke)
  151.     {
  152.     }
  153.     
  154.     /**
  155.     *    Called when a key is released. Calls checkCommandStatus() for command
  156.     *    processing. Part of IUIKeyListener. 
  157.     *
  158.     *    @see    #checkCommandStatus
  159.     *    @param    ke    Key event being processed.
  160.     */    
  161.     public synchronized void keyReleased(UIKeyEvent ke)
  162.     {
  163.         checkCommandStatus();
  164.     }
  165.     
  166.     /**
  167.     *    Called when a key is typed (after it is released). Does nothing; we do all
  168.     *    processing in keyReleased(). Part of IUIKeyListener. 
  169.     *
  170.     *    @param    ke    Key event being processed.
  171.     */    
  172.     public synchronized void keyTyped(UIKeyEvent ke)
  173.     {            
  174.     }
  175.     
  176.     /**
  177.     *    Called when the mouse button is clicked (pressed and released). Does nothing; we do all
  178.     *    processing in mouseReleased(). Public IUIMouseListener method. 
  179.     *
  180.     *    @param evt Mouse event that occurred
  181.     */    
  182.     public void mouseClicked(UIMouseEvent evt)
  183.     {
  184.     }
  185.     
  186.     /**
  187.     *    Called when the mouse button is pressed. Does nothing; we do all
  188.     *    processing in mouseReleased(). Public IUIMouseListener method. 
  189.     *
  190.     *    @param evt Mouse event that occurred     
  191.     */
  192.     public void mousePressed(UIMouseEvent evt)
  193.     {
  194.     }
  195.     
  196.     /**
  197.     *    Called when the mouse button is released. Calls checkCommandStatus()
  198.     *    for command processing. Public IUIMouseListener method. 
  199.     *
  200.     *    @param evt Mouse event that occurred     
  201.     *    @see    #checkCommandStatus
  202.     */    
  203.     public void mouseReleased(UIMouseEvent evt)
  204.     {
  205.         checkCommandStatus();
  206.     }
  207.     
  208.     /**
  209.     *    Called when the mouse enters the control. Does nothing; we don't
  210.     *    care about this event. Public IUIMouseListener method. 
  211.     *
  212.     *    @param evt Mouse event that occurred     
  213.     */    
  214.     public void mouseEntered(UIMouseEvent evt)
  215.     {
  216.         // don't care about this
  217.     }
  218.     
  219.     /**
  220.     *    Called when the mouse leaves the control. Does nothing; we don't
  221.     *    care about this event. Public IUIMouseListener method. 
  222.     *
  223.     *    @param evt Mouse event that occurred     
  224.     */    
  225.     public void mouseExited(UIMouseEvent evt)
  226.     {
  227.         // don't care about this
  228.     }
  229.     
  230.     
  231. }