KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > gui > menu > ExtendableMenuBar


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.core.gui.menu;
19
20 import java.util.Enumeration JavaDoc;
21 import java.util.Hashtable JavaDoc;
22 import java.util.logging.Logger JavaDoc;
23
24 import javax.swing.JMenuBar JavaDoc;
25 import javax.swing.JMenuItem JavaDoc;
26
27 import org.columba.core.gui.action.AbstractColumbaAction;
28
29
30 /**
31  * Extendable menubar.
32  * <p>
33  * Simply example showing how-to add a new action to the menu:
34  * <pre>
35  * ColumbaMenu menu = (ColumbaMenu) frame.getJMenuBar();
36  * menu.addMenuItem("my_reply_action_id", new ReplyAction(this),
37  * ColumbaMenu.MENU_VIEW, ColumbaMenu.PLACEHOLDER_BOTTOM);
38  * </pre>
39  * @author fdietz
40  *
41  */

42 public class ExtendableMenuBar extends JMenuBar JavaDoc {
43
44     private static final Logger JavaDoc LOG = Logger
45             .getLogger("org.columba.core.gui.menu");
46
47     private Hashtable JavaDoc<String JavaDoc, ExtendableMenu> map = new Hashtable JavaDoc<String JavaDoc, ExtendableMenu>();
48
49     
50     public ExtendableMenuBar() {
51         super();
52     }
53     
54     public void add(ExtendableMenu menu) {
55         if ( menu == null ) throw new IllegalArgumentException JavaDoc("menu == null");
56         Enumeration JavaDoc<ExtendableMenu> e = menu.getSubmenuEnumeration();
57         while (e.hasMoreElements()) {
58             ExtendableMenu submenu = e.nextElement();
59             map.put(submenu.getId(), submenu);
60         }
61
62         super.add(menu);
63     }
64     
65     public void insert(ExtendableMenu menu) {
66         if ( menu == null ) throw new IllegalArgumentException JavaDoc("menu == null");
67         
68         Enumeration JavaDoc<ExtendableMenu> e = menu.getSubmenuEnumeration();
69         while (e.hasMoreElements()) {
70             ExtendableMenu submenu = e.nextElement();
71             map.put(submenu.getId(), submenu);
72         }
73         
74         // we insert new menus between the "Edit" and the "Utilities, Help" menu
75
super.add(menu, getMenuCount()-2);
76     }
77
78     public boolean exists(String JavaDoc menuId) {
79         if ( menuId == null ) throw new IllegalArgumentException JavaDoc("menuId == null");
80         
81         if ( map.containsKey(menuId)) return true;
82         
83         return false;
84     }
85     
86     public ExtendableMenu getMenu(String JavaDoc menuId) {
87         if ( menuId == null ) throw new IllegalArgumentException JavaDoc("menuId == null");
88         
89         if (map.containsKey(menuId) == false)
90             throw new IllegalArgumentException JavaDoc("no menu for "+menuId+" found");
91         
92
93         ExtendableMenu menu = (ExtendableMenu) map.get(menuId);
94
95         return menu;
96     }
97
98     public void insertMenuItem(String JavaDoc menuId, String JavaDoc placeholderId,
99             JMenuItem JavaDoc menuItem) {
100         if ( menuId == null ) throw new IllegalArgumentException JavaDoc("menuId == null");
101         
102         if (map.containsKey(menuId) == false)
103             throw new IllegalArgumentException JavaDoc("no menu with id " + menuId
104                     + " found");
105
106         ExtendableMenu menu = (ExtendableMenu) map.get(menuId);
107         menu.insert(menuItem, placeholderId);
108     }
109
110     public void insertAction(String JavaDoc menuId, String JavaDoc placeholderId,
111             AbstractColumbaAction action) {
112         if ( menuId == null ) throw new IllegalArgumentException JavaDoc("menuId == null");
113         
114         if (map.containsKey(menuId) == false)
115             throw new IllegalArgumentException JavaDoc("no menu with id " + menuId
116                     + " found");
117
118         ExtendableMenu menu = (ExtendableMenu) map.get(menuId);
119         menu.insert(action, placeholderId);
120     /*TODO before inserting, find out if there's already a menu item
121      * with the same action command. if so, replace it, otherwise insert new
122      */

123     }
124
125 }
126
Popular Tags