001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: JMenuBuilder.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.swing;
009:
010: import javax.swing.*;
011:
012: import java.awt.MenuContainer;
013: import java.awt.event.ActionEvent;
014: import java.awt.event.ItemEvent;
015: import java.awt.event.ItemListener;
016:
017: public class JMenuBuilder {
018: private static final ItemActionEventBridge ITEM_ACTION_EVENT_BRIDGE = new ItemActionEventBridge();
019:
020: private static class ItemActionEventBridge implements ItemListener {
021: public void itemStateChanged(ItemEvent event) {
022: if (ItemEvent.SELECTED == event.getStateChange()) {
023: JMenuItem menuitem = (JMenuItem) event.getSource();
024: Action action = menuitem.getAction();
025: if (action != null) {
026: action.actionPerformed(new ActionEvent(event
027: .getSource(), ActionEvent.ACTION_PERFORMED,
028: menuitem.getActionCommand()));
029: }
030: }
031: }
032: }
033:
034: public static JMenu addMenu(MenuContainer parentMenu,
035: String menuLabel) {
036: return addMenu(parentMenu, menuLabel, (char) 0);
037: }
038:
039: public static JMenu addMenu(MenuContainer parentMenu,
040: String menuLabel, char mnemonic) {
041: if (null == parentMenu)
042: throw new IllegalArgumentException(
043: "parentMenu can't be null.");
044: if (null == menuLabel)
045: throw new IllegalArgumentException(
046: "menuLabel can't be null.");
047: if (0 == menuLabel.length())
048: throw new IllegalArgumentException(
049: "menuLabel can't be empty.");
050:
051: JMenu menu = new JMenu(menuLabel);
052: if (0 != mnemonic) {
053: menu.setMnemonic(mnemonic);
054: }
055:
056: ((JComponent) parentMenu).add(menu);
057:
058: return menu;
059: }
060:
061: public static JMenuItem addMenuItem(MenuContainer parentMenu,
062: Action action) {
063: if (null == action)
064: throw new IllegalArgumentException("action can't be null.");
065:
066: return processMenuItem(new JMenuItem(action), parentMenu, null);
067: }
068:
069: public static JCheckBoxMenuItem addCheckBoxMenuItem(
070: MenuContainer parentMenu, Action action) {
071: if (null == action)
072: throw new IllegalArgumentException("action can't be null.");
073:
074: return (JCheckBoxMenuItem) processMenuItem(
075: new JCheckBoxMenuItem(action), parentMenu, null);
076: }
077:
078: public static JCheckBoxMenuItem addCheckBoxMenuItem(
079: MenuContainer parentMenu, ButtonGroup group, Action action) {
080: if (null == action)
081: throw new IllegalArgumentException("action can't be null.");
082:
083: return (JCheckBoxMenuItem) processMenuItem(
084: new JCheckBoxMenuItem(action), parentMenu, null);
085: }
086:
087: public static JRadioButtonMenuItem addRadioButtonMenuItem(
088: MenuContainer parentMenu, Action action) {
089: if (null == action)
090: throw new IllegalArgumentException("action can't be null.");
091:
092: return (JRadioButtonMenuItem) processMenuItem(
093: new JRadioButtonMenuItem(action), parentMenu, null);
094: }
095:
096: public static JRadioButtonMenuItem addRadioButtonMenuItem(
097: MenuContainer parentMenu, ButtonGroup group, Action action) {
098: if (null == action)
099: throw new IllegalArgumentException("action can't be null.");
100:
101: return (JRadioButtonMenuItem) processMenuItem(
102: new JRadioButtonMenuItem(action), parentMenu, group);
103: }
104:
105: private static JMenuItem processMenuItem(JMenuItem menuItem,
106: MenuContainer parentMenu, ButtonGroup group) {
107: if (null == menuItem)
108: throw new IllegalArgumentException(
109: "menuItem can't be null.");
110: if (null == parentMenu)
111: throw new IllegalArgumentException(
112: "parentMenu can't be null.");
113:
114: ((JComponent) parentMenu).add(menuItem);
115: menuItem.addItemListener(ITEM_ACTION_EVENT_BRIDGE);
116:
117: if (group != null) {
118: group.add(menuItem);
119: }
120:
121: return menuItem;
122: }
123: }
|