Menu.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /* Menu.java -- A Java AWT Menu
  2. Copyright (C) 1999, 2002, 2004, 2006 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., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 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.MenuPeer;
  33. import java.io.Serializable;
  34. import java.util.Enumeration;
  35. import java.util.Vector;
  36. import javax.accessibility.AccessibleContext;
  37. import javax.accessibility.AccessibleRole;
  38. /**
  39. * This class represents a pull down or tear off menu in Java's AWT.
  40. *
  41. * @author Aaron M. Renn (arenn@urbanophile.com)
  42. */
  43. public class Menu extends MenuItem implements MenuContainer, Serializable
  44. {
  45. /**
  46. * The number used to generate the name returned by getName.
  47. */
  48. private static transient long next_menu_number;
  49. // Serialization Constant
  50. private static final long serialVersionUID = -8809584163345499784L;
  51. /**
  52. * @serial The actual items in the menu
  53. */
  54. private Vector items = new Vector();
  55. /**
  56. * @serial Flag indicating whether or not this menu is a tear off
  57. */
  58. private boolean tearOff;
  59. /**
  60. * @serial Indicates whether or not this is a help menu.
  61. */
  62. private boolean isHelpMenu;
  63. /*
  64. * @serial Unused in this implementation, but present in Sun's
  65. * serialization spec. Value obtained via reflection.
  66. */
  67. private int menuSerializedDataVersion = 1;
  68. static final transient String separatorLabel = "-";
  69. /**
  70. * Initializes a new instance of <code>Menu</code> with no label and that
  71. * is not a tearoff;
  72. *
  73. * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
  74. */
  75. public Menu()
  76. {
  77. }
  78. /**
  79. * Initializes a new instance of <code>Menu</code> that is not a tearoff and
  80. * that has the specified label.
  81. *
  82. * @param label The menu label.
  83. *
  84. * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
  85. */
  86. public Menu(String label)
  87. {
  88. this(label, false);
  89. }
  90. /**
  91. * Initializes a new instance of <code>Menu</code> with the specified
  92. * label and tearoff status.
  93. *
  94. * @param label The label for this menu
  95. * @param isTearOff <code>true</code> if this menu is a tear off menu,
  96. * <code>false</code> otherwise.
  97. *
  98. * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
  99. */
  100. public Menu(String label, boolean isTearOff)
  101. {
  102. super(label);
  103. tearOff = isTearOff;
  104. if (label.equals("Help"))
  105. isHelpMenu = true;
  106. if (GraphicsEnvironment.isHeadless())
  107. throw new HeadlessException();
  108. }
  109. /**
  110. * Tests whether or not this menu is a tearoff.
  111. *
  112. * @return <code>true</code> if this menu is a tearoff, <code>false</code>
  113. * otherwise.
  114. */
  115. public boolean isTearOff()
  116. {
  117. return(tearOff);
  118. }
  119. /**
  120. * Returns the number of items in this menu.
  121. *
  122. * @return The number of items in this menu.
  123. */
  124. public int getItemCount()
  125. {
  126. return countItems();
  127. }
  128. /**
  129. * Returns the number of items in this menu.
  130. *
  131. * @return The number of items in this menu.
  132. *
  133. * @deprecated As of JDK 1.1, replaced by getItemCount().
  134. */
  135. public int countItems()
  136. {
  137. return items.size();
  138. }
  139. /**
  140. * Returns the item at the specified index.
  141. *
  142. * @param index the item index.
  143. *
  144. * @return The item at the specified index.
  145. *
  146. * @exception ArrayIndexOutOfBoundsException If the index value is not valid.
  147. */
  148. public MenuItem getItem(int index)
  149. {
  150. return((MenuItem) items.elementAt(index));
  151. }
  152. /**
  153. * Adds the specified item to this menu. If it was previously part of
  154. * another menu, it is first removed from that menu.
  155. *
  156. * @param item The new item to add.
  157. *
  158. * @return The item that was added.
  159. */
  160. public MenuItem add(MenuItem item)
  161. {
  162. MenuContainer parent = item.getParent();
  163. if (parent != null)
  164. parent.remove(item);
  165. items.addElement(item);
  166. item.setParent(this);
  167. if (peer != null)
  168. {
  169. item.addNotify();
  170. MenuPeer mp = (MenuPeer) peer;
  171. mp.addItem(item);
  172. }
  173. return item;
  174. }
  175. /**
  176. * Add an item with the specified label to this menu.
  177. *
  178. * @param label The label of the menu item to add.
  179. */
  180. public void add(String label)
  181. {
  182. add(new MenuItem(label));
  183. }
  184. /**
  185. * Inserts the specified menu item into this menu at the specified index. If
  186. * the index is greater than or equal to the number of items already in the
  187. * menu, the new item is added as the last item in the menu.
  188. *
  189. * @param item The menu item to add (<code>null</code> not permitted).
  190. * @param index The index of the menu item (>= 0).
  191. *
  192. * @throws IllegalArgumentException if the index is less than zero.
  193. * @throws NullPointerException if <code>item</code> is <code>null</code>.
  194. */
  195. public void insert(MenuItem item, int index)
  196. {
  197. if (index < 0)
  198. throw new IllegalArgumentException("Index is less than zero");
  199. int count = getItemCount();
  200. if (index >= count)
  201. add(item);
  202. else
  203. {
  204. MenuContainer parent = item.getParent();
  205. if (parent != null)
  206. parent.remove(item);
  207. items.insertElementAt(item, index);
  208. item.setParent(this);
  209. MenuPeer peer = (MenuPeer) getPeer();
  210. if (peer == null)
  211. return;
  212. for (int i = count - 1; i >= index; i--)
  213. peer.delItem(i);
  214. item.addNotify();
  215. peer.addItem(item);
  216. // bear in mind that count is the number of items *before* the new
  217. // item was added
  218. for (int i = index + 1; i <= count; i++)
  219. peer.addItem((MenuItem) items.elementAt(i));
  220. }
  221. }
  222. /**
  223. * Inserts an item with the specified label into this menu at the specified
  224. * index. If the index is greater than or equal to the number of items
  225. * already in the menu, the new item is added as the last item in the menu.
  226. *
  227. * @param label The label of the item to add.
  228. * @param index The index of the menu item (>= 0).
  229. *
  230. * @throws IllegalArgumentException If the index is less than zero.
  231. */
  232. public void insert(String label, int index)
  233. {
  234. insert(new MenuItem(label), index);
  235. }
  236. /**
  237. * Adds a separator bar at the current menu location.
  238. */
  239. public void addSeparator()
  240. {
  241. add(new MenuItem(separatorLabel));
  242. }
  243. /**
  244. * Inserts a separator bar at the specified index value.
  245. *
  246. * @param index The index at which to insert a separator bar.
  247. *
  248. * @exception IllegalArgumentException If the index is less than zero.
  249. * @exception ArrayIndexOutOfBoundsException If the index is otherwise invalid.
  250. */
  251. public void insertSeparator(int index)
  252. {
  253. insert(new MenuItem(separatorLabel), index);
  254. }
  255. /**
  256. * Deletes the item at the specified index from this menu.
  257. *
  258. * @param index The index of the item to remove.
  259. *
  260. * @exception ArrayIndexOutOfBoundsException If the index is otherwise invalid.
  261. */
  262. public synchronized void remove(int index)
  263. {
  264. MenuItem item = (MenuItem) items.remove(index);
  265. MenuPeer mp = (MenuPeer) getPeer();
  266. if (mp != null)
  267. {
  268. mp.delItem(index);
  269. item.removeNotify();
  270. }
  271. item.setParent(null);
  272. }
  273. /**
  274. * Removes the specifed item from the menu. If the specified component
  275. * does not exist, this method does nothing.
  276. *
  277. * @param item The component to remove.
  278. */
  279. public void remove(MenuComponent item)
  280. {
  281. int index = items.indexOf(item);
  282. if (index == -1)
  283. return;
  284. remove(index);
  285. }
  286. /**
  287. * Removes all the elements from this menu.
  288. */
  289. public synchronized void removeAll()
  290. {
  291. int count = getItemCount();
  292. for(int i = 0; i < count; i++)
  293. {
  294. // We must always remove item 0.
  295. remove(0);
  296. }
  297. }
  298. /**
  299. * Creates the native peer for this object.
  300. */
  301. public void addNotify()
  302. {
  303. MenuPeer peer = (MenuPeer) getPeer();
  304. if (peer == null)
  305. {
  306. peer = getToolkit().createMenu(this);
  307. setPeer(peer);
  308. }
  309. Enumeration e = items.elements();
  310. while (e.hasMoreElements())
  311. {
  312. MenuItem mi = (MenuItem)e.nextElement();
  313. mi.addNotify();
  314. peer.addItem(mi);
  315. }
  316. super.addNotify();
  317. }
  318. /**
  319. * Destroys the native peer for this object.
  320. */
  321. public void removeNotify()
  322. {
  323. Enumeration e = items.elements();
  324. while (e.hasMoreElements())
  325. {
  326. MenuItem mi = (MenuItem) e.nextElement();
  327. mi.removeNotify();
  328. }
  329. super.removeNotify();
  330. }
  331. /**
  332. * Returns a debugging string for this menu.
  333. *
  334. * @return A debugging string for this menu.
  335. */
  336. public String paramString()
  337. {
  338. return (",tearOff=" + tearOff + ",isHelpMenu=" + isHelpMenu
  339. + super.paramString());
  340. }
  341. /**
  342. * Basic Accessibility class for Menu. Details get provided in derived
  343. * classes.
  344. */
  345. protected class AccessibleAWTMenu extends AccessibleAWTMenuItem
  346. {
  347. private static final long serialVersionUID = 5228160894980069094L;
  348. protected AccessibleAWTMenu()
  349. {
  350. }
  351. public AccessibleRole getAccessibleRole()
  352. {
  353. return AccessibleRole.MENU;
  354. }
  355. }
  356. /**
  357. * Gets the AccessibleContext associated with this <code>Menu</code>.
  358. * The context is created, if necessary.
  359. *
  360. * @return the associated context
  361. */
  362. public AccessibleContext getAccessibleContext()
  363. {
  364. /* Create the context if this is the first request */
  365. if (accessibleContext == null)
  366. accessibleContext = new AccessibleAWTMenu();
  367. return accessibleContext;
  368. }
  369. /**
  370. * Generate a unique name for this <code>Menu</code>.
  371. *
  372. * @return A unique name for this <code>Menu</code>.
  373. */
  374. String generateName()
  375. {
  376. return "menu" + getUniqueLong();
  377. }
  378. private static synchronized long getUniqueLong()
  379. {
  380. return next_menu_number++;
  381. }
  382. } // class Menu