MenuBar.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /* MenuBar.java -- An AWT menu bar class
  2. Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.awt;
  32. import java.awt.peer.MenuBarPeer;
  33. import java.awt.peer.MenuComponentPeer;
  34. import java.io.Serializable;
  35. import java.util.Enumeration;
  36. import java.util.Vector;
  37. /**
  38. * This class implements a menu bar in the AWT system.
  39. *
  40. * @author Aaron M. Renn (arenn@urbanophile.com)
  41. * @author Tom Tromey <tromey@redhat.com>
  42. */
  43. public class MenuBar extends MenuComponent
  44. implements MenuContainer, Serializable
  45. {
  46. /*
  47. * Static Variables
  48. */
  49. // Serialization Constant
  50. private static final long serialVersionUID = -4930327919388951260L;
  51. /*************************************************************************/
  52. /*
  53. * Instance Variables
  54. */
  55. /**
  56. * @serial The menu used for providing help information
  57. */
  58. private Menu helpMenu;
  59. /**
  60. * @serial The menus contained in this menu bar.
  61. */
  62. private Vector menus = new Vector();
  63. /*************************************************************************/
  64. /*
  65. * Constructors
  66. */
  67. /**
  68. * Initializes a new instance of <code>MenuBar</code>.
  69. *
  70. * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
  71. */
  72. public
  73. MenuBar()
  74. {
  75. if (GraphicsEnvironment.isHeadless())
  76. throw new HeadlessException ();
  77. }
  78. /*************************************************************************/
  79. /*
  80. * Instance Methods
  81. */
  82. /**
  83. * Returns the help menu for this menu bar. This may be <code>null</code>.
  84. *
  85. * @return The help menu for this menu bar.
  86. */
  87. public Menu
  88. getHelpMenu()
  89. {
  90. return(helpMenu);
  91. }
  92. /*************************************************************************/
  93. /**
  94. * Sets the help menu for this menu bar.
  95. *
  96. * @param helpMenu The new help menu for this menu bar.
  97. */
  98. public synchronized void
  99. setHelpMenu(Menu menu)
  100. {
  101. if (helpMenu != null)
  102. {
  103. helpMenu.removeNotify ();
  104. helpMenu.parent = null;
  105. }
  106. if (menu.parent != null)
  107. menu.parent.remove (menu);
  108. if (menu.parent != null)
  109. menu.parent.remove (menu);
  110. menu.parent = this;
  111. if (peer != null)
  112. {
  113. MenuBarPeer mp = (MenuBarPeer) peer;
  114. mp.addHelpMenu (menu);
  115. }
  116. }
  117. /*************************************************************************/
  118. /** Add a menu to this MenuBar. If the menu has already has a
  119. * parent, it is first removed from its old parent before being
  120. * added.
  121. *
  122. * @param menu The menu to add.
  123. *
  124. * @return The menu that was added.
  125. */
  126. public synchronized Menu
  127. add(Menu menu)
  128. {
  129. if (menu.parent != null)
  130. menu.parent.remove (menu);
  131. menu.parent = this;
  132. menus.addElement(menu);
  133. if (peer != null)
  134. {
  135. MenuBarPeer mp = (MenuBarPeer) peer;
  136. mp.addMenu (menu);
  137. }
  138. return(menu);
  139. }
  140. /*************************************************************************/
  141. /**
  142. * Removes the menu at the specified index.
  143. *
  144. * @param index The index of the menu to remove from the menu bar.
  145. */
  146. public synchronized void
  147. remove(int index)
  148. {
  149. Menu m = (Menu) menus.get (index);
  150. menus.remove (index);
  151. m.removeNotify ();
  152. m.parent = null;
  153. if (peer != null)
  154. {
  155. MenuBarPeer mp = (MenuBarPeer) peer;
  156. mp.delMenu (index);
  157. }
  158. }
  159. /*************************************************************************/
  160. /**
  161. * Removes the specified menu from the menu bar.
  162. *
  163. * @param menu The menu to remove from the menu bar.
  164. */
  165. public void
  166. remove(MenuComponent menu)
  167. {
  168. int index = menus.indexOf(menu);
  169. if (index == -1)
  170. return;
  171. remove(index);
  172. }
  173. /*************************************************************************/
  174. /**
  175. * Returns the number of elements in this menu bar.
  176. *
  177. * @return The number of elements in the menu bar.
  178. */
  179. public int
  180. getMenuCount()
  181. {
  182. // FIXME: How does the help menu fit in here?
  183. return(menus.size());
  184. }
  185. /*************************************************************************/
  186. /**
  187. * Returns the number of elements in this menu bar.
  188. *
  189. * @return The number of elements in the menu bar.
  190. *
  191. * @deprecated This method is deprecated in favor of <code>getMenuCount()</code>.
  192. */
  193. public int
  194. countMenus()
  195. {
  196. return(getMenuCount());
  197. }
  198. /*************************************************************************/
  199. /**
  200. * Returns the menu at the specified index.
  201. *
  202. * @return The requested menu.
  203. *
  204. * @exception ArrayIndexOutOfBoundsException If the index is not valid.
  205. */
  206. public Menu
  207. getMenu(int index)
  208. {
  209. return((Menu)menus.elementAt(index));
  210. }
  211. /*************************************************************************/
  212. /**
  213. * Creates this object's native peer.
  214. */
  215. public void
  216. addNotify()
  217. {
  218. if (getPeer() == null)
  219. setPeer((MenuComponentPeer)getToolkit().createMenuBar(this));
  220. }
  221. /*************************************************************************/
  222. /**
  223. * Destroys this object's native peer.
  224. */
  225. public void
  226. removeNotify()
  227. {
  228. super.removeNotify();
  229. }
  230. /*************************************************************************/
  231. /**
  232. * Returns a list of all shortcuts for the menus in this menu bar.
  233. *
  234. * @return A list of all shortcuts for the menus in this menu bar.
  235. */
  236. public synchronized Enumeration
  237. shortcuts()
  238. {
  239. Vector shortcuts = new Vector();
  240. Enumeration e = menus.elements();
  241. while (e.hasMoreElements())
  242. {
  243. Menu menu = (Menu)e.nextElement();
  244. if (menu.getShortcut() != null)
  245. shortcuts.addElement(menu.getShortcut());
  246. }
  247. return(shortcuts.elements());
  248. }
  249. /*************************************************************************/
  250. /**
  251. * Returns the menu item for the specified shortcut, or <code>null</code>
  252. * if no such item exists.
  253. *
  254. * @param shortcut The shortcut to return the menu item for.
  255. *
  256. * @return The menu item for the specified shortcut.
  257. */
  258. public MenuItem
  259. getShortcutMenuItem(MenuShortcut shortcut)
  260. {
  261. Enumeration e = menus.elements();
  262. while (e.hasMoreElements())
  263. {
  264. Menu menu = (Menu)e.nextElement();
  265. MenuShortcut s = menu.getShortcut();
  266. if ((s != null) && (s.equals(shortcut)))
  267. return(menu);
  268. }
  269. return(null);
  270. }
  271. /*************************************************************************/
  272. /**
  273. * Deletes the specified menu shortcut.
  274. *
  275. * @param shortcut The shortcut to delete.
  276. */
  277. public void
  278. deleteShortcut(MenuShortcut shortcut)
  279. {
  280. MenuItem it;
  281. // This is a slow implementation, but it probably doesn't matter.
  282. while ((it = getShortcutMenuItem (shortcut)) != null)
  283. it.deleteShortcut ();
  284. }
  285. } // class MenuBar