Source code: org/jext/menus/JextMenuBar.java
1 /*
2 * 11/25/2000 - 18:13:12
3 *
4 * JextMenuBar.java - Extended JMenuBar
5 * Copyright (C) 1999 Romain Guy
6 * romain.guy@jext.org
7 * www.jext.org
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24 package org.jext.menus;
25
26 import java.util.Hashtable;
27
28 import java.awt.Container;
29
30 import javax.swing.JMenu;
31 import javax.swing.JMenuBar;
32 import javax.swing.JMenuItem;
33 import javax.swing.JSeparator;
34 import javax.swing.MenuElement;
35
36 import org.jext.Jext;
37 import org.jext.JextFrame;
38 import org.jext.gui.JextMenu;
39 import org.jext.gui.JextMenuSeparator;
40
41 /**
42 * The menu bar of Jext windows. This menu bar allows
43 * to add items and menus according to a position indicated
44 * by a menu ID. Indeed, this class allows to register menus
45 * with an ID.<br>Using IDs allows, for instance, to add
46 * plugins submenus in other menus than in Plugins.
47 * (see Java and HTML plugins to see how this works).
48 * @author Romain Guy
49 */
50
51 public class JextMenuBar extends JMenuBar
52 {
53 private Hashtable menus = new Hashtable();
54 private int fileMenusAdded, fileItemsAdded;
55 private int editMenusAdded, editItemsAdded;
56
57 /**
58 * Creates a new menu bar.
59 */
60
61 public JextMenuBar()
62 {
63 super();
64 }
65
66 /**
67 * Adds a menu in the menu bar and register it with
68 * an identifier which can be used later to add items
69 * or submenus in this menu.
70 * @param menu The menu to be added
71 * @param ID The identification string for this menu
72 */
73
74 public void addIdentifiedMenu(JMenu menu, String ID)
75 {
76 if (menus.containsKey(ID))
77 System.err.println("JextMenuBar: There is already a menu with ID '" + ID + "'!");
78
79 menus.put(ID, menu);
80 add(menu);
81 JextFrame frame = getJextFrame();
82 if (frame != null)
83 frame.itemAdded(menu);
84 }
85
86 /**
87 * Adds a submenu in a menu designed by an ID string.
88 * @param item The menu to be added
89 * @param ID The ID string of the menu in which item has to be added
90 */
91
92 public void addMenu(JMenu item, String ID)
93 {
94 int pos = -1;
95 JMenu _menu = (JMenu) menus.get(ID);
96 if (_menu == null)
97 return;
98
99 if (ID.equals("Edit"))
100 {
101 pos = 13 + editMenusAdded;
102 editMenusAdded++;
103 } else if (ID.equals("File")) {
104 pos = 22 + fileMenusAdded;
105 fileMenusAdded++;
106 }
107
108 if (pos == -1)
109 {
110 if (!(_menu.getMenuComponent(_menu.getItemCount() - 2) instanceof JSeparator))
111 {
112 if (Jext.getFlatMenus())
113 _menu.getPopupMenu().add(new JextMenuSeparator());
114 else
115 _menu.getPopupMenu().addSeparator();
116 }
117 _menu.add(item);
118 } else
119 _menu.insert(item, pos);
120
121 JextFrame frame = getJextFrame();
122 if (frame != null)
123 frame.itemAdded(item);
124 }
125
126 /**
127 * Adds an item in a menu designed by an ID string.
128 * @param item The menu item to be added
129 * @param ID The ID string of the menu in which item has to be added
130 */
131
132 public void addMenuItem(JMenuItem item, String ID)
133 {
134 int pos = -1;
135 JMenu _menu = (JMenu) menus.get(ID);
136 if (_menu == null)
137 return;
138 if (ID.equals("Edit"))
139 {
140 pos = 16 + editMenusAdded + editItemsAdded;
141 editItemsAdded++;
142 } else if (ID.equals("File")) {
143 pos = 22 + fileMenusAdded + fileItemsAdded;
144 fileItemsAdded++;
145 }
146
147 if (pos == -1)
148 {
149 if (!(_menu.getMenuComponent(_menu.getItemCount() - 2) instanceof JSeparator))
150 {
151 if (Jext.getFlatMenus())
152 _menu.getPopupMenu().add(new JextMenuSeparator());
153 else
154 _menu.getPopupMenu().addSeparator();
155 }
156 _menu.add(item);
157 } else {
158 if (fileItemsAdded == 1)
159 {
160 if (Jext.getFlatMenus())
161 _menu.getPopupMenu().insert(new JextMenuSeparator(), pos);
162 else
163 _menu.getPopupMenu().insert(new JSeparator(), pos);
164 }
165 _menu.insert(item, pos);
166 }
167
168 JextFrame frame = getJextFrame();
169 if (frame != null)
170 frame.itemAdded(item);
171 }
172
173 /**
174 * Restores the menu bar.
175 */
176
177 public void reset()
178 {
179 fileMenusAdded = 0;
180 fileItemsAdded = 0;
181 editMenusAdded = 0;
182 editItemsAdded = 0;
183 }
184
185 /**
186 * Get the parent JextFrame
187 * @return org.jext.JextFrame
188 */
189
190 private JextFrame getJextFrame()
191 {
192 Container parent;
193 parent = getParent(); // JLayeredPane
194 if (parent != null)
195 parent = parent.getParent(); // JRootPane
196 if (parent != null)
197 parent = parent.getParent(); // JextFrame
198 return (JextFrame) parent;
199 }
200
201 /***************************************************************************
202 Patch
203 -> Memory management improvements : it may help the garbage collector.
204 -> Author : Julien Ponge (julien@izforge.com)
205 -> Date : 23, May 2001
206 ***************************************************************************/
207 protected void finalize() throws Throwable
208 {
209 super.finalize();
210
211 menus.clear();
212 }
213 // End of patch
214
215 }
216
217 // End of JextMenuBar