View Javadoc

1   package com.explosion.expf.menusandtools.menu;
2   
3   import java.util.Enumeration;
4   import java.util.Hashtable;
5   
6   import javax.swing.JCheckBoxMenuItem;
7   import javax.swing.JMenu;
8   import javax.swing.JMenuItem;
9   import javax.swing.KeyStroke;
10  
11  import com.explosion.expf.Application;
12  import com.explosion.expf.ExpListener;
13  
14  
15  /* =============================================================================
16   *       
17   *     Copyright 2004 Stephen Cowx
18   *
19   *     Licensed under the Apache License, Version 2.0 (the "License");
20   *     you may not use this file except in compliance with the License.
21   *     You may obtain a copy of the License at
22   *
23   *     http://www.apache.org/licenses/LICENSE-2.0
24   *
25   *     Unless required by applicable law or agreed to in writing, software
26   *     distributed under the License is distributed on an "AS IS" BASIS,
27   *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28   *     See the License for the specific language governing permissions and
29   *     limitations under the License.
30   * 
31   * =============================================================================
32   */
33  
34  /***
35   * @author Stephen Cowx
36   * Created on 05-Mar-2005
37   */
38  public class MenuHelper
39  {
40      private Hashtable elements = new Hashtable();
41      private ExpListener listener = null;
42         
43      
44      /***
45       * @param listener
46       */
47      public MenuHelper(ExpListener listener)
48      {
49          super();
50          this.listener = listener;
51      }
52      /***
53       *  This method adds a JMenu to a MenuBar
54       */
55      public ExpMenu createExpMenu(String menuText, char mnemonic, String actionCommand, int enabledThreshold)
56      {
57        /* Create a new MenusAndTools Item */
58        ExpMenu menu = new ExpMenu(menuText, actionCommand, enabledThreshold);
59        
60        /* Set the name of the menu */
61        menu.setName(actionCommand);
62        
63        /* Set the name for this menu */
64        menu.setName(menuText);
65        
66        /* Set its mnemonic */
67        if (mnemonic != -1)
68          menu.setMnemonic(mnemonic);
69  
70        /* Add it to the list of elements */
71        elements.put(actionCommand, menu);
72  
73        return menu;
74      }
75  
76      /***
77       *  This method adds a menuItem to a MenusAndTools
78       */
79      public JMenuItem createMenuItem(String itemText, char mnemonic, String actionCommand, KeyStroke accelerator, int enabledThreshold)
80      {
81        /* Create a new MenusAndTools Item */
82        ExpMenuItem menuItem = new ExpMenuItem(itemText, actionCommand, enabledThreshold);
83  
84        /* Set its mnemonic */
85        if (mnemonic != -1)
86          menuItem.setMnemonic(mnemonic);
87  
88        /* Set its action command */
89        menuItem.setActionCommand(actionCommand);
90        
91        /* Set its name */
92        menuItem.setName(itemText);
93  
94        /* Set its accelerator */
95        if (accelerator != null)
96          menuItem.setAccelerator(accelerator);
97  
98        /* Add the listener to it */
99        menuItem.addActionListener(listener);
100 
101       /* Add it to the list of elements */
102       elements.put(actionCommand, menuItem);
103 
104       return menuItem;
105     }
106 
107     /***
108      *  This method adds a menuItem to a MenusAndTools
109      */
110     public JCheckBoxMenuItem createCheckBoxMenuItem(String itemText, char mnemonic, String actionCommand, KeyStroke accelerator, int enabledThreshold)
111     {
112       /* Create a new MenusAndTools Item */
113       ExpCheckBoxMenuItem menuItem = new ExpCheckBoxMenuItem(itemText, actionCommand, enabledThreshold);
114 
115       /* Set its mnemonic */
116       if (mnemonic != -1)
117         menuItem.setMnemonic(mnemonic);
118 
119       /* Set its action command */
120       menuItem.setActionCommand(actionCommand);
121 
122       /* Set its accelerator */
123       if (accelerator != null)
124         menuItem.setAccelerator(accelerator);
125 
126       /* Add the listener to it */
127       menuItem.addActionListener(listener);
128 
129       /* Add it to the list of elements */
130       elements.put(actionCommand, menuItem);
131 
132       return menuItem;
133     }
134     
135     
136     public JMenu getMenu(String menuName)
137     {
138       return (JMenu) elements.get(menuName);
139     }
140 
141     public Object getElement(String elementName)
142     {
143       return elements.get(elementName);
144     }
145 
146     /***
147      * This method runs through the list of menu's and their children and asks each of them to enable or diable themselves
148      */
149     public void checkEnabled()
150     {
151       try
152       {
153         Enumeration en = elements.elements();
154         while (en.hasMoreElements())
155         {
156           Object element = en.nextElement();
157           if (element instanceof ExpMenu)
158             ((ExpMenu) element).checkEnabled(Application.getActiveComponentCookies(), Application.getLocalCookies(),Application.getGlobalCookies());
159           else if (element instanceof ExpMenuItem)
160             ((ExpMenuItem) element).checkEnabled(Application.getActiveComponentCookies(), Application.getLocalCookies(),Application.getGlobalCookies());
161           else if (element instanceof ExpCheckBoxMenuItem)
162             ((ExpCheckBoxMenuItem) element).checkEnabled(Application.getActiveComponentCookies(), Application.getLocalCookies(),Application.getGlobalCookies());
163         }
164       }
165       catch (Exception e)
166       {
167         com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(e, "Exception caught while checking menuItems to see if they should be enabled or not.");
168       }
169     }    
170 }