1 /*
2 * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3 * the spring framework, http://el4j.sf.net
4 * Copyright (C) 2008 by ELCA Informatique SA, Av. de la Harpe 22-24,
5 * 1000 Lausanne, Switzerland, http://www.elca.ch
6 *
7 * EL4J is published under the GNU Lesser General Public License (LGPL)
8 * Version 2.1. See http://www.gnu.org/licenses/
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * For alternative licensing, please contact info@elca.ch
16 */
17 package ch.elca.el4j.services.gui.swing.util;
18
19 import java.util.Collection;
20
21 import javax.swing.JComponent;
22 import javax.swing.JMenu;
23 import javax.swing.JMenuItem;
24 import javax.swing.JPopupMenu;
25 import javax.swing.JSeparator;
26
27 import ch.elca.el4j.services.gui.swing.ActionsContext;
28
29 /**
30 * This utility class helps to create menus using Sun's appFramework.
31 *
32 * @svnLink $Revision: 3880 $;$Date: 2009-08-04 15:17:52 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/swing/src/main/java/ch/elca/el4j/services/gui/swing/util/MenuUtils.java $
33 *
34 * @author Stefan Wismer (SWI)
35 */
36 public final class MenuUtils {
37
38 /**
39 * The hidden constructor.
40 */
41 private MenuUtils() { }
42
43 /**
44 * Creates a JMenu out of a String array containing action names. A
45 * separator is represented by the string "---"
46 *
47 * @param context the actions context
48 * @param menuName the menu name
49 * @param actionNames the collection of menu items
50 * @return a JMenu
51 */
52 public static JMenu createMenu(ActionsContext context, String menuName, Collection<String> actionNames) {
53 JMenu menu = new JMenu();
54 menu.setName(menuName);
55 return initMenu(context, actionNames, menu);
56 }
57
58 /**
59 * Creates a JPopupMenu out of a String array containing action names. A
60 * separator is represented by the string "---"
61 *
62 * @param context the actions context
63 * @param actionNames the collection of menu items
64 * @return a JPopupMenu
65 */
66 public static JPopupMenu createPopup(ActionsContext context, Collection<String> actionNames) {
67 JPopupMenu menu = new JPopupMenu();
68 return initMenu(context, actionNames, menu);
69 }
70
71 /**
72 * Fills a menu with menu items.
73 *
74 * @param <T> the menu type (e.g. JMenu, JPopupMenu)
75 * @param context the actions context
76 * @param actionNames the collection of menu items
77 * @param menu the menu to insert the items
78 * @return a menu
79 */
80 private static <T extends JComponent> T initMenu(ActionsContext context, Collection<String> actionNames, T menu) {
81 for (String actionName : actionNames) {
82 if (actionName.equals("---")) {
83 menu.add(new JSeparator());
84 } else {
85 JMenuItem menuItem = new JMenuItem();
86 menuItem.setAction(context.getAction(actionName));
87 //menuItem.setIcon(null);
88 menu.add(menuItem);
89 }
90 }
91 return menu;
92 }
93 }