001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.core.gui.menu;
019:
020: import java.util.Enumeration;
021: import java.util.Hashtable;
022: import java.util.logging.Logger;
023:
024: import javax.swing.JMenuBar;
025: import javax.swing.JMenuItem;
026:
027: import org.columba.core.gui.action.AbstractColumbaAction;
028:
029: /**
030: * Extendable menubar.
031: * <p>
032: * Simply example showing how-to add a new action to the menu:
033: * <pre>
034: * ColumbaMenu menu = (ColumbaMenu) frame.getJMenuBar();
035: * menu.addMenuItem("my_reply_action_id", new ReplyAction(this),
036: * ColumbaMenu.MENU_VIEW, ColumbaMenu.PLACEHOLDER_BOTTOM);
037: * </pre>
038: * @author fdietz
039: *
040: */
041: public class ExtendableMenuBar extends JMenuBar {
042:
043: private static final Logger LOG = Logger
044: .getLogger("org.columba.core.gui.menu");
045:
046: private Hashtable<String, ExtendableMenu> map = new Hashtable<String, ExtendableMenu>();
047:
048: public ExtendableMenuBar() {
049: super ();
050: }
051:
052: public void add(ExtendableMenu menu) {
053: if (menu == null)
054: throw new IllegalArgumentException("menu == null");
055: Enumeration<ExtendableMenu> e = menu.getSubmenuEnumeration();
056: while (e.hasMoreElements()) {
057: ExtendableMenu submenu = e.nextElement();
058: map.put(submenu.getId(), submenu);
059: }
060:
061: super .add(menu);
062: }
063:
064: public void insert(ExtendableMenu menu) {
065: if (menu == null)
066: throw new IllegalArgumentException("menu == null");
067:
068: Enumeration<ExtendableMenu> e = menu.getSubmenuEnumeration();
069: while (e.hasMoreElements()) {
070: ExtendableMenu submenu = e.nextElement();
071: map.put(submenu.getId(), submenu);
072: }
073:
074: // we insert new menus between the "Edit" and the "Utilities, Help" menu
075: super .add(menu, getMenuCount() - 2);
076: }
077:
078: public boolean exists(String menuId) {
079: if (menuId == null)
080: throw new IllegalArgumentException("menuId == null");
081:
082: if (map.containsKey(menuId))
083: return true;
084:
085: return false;
086: }
087:
088: public ExtendableMenu getMenu(String menuId) {
089: if (menuId == null)
090: throw new IllegalArgumentException("menuId == null");
091:
092: if (map.containsKey(menuId) == false)
093: throw new IllegalArgumentException("no menu for " + menuId
094: + " found");
095:
096: ExtendableMenu menu = (ExtendableMenu) map.get(menuId);
097:
098: return menu;
099: }
100:
101: public void insertMenuItem(String menuId, String placeholderId,
102: JMenuItem menuItem) {
103: if (menuId == null)
104: throw new IllegalArgumentException("menuId == null");
105:
106: if (map.containsKey(menuId) == false)
107: throw new IllegalArgumentException("no menu with id "
108: + menuId + " found");
109:
110: ExtendableMenu menu = (ExtendableMenu) map.get(menuId);
111: menu.insert(menuItem, placeholderId);
112: }
113:
114: public void insertAction(String menuId, String placeholderId,
115: AbstractColumbaAction action) {
116: if (menuId == null)
117: throw new IllegalArgumentException("menuId == null");
118:
119: if (map.containsKey(menuId) == false)
120: throw new IllegalArgumentException("no menu with id "
121: + menuId + " found");
122:
123: ExtendableMenu menu = (ExtendableMenu) map.get(menuId);
124: menu.insert(action, placeholderId);
125: /*TODO before inserting, find out if there's already a menu item
126: * with the same action command. if so, replace it, otherwise insert new
127: */
128: }
129:
130: }
|