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

  1. //
  2. // (c) 1998 Microsoft Corporation.  All rights reserved.
  3. //
  4. import com.ms.ui.*;
  5. import com.ms.ui.event.*;
  6. import java.util.*;
  7.  
  8. /**
  9. *    JNotepad specific extension of CommandFeature. Using the ICommandFeature
  10. *    and ICommandFeedback interfaces, this module allows a program to handle
  11. *    command processing and routing quickly and easily, as well as command item 
  12. *    enabling and disabling. 
  13. *
  14. *    @see    IFeature
  15. *    @see    ICommandFeedback
  16. *    @see    IComponentFeature
  17. *    @see    CommandFeature
  18. *
  19. *    @version    1.0, 8/6/97
  20. */
  21.  
  22. public class JNoteCommandFeature extends CommandFeature
  23. {        
  24.         
  25.     /**
  26.     *    Creates a new JNoteCommandFeature.
  27.     */
  28.     public JNoteCommandFeature()
  29.     {
  30.         super();
  31.     }
  32.     
  33.     
  34.     /**
  35.     *    Private function. Goes through all of the known general file commands
  36.     *    (those defined in the IFileOperationTarget
  37.     *    interface) and calls appropriate methods in that interface
  38.     *    if the command is found. If none of these are found, it calls the
  39.     *    otherCommand() method to see if the command is an application-specific one.
  40.     *
  41.     *    @param    name    Name of command
  42.     *    @param    evt        Event object which generated this command
  43.     *    @returns    true if the command was handled, false if not
  44.     */
  45.     private boolean checkFileCommands(String name, UIActionEvent evt)
  46.     {
  47.         IUIComponent comp=evt.getActionItem();
  48.         switch( comp.getID() )
  49.         {
  50.         case ResourceIDs.ID_FILE_NEW:
  51.             return doFileNew();
  52.         case ResourceIDs.ID_FILE_OPEN:
  53.             return doFileOpen();
  54.         case ResourceIDs.ID_FILE_SAVE:
  55.             return doFileSave();
  56.         case ResourceIDs.ID_FILE_SAVEAS:
  57.             return doFileSaveAs();
  58.         case ResourceIDs.ID_FILE_CLOSE:
  59.             return doFileClose();
  60.         case ResourceIDs.ID_FILE_PRINT:
  61.             return doFilePrint();
  62.  
  63.         case ResourceIDs.ID_EDIT_FIND:
  64.             return doEditFind();
  65.         case ResourceIDs.ID_EDIT_REPLACE:
  66.             return doEditReplace();
  67.         } 
  68.         return fileTarget.otherCommand(name, evt);                        
  69.     }
  70.     
  71.     /**
  72.     *    Private function. Goes through all of the known general text commands
  73.     *    (those defined in the ITextOperationTarget 
  74.     *    interface) and calls appropriate methods in that interface
  75.     *    if the command is found. If none of these are found, it calls the
  76.     *    otherCommand() method to see if the command is an application-specific one.
  77.     *
  78.     *    @param    name    Name of command
  79.     *    @param    evt        Event object which generated this command
  80.     *    @returns    true if the command was handled, false if not
  81.     */
  82.     private boolean checkTextCommands(String name, UIActionEvent evt)
  83.     {
  84.         IUIComponent comp=evt.getActionItem();
  85.         switch( comp.getID() )
  86.         {
  87.         case ResourceIDs.ID_EDIT_UNDO:
  88.             return doEditUndo();
  89.  
  90.         case ResourceIDs.ID_EDIT_REDO:
  91.             return doEditRedo();
  92.  
  93.         case ResourceIDs.ID_EDIT_CUT:
  94.             return doEditCut();
  95.  
  96.         case ResourceIDs.ID_EDIT_COPY:
  97.             return doEditCopy();
  98.  
  99.         case ResourceIDs.ID_EDIT_PASTE:
  100.             return doEditPaste();
  101.  
  102.         case ResourceIDs.ID_EDIT_DELETE:
  103.             return doEditDelete();
  104.  
  105.         case ResourceIDs.ID_EDIT_SELECT_ALL:
  106.             return doEditSelectAll();
  107.  
  108.         case ResourceIDs.ID_FONT:
  109.             return doFont();
  110.  
  111.         case ResourceIDs.ID_TEXTCOLOR:
  112.             return doTextColor();
  113.  
  114.         case ResourceIDs.ID_BGCOLOR:
  115.             return doBackgroundColor();
  116.         }
  117.         return textTarget.otherCommand(name, evt);                        
  118.     }
  119.     
  120.     /**
  121.     *    Called when an action event
  122.     *    occurs in one of the talker components. Processes the command through
  123.     *    <a href="#checkTextCommands">checkTextCommands</a> and
  124.     *    <a href="#checkFileCommands">checkFileCommands</a>. 
  125.     *    Part of the IUIActionListener interface. 
  126.     *
  127.     *    @param    evt    UIActionEvent which occurred.
  128.     */
  129.     public void actionPerformed(UIActionEvent evt)
  130.     {
  131.         String name = evt.getActionCommand();
  132.         
  133.         if (name == null)
  134.         {
  135.             return;
  136.         }
  137.         
  138.         boolean ret = false;
  139.         
  140.         if (fileTarget != null)
  141.         {
  142.             ret = checkFileCommands(name, evt);
  143.         }
  144.         
  145.         if ((textTarget != null) && (ret == false))
  146.         {
  147.             checkTextCommands(name, evt);
  148.         }
  149.         
  150.         textTarget.requestFocus();
  151.     }
  152.  
  153.     protected boolean doFileNew()
  154.     {
  155.         fileTarget.newFile();
  156.         return true;
  157.     }
  158.  
  159.     protected boolean doFileOpen()
  160.     {
  161.         fileTarget.openFile();
  162.         return true;
  163.     }
  164.  
  165.     protected boolean doFileSave()
  166.     {
  167.         fileTarget.saveFile();
  168.         return true;
  169.     }
  170.  
  171.     protected boolean doFileSaveAs()
  172.     {
  173.         fileTarget.saveFileAs();
  174.         return true;
  175.     }
  176.  
  177.     protected boolean doFileClose()
  178.     {
  179.         fileTarget.closeFile();
  180.         return true;
  181.     }
  182.  
  183.     protected boolean doEditFind()
  184.     {
  185.         fileTarget.searchReplace(true);
  186.         return true;
  187.     }
  188.  
  189.     protected boolean doEditReplace()
  190.     {
  191.         fileTarget.searchReplace(false);
  192.         return true;
  193.     }
  194.  
  195.     protected boolean doFilePrint()
  196.     {
  197.         fileTarget.printFile();
  198.         return true;
  199.     }
  200.  
  201.     protected boolean doEditUndo()
  202.     {
  203.         textTarget.undo();
  204.         return true;
  205.     }
  206.  
  207.     protected boolean doEditRedo()
  208.     {
  209.         textTarget.redo();
  210.         return true;
  211.     }
  212.  
  213.     protected boolean doEditCut()
  214.     {
  215.         textTarget.cut();
  216.         return true;
  217.     }
  218.  
  219.     protected boolean doEditCopy()
  220.     {
  221.         textTarget.copy();
  222.         return true;
  223.     }
  224.  
  225.     protected boolean doEditPaste()
  226.     {
  227.         textTarget.paste();
  228.         return true;
  229.     }
  230.  
  231.     protected boolean doEditDelete()
  232.     {
  233.         textTarget.delete();
  234.         return true;
  235.     }
  236.  
  237.     protected boolean doEditSelectAll()
  238.     {
  239.         textTarget.selectAll();
  240.         return true;
  241.     }
  242.  
  243.     protected boolean doFont()
  244.     {
  245.         textTarget.changeFont();
  246.         return true;
  247.     }
  248.  
  249.     protected boolean doTextColor()
  250.     {
  251.         textTarget.changeTextColor();        
  252.         return true;
  253.     }
  254.  
  255.     protected boolean doBackgroundColor()
  256.     {
  257.         textTarget.changeBackColor();
  258.         return true;
  259.     }
  260. }
  261.