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

  1. //
  2. // (c) 1998 Microsoft Corporation.  All rights reserved.
  3. //
  4.  
  5. import JNoteKeyFeature;
  6. import com.ms.ui.event.*;
  7. import java.awt.event.*;
  8. import java.awt.*;
  9. import com.ms.ui.*;
  10. import java.util.Hashtable;
  11.  
  12. /**
  13. *    IFeature plugin. Intercepts accelerator keys (also known as shortcut keys)
  14. *    from the text area and converts them into menu item events. 
  15. *
  16. *    @see    IFeature
  17. *    @see    IKeyFeature
  18. *    @see    JNoteKeyFeature
  19. *
  20. *    @version    1.0, 7/23/97
  21. */
  22.  
  23. public class AcceleratorFeature extends JNoteKeyFeature
  24. {
  25.     /**
  26.     *    Hashtable to hold keys-event pairs for control-key combinations    
  27.     */
  28.     protected Hashtable controlKeysTable;
  29.     
  30.     /**
  31.     *    Hashtable to hold keys-event pairs for shift-control-key combinations    
  32.     */
  33.     protected Hashtable shiftCtrlKeysTable;
  34.     
  35.     /**
  36.     *    Component to send events to    
  37.     */
  38.     protected UIComponent eventTarget;
  39.     
  40.     
  41.     /**
  42.     *    Object which handles feedback (components telling menu bars/toolbars what to gray out)
  43.     */
  44.     protected ICommandFeedback feedbackObj;
  45.     
  46.     /**
  47.     *    Creates a new AcceleratorFeature which sends events to the given target
  48.     *
  49.     *    @param    eventtarget    UIComponent to send command selection events to
  50.     */
  51.     public AcceleratorFeature(UIComponent eventtarget)
  52.     {
  53.         localInit(eventtarget, null);
  54.     }    
  55.     /**
  56.     *    Creates a new AcceleratorFeature which sends events to the given target
  57.     *
  58.     *    @param    eventtarget    UIComponent to send command selection events to
  59.     *    @param    feedbackobj    Object which handles command feedback - enabling and disabling menu items
  60.     */
  61.     public AcceleratorFeature(UIComponent eventtarget, ICommandFeedback feedbackobj)
  62.     {
  63.         localInit(eventtarget, feedbackobj);
  64.     }    
  65.     
  66.     /**
  67.     *    Private init function. Handles initialization of variables. 
  68.     */
  69.     private void localInit(UIComponent eventtarget, ICommandFeedback feedbackobj)
  70.     {
  71.         controlKeysTable = new Hashtable(11);
  72.         shiftCtrlKeysTable = new Hashtable(11);
  73.         eventTarget = eventtarget;
  74.         feedbackObj = feedbackobj;
  75.     }
  76.     
  77.     /**
  78.     *    Adds an accelerator key.
  79.     *
  80.     *    @param    accelKey    key to add to accelerator list.
  81.     *    @param    action        Name of command to activate when accelerator key is pressed.
  82.     *    @param    hasShift    false if the command requires control to be held with the accelerator key,
  83.     *                            true if the command requires control and shift to be held down.
  84.     */
  85.     public void addAccelerator(char accelKey, String action, boolean hasShift)
  86.     {        
  87.         // convert to uppercase and place in Character object
  88.         Character e1 = new Character(Character.toUpperCase(accelKey));
  89.         // create a new action event which will simulate clicking on the
  90.         //        menu item/toolbar button.
  91.         Event e2 = new Event(eventTarget, 0, Event.ACTION_EVENT, 0, 0,
  92.             0, 0, new UIMenuItem(action));
  93.         
  94.         // put key-event pair in the right hashtable
  95.         if (!hasShift)
  96.         {
  97.             controlKeysTable.put(e1, e2);
  98.         }
  99.         else
  100.         {
  101.             shiftCtrlKeysTable.put(e1, e2);
  102.         }
  103.     }
  104.     
  105.     /**
  106.     *    Removes an accelerator key.
  107.     *
  108.     *    @param    accelKey    Key to remove
  109.     *    @param    hasShift    true if removing from control-shift-key accelerator list,
  110.     *                            false if from the control-key acclerator list.
  111.     */
  112.     public void removeAccelerator(char accelKey, boolean hasShift)
  113.     {
  114.         controlKeysTable.remove(new Character(accelKey));
  115.     }
  116.     
  117.     /**
  118.     *    Called when a key is pressed. Part of IUIKeyListener. Finds out if an
  119.     *    accelerator key has been pressed; if so, find the Event associated with
  120.     *    it and send it to the event target.
  121.     *
  122.     *    @param    ke    KeyEvent which is being handled
  123.     */
  124.     public synchronized void keyPressed(UIKeyEvent ke)
  125.     {
  126.         int iModifiers = ke.getModifiers();
  127.         
  128.         if ((iModifiers & UIInputEvent.CTRL_MASK) > 0)
  129.         {
  130.             // it's a control-key combo
  131.             Character e1 = new Character((char)ke.getKeyCode());
  132.             Event e = null;
  133.             
  134.             // pull Event out of right table
  135.             if ((iModifiers & UIInputEvent.SHIFT_MASK) == 0)
  136.             {
  137.                 e = (Event)controlKeysTable.get(e1);
  138.             }
  139.             else
  140.             {
  141.                 e = (Event)shiftCtrlKeysTable.get(e1);
  142.             }
  143.             
  144.             // Send a item selected message to the
  145.             // attached event target
  146.             if (e != null)
  147.             {
  148.                 // if there's a feedback object, ask it if the item is
  149.                 // disabled.
  150.                 if (feedbackObj != null) 
  151.                 {
  152.                     if (!feedbackObj.isEnabled(((UIComponent)e.arg).getName()))
  153.                     {
  154.                         // not enabled, so don't do anything
  155.                         return;
  156.                     }
  157.                 }
  158.                 
  159.                 try
  160.                 {
  161.                     int i = Integer.parseInt(((UIComponent)e.arg).getName());
  162.                     ((IUIComponent)e.arg).setID(i);
  163.                 }
  164.                 catch ( Exception ex )
  165.                 {
  166.                 }
  167.  
  168.                 // send the event to the right place
  169.                 eventTarget.postEvent(e);
  170.             }
  171.         }
  172.         
  173.     }
  174.     
  175.     /**
  176.     *    Resets the feature; done when it is removed from
  177.     *    an edit control. Does nothing since there is no state to reset.
  178.     *    Part of IFeature. 
  179.     */
  180.     public void reset()
  181.     {
  182.         // no need to do anything. Accelerator keys don't
  183.         // change between files.
  184.     }
  185.     
  186. }
  187.  
  188.