home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / afc11 / JNotepad / src / JNoteMenubar.java < prev    next >
Encoding:
Java Source  |  2000-05-04  |  11.1 KB  |  355 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 java.awt.*;
  7. import com.ms.ui.resource.*;
  8. import java.util.Hashtable;
  9. import com.ms.fx.FxColor;
  10. /**
  11.  *    An extension of UIBand which adds some extra functionality and ease
  12.  *    of use features. JNoteMenubar supports loading in menus from resource
  13.  *    files, automatically places menu items in a Hashtable for easy access,
  14.  *    and supports a Most Recently Used (MRU) Files menu.
  15.  *    <p>
  16.  *    JNoteMenubar uses the ICommandFeedback interface to enabled and disable menu
  17.  *    items (as well as check and uncheck them). The easiest way to get command events
  18.  *    from a JNoteMenubar is to use a CommandFeature.
  19.  *
  20.  *    @see    #ICommandFeedback
  21.  *    @see    #CommandFeature
  22.  *
  23.  *    @version    1.0, 7/9/97
  24.  */
  25. public class JNoteMenubar extends UIBand implements IConstants, ICommandFeedback{
  26.     /**
  27.     *    Hashtable to store menu items in, for easy access
  28.     */
  29.     protected Hashtable menuItemTable;
  30.     /**
  31.     *    Menu items in most recently used files list    
  32.     */
  33.     protected UIMenuItem MRUList[];
  34.     /**
  35.     *    Maximum number of files in the MRU list.    
  36.     */
  37.     public static final int MENUBAR_MAX_MRU = 4;
  38.     /**
  39.     *    Creates a new, empty JNoteMenubar.
  40.     */
  41.     public JNoteMenubar(){
  42.         super();
  43.         init();        
  44.     }
  45. /**
  46.  *    Creates a new JNoteMenubar and fills it with a menu from a resource file.
  47.  *
  48.  *    @param    name    Name of menubar
  49.  *    @param    menuResource    Name of resource file (.res) to load menu from
  50.  *    @param    menuID    integer resource ID of menu to load
  51.  */
  52.     public JNoteMenubar(String name, 
  53.                         String menuResource, 
  54.                         int menuID){
  55.         super(name, BREAK);
  56.         loadMenu(this, menuResource, menuID);
  57.         init();
  58.     }
  59. /**
  60.  *    Creates a new JNoteMenubar and fills it with a menu from a resource file.
  61.  *
  62.  *    @param    name    Name of menubar
  63.  *    @param    menuResource    Win32Resource decoder object to load resources from
  64.  *    @param    menuID    integer resource ID of menu to load
  65.  */
  66.     public JNoteMenubar(String name, 
  67.                         Win32ResourceDecoder menuResource, 
  68.                         int menuID){
  69.         super(name, BREAK);
  70.         loadMenu(this, menuResource, menuID);
  71.         init();
  72.     }
  73. /**
  74.  *    private init method. Initializes all variables and calls loadMenuItems()
  75.  *    to load all menu items into the hashtable.
  76.  */
  77.     private void init(){
  78.         MRUList = null;
  79.         menuItemTable = new Hashtable(23);
  80.         loadMenuItems();
  81.     }
  82. /**
  83.  *    Goes through a menu, grabs all menu items,
  84.  *    and puts them into the hashtable. Private helper function. 
  85.  *    This function is called recursively on submenus.
  86.  *
  87.  *    @param    menulist    Menu list to traverse. 
  88.  */
  89.     private void addItemsFromMenuList(UIMenuList menulist, 
  90.                                       UIMenuLauncher menulistOwner){    
  91.         if (menulist == null)
  92.             return;
  93.         UIComponent comp2 = null;    
  94.         int iNumItems = menulist.getComponentCount();
  95.         for (int j=0; j < iNumItems; ++j){
  96.             comp2 = (UIComponent)menulist.getComponent(j);                                    
  97.             if (comp2.getName() != null){
  98.                 String name = comp2.getName();
  99.                 menuItemTable.put(name, comp2);
  100.                 if (comp2 instanceof UIMenuLauncher){
  101.                     UIMenuList ml = ((UIMenuLauncher)comp2).getMenu();
  102.                     if (ml != null)
  103.                         addItemsFromMenuList(ml, (UIMenuLauncher)comp2);
  104.                 }
  105.             }
  106.         }
  107.     }
  108. /**
  109.  *    Puts all of the items in the menu into a table. This allows us to
  110.  *    have quick and easy access to menu items.
  111.  */
  112.     public void loadMenuItems()    {
  113.         MRUList = new UIMenuItem[MENUBAR_MAX_MRU];
  114.         menuItemTable.clear();
  115.         int iNumComp = getComponentCount();
  116.         if (iNumComp < 2)        {
  117.             return;
  118.         }
  119.         UIContainer comp = null;
  120.         for (int i = 1; i < iNumComp; ++i)        {
  121.             comp = (UIContainer)getComponent(i);
  122.             if ((comp instanceof UIMenuButton))            {
  123.                 UIMenuList menulist = ((UIMenuButton)comp).getMenu();
  124.                 menuItemTable.put(comp.getName(), menulist);
  125.                 addItemsFromMenuList(menulist, (UIMenuLauncher)comp);
  126.             }
  127.         }
  128.         //loadMRUList();            
  129.     }
  130. /**
  131.  *    Retrieves a menu item from the menu.
  132.  *
  133.  *    @param    name    Name of menu item to retrieve
  134.  *    @returns    Menu item with the given name. Cast this to the proper type.
  135.  */
  136.     public UIComponent getMenuItem(String name)    {
  137.         if (name == null)        {
  138.             return null;
  139.         }
  140.         return (UIComponent)menuItemTable.get(name);
  141.     }
  142. /**
  143.  *    Adds a submenu to the given menu item.
  144.  *    
  145.  *    @param    ml    UIMenuList to add to the menu item
  146.  */
  147.     public boolean addSubmenuToMenu(String command, UIMenuList ml)    {
  148.         Object item = menuItemTable.get(command);
  149.         if ((item != null) && (item instanceof UIMenuLauncher)){
  150.             UIMenuLauncher mlaunch = (UIMenuLauncher)item;
  151.             // add it to the menu item
  152.             mlaunch.setMenu(ml);
  153.             return true;
  154.         }
  155.         return false;
  156.     }
  157. /**
  158.  *    Adds a word wrap submenu to the given menu ID.
  159.  *
  160.  *    @param    itemID    Menu item ID to add submenu to
  161.  */
  162.     public boolean addWordWrapSubmenu(String command)    {
  163.         // create a word wrap submenu
  164.         UIMenuList ml = new UIMenuList();
  165.         UIRadioButton noWrap = new UIRadioButton(JNotePad.loadString(ResourceIDs.IDS_NOWORDWRAP));
  166.         UIRadioButton wordWrap = new UIRadioButton(JNotePad.loadString(ResourceIDs.IDS_WORDWRAP));
  167.         UIRadioButton wordWrap2 = new UIRadioButton(JNotePad.loadString(ResourceIDs.IDS_WORDWRAP2));
  168.         noWrap.setID(ResourceIDs.ID_NOWORDWRAP);
  169.         wordWrap.setID(ResourceIDs.ID_WORDWRAP);
  170.         wordWrap2.setID(ResourceIDs.ID_WORDWRAP2);
  171.         ml.add(noWrap);
  172.         ml.add(wordWrap);
  173.         ml.add(wordWrap2);
  174.         menuItemTable.put(noWrap.getName(), noWrap);
  175.         menuItemTable.put(wordWrap.getName(), wordWrap);
  176.         menuItemTable.put(wordWrap2.getName(), wordWrap2);
  177.         return addSubmenuToMenu(command, ml);
  178.     }
  179. /**
  180.  *    Fills a menu with a menu retrieved from a resource file.
  181.  *
  182.  *    @param    menu    Menu to load into
  183.  *    @param    menuResource    Name of resource file to load menu from
  184.  *    @param    menuID    Integer resource ID of menu to load
  185.  *
  186.  *    @returns    true if the menu was loaded, false if not.
  187.  */
  188.     static public boolean loadMenu(UIBand menu, String menuResource, int menuID)    {
  189.         Win32ResourceDecoder resdec;
  190.         try{
  191.             resdec = new Win32ResourceDecoder(menuResource);
  192.         }catch (Exception e){
  193.             // menu init error
  194.             UIMessageBox box = new UIMessageBox(new UIFrame(), 
  195.                 JNotePad.loadString(ResourceIDs.IDS_MSGTITLE),
  196.                 JNotePad.loadString(ResourceIDs.IDS_MSGMENUINIT),
  197.                 UIMessageBox.STOP, UIButtonBar.OK);
  198.             box.doModal();                                    
  199.             return false;        
  200.         }
  201.         return loadMenu(menu, resdec, menuID);
  202.     }
  203. /**
  204.  */
  205.     static public boolean loadMenu(UIBand menu, Win32ResourceDecoder menuResource, int menuID){
  206.         try{
  207.             menuResource.populateMenu(menuID, menu);
  208.         }catch (Exception e){
  209.             // internal menu error
  210.             UIMessageBox box = new UIMessageBox(null, 
  211.                 JNotePad.loadString(ResourceIDs.IDS_MSGTITLE),
  212.                 JNotePad.loadString(ResourceIDs.IDS_MSGMENULOAD),
  213.                 box.STOP, UIButtonBar.OK);
  214.             box.doModal();
  215.             return false;
  216.         }        
  217.         return true;
  218.     }
  219. /**
  220.  *    Private method to load the MRU list from the JNoteSettings object
  221.  */
  222.     private void loadMRUList()    {
  223.         JNoteSettings settingsObj = JNoteAppSettings.getSettingsObj();
  224.         initMRUList();
  225.         for (int i=1; i < MENUBAR_MAX_MRU+1; ++i){
  226.             String MRUfile = settingsObj.get("Menubar", "MRU"+i, null);
  227.             if (MRUfile != null)
  228.                 addMRUListEntry(MRUfile);
  229.         }                                
  230.     }
  231. /**
  232.  *    Private method to initialize the MRU file list.
  233. */
  234.     private void initMRUList()    {
  235.         Object obj = menuItemTable.get("Recent Files");
  236.         UIMenuList menulist = null;
  237.         if (obj instanceof UIMenuList)
  238.             menulist = (UIMenuList)obj;
  239.         else if (obj instanceof UIMenuLauncher)
  240.             menulist = ((UIMenuLauncher)obj).getMenu();
  241.         else        
  242.             return;
  243.         if (menulist == null)        
  244.             return;
  245.         if (menulist != null){    
  246.             MRUList[0] = (UIMenuItem)menulist.getComponent(0);
  247.             menuItemTable.remove(MRUList[0].getName());
  248.             MRUList[0].setName("1 ");
  249.             for (int i = 1; i < MENUBAR_MAX_MRU; ++i)            {
  250.                 MRUList[i] = new UIMenuItem((i+1)+" ");
  251.                 menulist.add(MRUList[i]);
  252.             }
  253.         }
  254.     }
  255. /**
  256.  *    Adds a filename to the Most Recently Used 
  257.  *    (MRU) submenu. The last item in the list is discarded if this addition
  258.  *    causes the list to overflow. Part of the ICommandFeedback feature. 
  259.  *
  260.  *    @param    filename    Name of the file to add to the MRU list.
  261.  */
  262.     public void addToMRUList(String filename)    {
  263.         if (filename == null){
  264.             // internal error
  265.             UIMessageBox box = new UIMessageBox(new UIFrame(), 
  266.                 JNotePad.loadString(ResourceIDs.IDS_MSGTITLE),
  267.                 JNotePad.loadString(ResourceIDs.IDS_MSGINTERNALERR),
  268.                 box.STOP, UIButtonBar.OK);
  269.             box.doModal();
  270.             return;
  271.         }
  272.         if (MRUList == null)
  273.             return;
  274.         addMRUListEntry(filename);
  275.         JNoteSettings settings = JNoteAppSettings.getSettingsObj();
  276.         for (int i = 0; i < MENUBAR_MAX_MRU; ++i)        {
  277.             String MRUfilename = MRUList[i].getName().substring(1);
  278.             settings.put("Menubar", "MRU"+(i+1), MRUfilename);
  279.         }            
  280.     }
  281. /**
  282.  *    Private function to actually add an entry to the MRU list. Does not
  283.  *    update any JNoteSettings entries.
  284.  *    
  285.  *    @param    filename    Filename to add
  286.  */
  287.     protected void addMRUListEntry(String filename)    {    
  288.         if (MRUList == null)        
  289.             return;
  290.         if (filename == null)        
  291.             return;
  292.         for (int i = 0; i < MENUBAR_MAX_MRU; ++i)        
  293.             if (MRUList[i].getName().substring(2).equals(filename)){
  294.                 return;
  295.         }
  296.         JNoteSettings settings = JNoteAppSettings.getSettingsObj();
  297.         for (int i = MENUBAR_MAX_MRU-1; i > 0; --i)        {
  298.             String MRUfilename = MRUList[i-1].getName().substring(1);
  299.             MRUList[i].setName((i+1) + MRUfilename);
  300.             settings.put("Menubar", "MRU"+(i+1), MRUfilename);
  301.         }
  302.         MRUList[0].setName("1 "+filename);
  303.         settings.put("Menubar", "MRU1", filename);
  304.     }
  305. /**
  306.  *    Called when a menu item needs to be enabled
  307.  *    or disabled. Finds the item and changes its state. Part of the ICommandFeedback feature.
  308.  *
  309.  *    @param    command    Name of menu item to enable or disable
  310.  *    @param    on    true if menu item is to be enabled, false if not
  311.  */
  312.     public void enableCommand(String command, boolean on){
  313.         Object m = menuItemTable.get(command);
  314.         if (m != null)        
  315.             ((UIComponent)m).setEnabled(on);
  316.     }
  317. /**
  318.  *    Queries the enabled state of a menu item. Part of the ICommandFeedback feature.
  319.  *
  320.  *    @param    command    Name of menu item to check
  321.  *    @returns    true if menu item is enabled, false if it is not
  322.  */
  323.     public boolean isEnabled(String command){    
  324.         Object m = menuItemTable.get(command);
  325.         if (m != null)        
  326.             return ((UIComponent)m).isEnabled();
  327.         else
  328.             return true;
  329.     }
  330. /**
  331.  *    Sets a checkbutton to be checked or unchecked. Part of the ICommandFeedback feature.
  332.  *    
  333.  *    @param    command    Name of menu item to check or uncheck
  334.  *    @param    on    true if button is to be checked, false if it is to be unchecked
  335.  */
  336.     public void setChecked(String command, boolean on)    {
  337.         Object m = menuItemTable.get(command);
  338.         if ((m != null) && (m instanceof UICheckButton))
  339.             ((UICheckButton)m).setChecked(on);
  340.     }
  341. /**
  342.  *    Called when a control in the system changes color. Since we don't care
  343.  *    about color changes, we do nothing.
  344.  *
  345.  *    @param    comp    Component who had a color change
  346.  *    @param    color    Color that the component was changed to
  347.  *    @param    whichPart    Which part of the component had a color change. This can
  348.  *                        be either FOREGROUND or BACKGROUND.
  349.  */        
  350.     public void    notifyColorChange(Object comp, 
  351.                                   FxColor color, 
  352.                                   int whichPart){
  353.         // do nothing
  354.     }
  355. }