KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > adwt > service > MenuBarServiceProvider


1 /*
2  * EJTools, the Enterprise Java Tools
3  *
4  * Distributable under LGPL license.
5  * See terms of license at www.gnu.org.
6  */

7 package org.ejtools.adwt.service;
8
9 import java.awt.Container JavaDoc;
10 import java.beans.beancontext.BeanContextServices JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.ResourceBundle JavaDoc;
13 import java.util.Vector JavaDoc;
14
15 import javax.swing.JMenu JavaDoc;
16 import javax.swing.JMenuBar JavaDoc;
17 import javax.swing.JMenuItem JavaDoc;
18
19 import org.ejtools.adwt.AwtToolkit;
20 import org.ejtools.adwt.action.CommandAction;
21 import org.ejtools.beans.beancontext.CustomBeanContextServiceProvider;
22
23 /**
24  * Description of the Class
25  *
26  * @author Laurent Etiemble
27  * @version $Revision: 1.5 $
28  * @todo Javadoc to complete
29  */

30 public class MenuBarServiceProvider extends CustomBeanContextServiceProvider implements MenuBarService
31 {
32    /** Description of the Field */
33    protected Vector JavaDoc listeners = new Vector JavaDoc();
34    /** Description of the Field */
35    protected JMenuBar JavaDoc menuBar = new JMenuBar JavaDoc();
36    /** Description of the Field */
37    protected Vector JavaDoc requestors = new Vector JavaDoc();
38    /** Description of the Field */
39    protected MenuBarService service = null;
40
41
42    /** Constructor for the MenuBarServiceProvider object */
43    public MenuBarServiceProvider()
44    {
45       this.service = this;
46    }
47
48
49    /**
50     * Adds a feature to the MenuBarListener attribute of the MenuBarServiceProvider object
51     *
52     * @param mbsl The feature to be added to the MenuBarListener attribute
53     */

54    public void addMenuBarListener(MenuBarService.Listener mbsl)
55    {
56       this.listeners.add(mbsl);
57    }
58
59
60    /**
61     * Gets the container attribute of the MenuBarServiceProvider object
62     *
63     * @return The container value
64     */

65    public Container JavaDoc getContainer()
66    {
67       return this.menuBar;
68    }
69
70
71    /**
72     * Gets the currentServiceSelectors attribute of the ApplicationServiceProvider object
73     *
74     * @param bcs Description of Parameter
75     * @param serviceClass Description of Parameter
76     * @return The currentServiceSelectors value
77     */

78    public Iterator JavaDoc getCurrentServiceSelectors(BeanContextServices JavaDoc bcs, Class JavaDoc serviceClass)
79    {
80       return new Vector JavaDoc().iterator();
81    }
82
83
84    /**
85     * Gets the service attribute of the ApplicationServiceProvider object
86     *
87     * @param bcs Description of Parameter
88     * @param requestor Description of Parameter
89     * @param serviceClass Description of Parameter
90     * @param serviceSelector Description of Parameter
91     * @return The service value
92     */

93    public Object JavaDoc getService(BeanContextServices JavaDoc bcs, Object JavaDoc requestor, Class JavaDoc serviceClass, Object JavaDoc serviceSelector)
94    {
95       // Check if the requestor has already a service
96
if (!requestors.contains(requestor))
97       {
98          requestors.add(requestor);
99       }
100       return service;
101    }
102
103
104    /**
105     * Description of the Method
106     *
107     * @param action Description of Parameter
108     */

109    public void register(CommandAction action)
110    {
111       String JavaDoc menuName = action.getMenu();
112       ResourceBundle JavaDoc res = action.getResourceBundle();
113       JMenu JavaDoc parent = findMenu(menuName);
114
115       if (parent == null)
116       {
117          parent = addMenu(res, menuName);
118       }
119
120       addMenuItem(action, parent);
121    }
122
123
124    /**
125     * Description of the Method
126     *
127     * @param bcs Description of Parameter
128     * @param requestor Description of Parameter
129     * @param service Description of Parameter
130     */

131    public void releaseService(BeanContextServices JavaDoc bcs, Object JavaDoc requestor, Object JavaDoc service) { }
132
133
134    /**
135     * Description of the Method
136     *
137     * @param mbsl Description of Parameter
138     */

139    public void removeMenuBarListener(MenuBarService.Listener mbsl)
140    {
141       this.listeners.remove(mbsl);
142    }
143
144
145    /**
146     * Description of the Method
147     *
148     * @param action Description of Parameter
149     */

150    public void unregister(CommandAction action)
151    {
152       String JavaDoc menuName = action.getMenu();
153       JMenu JavaDoc parent = findMenu(menuName);
154
155       if (parent == null)
156       {
157          return;
158       }
159
160       removeMenuItem(action, parent);
161    }
162
163
164    /**
165     * Adds a feature to the Menu attribute of the MenuBarServiceProvider object
166     *
167     * @param menuName The feature to be added to the Menu attribute
168     * @param res The feature to be added to the Menu attribute
169     * @return Description of the Returned Value
170     */

171    protected JMenu JavaDoc addMenu(ResourceBundle JavaDoc res, String JavaDoc menuName)
172    {
173       JMenu JavaDoc menu = new JMenu JavaDoc(res.getString(menuName));
174       menu.setFont(AwtToolkit.FONT);
175       menu.setName(menuName);
176       try
177       {
178          menu.setMnemonic(Integer.parseInt(res.getString(menuName + ".mnemonic")));
179       }
180       catch (Exception JavaDoc e)
181       {
182       }
183       this.menuBar.add(menu);
184
185       this.update();
186
187       return menu;
188    }
189
190
191    /**
192     * Adds a feature to the MenuItem attribute of the MenuBarServiceProvider object
193     *
194     * @param action The feature to be added to the MenuItem attribute
195     * @param menu The feature to be added to the MenuItem attribute
196     */

197    protected void addMenuItem(CommandAction action, JMenu JavaDoc menu)
198    {
199       JMenuItem JavaDoc item = action.getMenuItem();
200
201       if (action.getMenuLayout() == MenuBarService.SEPARATOR_BEFORE)
202       {
203          if (menu.getItemCount() > 0)
204          {
205             menu.addSeparator();
206          }
207       }
208
209       JMenuItem JavaDoc addedItem = menu.add(item);
210       addedItem.setFont(AwtToolkit.FONT);
211
212       if (action.getValue(CommandAction.TOOLTIP) != null)
213       {
214          addedItem.setToolTipText((String JavaDoc) action.getValue(CommandAction.TOOLTIP));
215       }
216
217       if (action.getMenuLayout() == MenuBarService.SEPARATOR_AFTER)
218       {
219          menu.addSeparator();
220       }
221    }
222
223
224    /**
225     * Description of the Method
226     *
227     * @param menuName Description of Parameter
228     * @return Description of the Returned Value
229     */

230    protected JMenu JavaDoc findMenu(String JavaDoc menuName)
231    {
232       for (int i = 0; i < menuBar.getMenuCount(); i++)
233       {
234          JMenu JavaDoc menu = menuBar.getMenu(i);
235          if (menu.getName().equals(menuName))
236          {
237             return menu;
238          }
239       }
240
241       return null;
242    }
243
244
245    /**
246     * @return The serviceClass value
247     */

248    protected Class JavaDoc[] getServiceClass()
249    {
250       return new Class JavaDoc[]{MenuBarService.class};
251    }
252
253
254    /**
255     * Description of the Method
256     *
257     * @param action Description of Parameter
258     * @param menu Description of Parameter
259     */

260    protected void removeMenuItem(CommandAction action, JMenu JavaDoc menu)
261    {
262       JMenuItem JavaDoc item = action.getMenuItem();
263
264       menu.remove(item);
265    }
266
267
268    /** Description of the Method */
269    protected void update()
270    {
271       for (int i = 0; i < this.listeners.size(); i++)
272       {
273          MenuBarService.Listener o = (MenuBarService.Listener) this.listeners.elementAt(i);
274          o.update(this.menuBar);
275       }
276    }
277 }
278
279
Popular Tags