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