001: /*
002: * MenuBuilder.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.underworldlabs.swing.util;
023:
024: import javax.swing.JMenu;
025: import javax.swing.JMenuItem;
026: import javax.swing.ImageIcon;
027: import javax.swing.JRadioButtonMenuItem;
028: import javax.swing.JCheckBoxMenuItem;
029: import javax.swing.Action;
030:
031: /* ----------------------------------------------------------
032: * CVS NOTE: Changes to the CVS repository prior to the
033: * release of version 3.0.0beta1 has meant a
034: * resetting of CVS revision numbers.
035: * ----------------------------------------------------------
036: */
037:
038: /**
039: * A helper class for creating menu items.
040: *
041: * @author Takis Diakoumis
042: * @version $Revision: 1.5 $
043: * @date $Date: 2006/05/29 15:23:58 $
044: */
045: public class MenuBuilder {
046:
047: /** A <code>JMenuItem</code> menu item */
048: public static final int ITEM_PLAIN = 0;
049:
050: /** A <code>JCheckBoxMenuItem</code> menu item */
051: public static final int ITEM_CHECK = 1;
052:
053: /** A <code>JRadioButtonMenuItem</code> menu item */
054: public static final int ITEM_RADIO = 2;
055:
056: /**
057: * Creates a new <code>MenuBuilder</code> object
058: */
059: public MenuBuilder() {
060: }
061:
062: public JMenuItem createMenuItem(JMenu menu, int iType,
063: String sText, ImageIcon image, int acceleratorKey,
064: String sToolTip, Action a) {
065: return createMenuItem(menu, iType, sText, image,
066: acceleratorKey, sToolTip, a, null);
067: }
068:
069: /**
070: * Creates the required menu item.<br><p>
071: * The menu item is associated with a <code>JMenu</code>,
072: * a name, an image icon, a key mnemonic and a tool tip.
073: *
074: * @param menu The <code>JMenu</code> that this menu item
075: * will be associated with
076: * @param iType The type of menu item to be constructed
077: * (see public fields)
078: * @param sText The name of this menu item
079: * @param image The image icon to be displayed with this menu item
080: * @param acceleratorKey The keyboard mnemonic associated with the
081: * menu item
082: * @param sToolTip The tool tip associated with the menu item
083: *
084: * @return The constructed <code>JMenuItem</code>
085: */
086: public JMenuItem createMenuItem(JMenu menu, int iType,
087: String sText, ImageIcon image, int acceleratorKey,
088: String sToolTip, Action a, String actionCommand) {
089:
090: JMenuItem menuItem;
091: switch (iType) {
092: case ITEM_RADIO:
093: menuItem = new JRadioButtonMenuItem();
094: break;
095: case ITEM_CHECK:
096: menuItem = new JCheckBoxMenuItem();
097: break;
098: default:
099: menuItem = new JMenuItem();
100: break;
101: }
102:
103: if (a != null) {
104: menuItem.setAction(a);
105: }
106:
107: if (acceleratorKey > 0) {
108: menuItem.setMnemonic(acceleratorKey);
109: }
110:
111: if (sToolTip != null) {
112: menuItem.setToolTipText(sToolTip);
113: }
114:
115: if (actionCommand != null) {
116: menuItem.setActionCommand(actionCommand);
117: }
118:
119: menuItem.setIcon(image);
120: menuItem.setText(sText);
121:
122: if (menu != null) {
123: menu.add(menuItem);
124: }
125:
126: return menuItem;
127: }
128:
129: public JMenuItem createMenuItem(JMenu menu, int iType, Action action) {
130: return createMenuItem(menu, iType, (String) action
131: .getValue(Action.NAME), null, ((Integer) action
132: .getValue(Action.MNEMONIC_KEY)).intValue(),
133: (String) action.getValue(Action.SHORT_DESCRIPTION),
134: action, null);
135: }
136:
137: public JMenuItem createMenuItem(JMenu menu, String sText,
138: int iType, String sToolTip) {
139: return createMenuItem(menu, iType, sText, null, -1, sToolTip,
140: null);
141: }
142:
143: public JMenuItem createMenuItem(JMenu menu, int iType,
144: int acceleratorKey, String sToolTip, Action a) {
145: return createMenuItem(menu, iType, null, null, acceleratorKey,
146: sToolTip, a, null);
147: }
148:
149: public JMenuItem createMenuItem(JMenu menu, int iType,
150: String sToolTip, Action a) {
151: return createMenuItem(menu, iType, null, null, 0, sToolTip, a,
152: null);
153: }
154:
155: /**
156: * Creates the required menu.<br>
157: * The menu item is initialised with a name and
158: * key mnemonic.
159: *
160: * @param sText The name of this menu
161: * @param acceleratorKey The keyboard mnemonic associated
162: * with this menu
163: *
164: * @return The constructed <code>JMenu</code>
165: */
166: public JMenu createMenu(String sText, int acceleratorKey) {
167: JMenu menu = new JMenu(sText);
168: // menu.setOpaque(true);
169: if (acceleratorKey > 0) {
170: menu.setMnemonic(acceleratorKey);
171: }
172: return menu;
173: }
174:
175: }
|