17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package java.awt;
26
27 import java.io.IOException;
28 import java.io.ObjectInputStream;
29 import java.util.Vector;
30 import java.util.Enumeration;
31 import sun.awt.AWTAccessor;
32 import java.awt.peer.MenuBarPeer;
33 import java.awt.event.KeyEvent;
34 import javax.accessibility.*;
35
36 /**
37 * The <code>MenuBar</code> class encapsulates the platform's
38 * concept of a menu bar bound to a frame. In order to associate
39 * the menu bar with a <code>Frame</code> object, call the
40 * frame's <code>setMenuBar</code> method.
41 * <p>
42 * <A NAME="mbexample"></A><!-- target for cross references -->
43 * This is what a menu bar might look like:
44 * <p>
45 * <img src="doc-files/MenuBar-1.gif"
46 * alt="Diagram of MenuBar containing 2 menus: Examples and Options.
47 * Examples menu is expanded showing items: Basic, Simple, Check, and More Examples."
48 * style="float:center; margin: 7px 10px;">
49 * <p>
50 * A menu bar handles keyboard shortcuts for menu items, passing them
51 * along to its child menus.
52 * (Keyboard shortcuts, which are optional, provide the user with
53 * an alternative to the mouse for invoking a menu item and the
54 * action that is associated with it.)
55 * Each menu item can maintain an instance of <code>MenuShortcut</code>.
56 * The <code>MenuBar</code> class defines several methods,
57 * {@link MenuBar#shortcuts} and
58 * {@link MenuBar#getShortcutMenuItem}
59 * that retrieve information about the shortcuts a given
60 * menu bar is managing.
61 *
62 * @author Sami Shaio
63 * @see java.awt.Frame
64 * @see java.awt.Frame#setMenuBar(java.awt.MenuBar)
65 * @see java.awt.Menu
66 * @see java.awt.MenuItem
67 * @see java.awt.MenuShortcut
68 * @since 1.0
69 */
70 public class MenuBar extends MenuComponent implements MenuContainer, Accessible {
71
72 static {
73 /* ensure that the necessary native libraries are loaded */
74 Toolkit.loadLibraries();
75 if (!GraphicsEnvironment.isHeadless()) {
76 initIDs();
272 if (index >= 0) {
273 remove(index);
274 }
275 }
276 }
277
278 /**
279 * Gets the number of menus on the menu bar.
280 * @return the number of menus on the menu bar.
281 * @since 1.1
282 */
283 public int getMenuCount() {
284 return countMenus();
285 }
286
287 /**
288 * Gets the number of menus on the menu bar.
289 *
290 * @return the number of menus on the menu bar.
291 * @deprecated As of JDK version 1.1,
292 * replaced by <code>getMenuCount()</code>.
293 */
294 @Deprecated
295 public int countMenus() {
296 return getMenuCountImpl();
297 }
298
299 /*
300 * This is called by the native code, so client code can't
301 * be called on the toolkit thread.
302 */
303 final int getMenuCountImpl() {
304 return menus.size();
305 }
306
307 /**
308 * Gets the specified menu.
309 * @param i the index position of the menu to be returned.
310 * @return the menu at the specified index of this menu bar.
311 */
312 public Menu getMenu(int i) {
325 * Gets an enumeration of all menu shortcuts this menu bar
326 * is managing.
327 * @return an enumeration of menu shortcuts that this
328 * menu bar is managing.
329 * @see java.awt.MenuShortcut
330 * @since 1.1
331 */
332 public synchronized Enumeration<MenuShortcut> shortcuts() {
333 Vector<MenuShortcut> shortcuts = new Vector<>();
334 int nmenus = getMenuCount();
335 for (int i = 0 ; i < nmenus ; i++) {
336 Enumeration<MenuShortcut> e = getMenu(i).shortcuts();
337 while (e.hasMoreElements()) {
338 shortcuts.addElement(e.nextElement());
339 }
340 }
341 return shortcuts.elements();
342 }
343
344 /**
345 * Gets the instance of <code>MenuItem</code> associated
346 * with the specified <code>MenuShortcut</code> object,
347 * or <code>null</code> if none of the menu items being managed
348 * by this menu bar is associated with the specified menu
349 * shortcut.
350 * @param s the specified menu shortcut.
351 * @return the menu item for the specified shortcut.
352 * @see java.awt.MenuItem
353 * @see java.awt.MenuShortcut
354 * @since 1.1
355 */
356 public MenuItem getShortcutMenuItem(MenuShortcut s) {
357 int nmenus = getMenuCount();
358 for (int i = 0 ; i < nmenus ; i++) {
359 MenuItem mi = getMenu(i).getShortcutMenuItem(s);
360 if (mi != null) {
361 return mi;
362 }
363 }
364 return null; // MenuShortcut wasn't found
365 }
366
367 /*
403 int nmenus = getMenuCount();
404 for (int i = 0 ; i < nmenus ; i++) {
405 getMenu(i).deleteShortcut(s);
406 }
407 }
408
409 /* Serialization support. Restore the (transient) parent
410 * fields of Menubar menus here.
411 */
412
413 /**
414 * The MenuBar's serialized data version.
415 *
416 * @serial
417 */
418 private int menuBarSerializedDataVersion = 1;
419
420 /**
421 * Writes default serializable fields to stream.
422 *
423 * @param s the <code>ObjectOutputStream</code> to write
424 * @see AWTEventMulticaster#save(ObjectOutputStream, String, EventListener)
425 * @see #readObject(java.io.ObjectInputStream)
426 */
427 private void writeObject(java.io.ObjectOutputStream s)
428 throws java.lang.ClassNotFoundException,
429 java.io.IOException
430 {
431 s.defaultWriteObject();
432 }
433
434 /**
435 * Reads the <code>ObjectInputStream</code>.
436 * Unrecognized keys or values will be ignored.
437 *
438 * @param s the <code>ObjectInputStream</code> to read
439 * @exception HeadlessException if
440 * <code>GraphicsEnvironment.isHeadless</code> returns
441 * <code>true</code>
442 * @see java.awt.GraphicsEnvironment#isHeadless
443 * @see #writeObject(java.io.ObjectOutputStream)
444 */
445 private void readObject(ObjectInputStream s)
446 throws ClassNotFoundException, IOException, HeadlessException
447 {
448 // HeadlessException will be thrown from MenuComponent's readObject
449 s.defaultReadObject();
450 for (int i = 0; i < menus.size(); i++) {
451 Menu m = menus.elementAt(i);
452 m.parent = this;
453 }
454 }
455
456 /**
457 * Initialize JNI field and method IDs
458 */
459 private static native void initIDs();
460
461
477 if (accessibleContext == null) {
478 accessibleContext = new AccessibleAWTMenuBar();
479 }
480 return accessibleContext;
481 }
482
483 /**
484 * Defined in MenuComponent. Overridden here.
485 */
486 int getAccessibleChildIndex(MenuComponent child) {
487 return menus.indexOf(child);
488 }
489
490 /**
491 * Inner class of MenuBar used to provide default support for
492 * accessibility. This class is not meant to be used directly by
493 * application developers, but is instead meant only to be
494 * subclassed by menu component developers.
495 * <p>
496 * This class implements accessibility support for the
497 * <code>MenuBar</code> class. It provides an implementation of the
498 * Java Accessibility API appropriate to menu bar user-interface elements.
499 * @since 1.3
500 */
501 protected class AccessibleAWTMenuBar extends AccessibleAWTMenuComponent
502 {
503 /*
504 * JDK 1.3 serialVersionUID
505 */
506 private static final long serialVersionUID = -8577604491830083815L;
507
508 /**
509 * Get the role of this object.
510 *
511 * @return an instance of AccessibleRole describing the role of the
512 * object
513 * @since 1.4
514 */
515 public AccessibleRole getAccessibleRole() {
516 return AccessibleRole.MENU_BAR;
517 }
|
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package java.awt;
26
27 import java.io.IOException;
28 import java.io.ObjectInputStream;
29 import java.util.Vector;
30 import java.util.Enumeration;
31 import sun.awt.AWTAccessor;
32 import java.awt.peer.MenuBarPeer;
33 import java.awt.event.KeyEvent;
34 import javax.accessibility.*;
35
36 /**
37 * The {@code MenuBar} class encapsulates the platform's
38 * concept of a menu bar bound to a frame. In order to associate
39 * the menu bar with a {@code Frame} object, call the
40 * frame's {@code setMenuBar} method.
41 * <p>
42 * <A NAME="mbexample"></A><!-- target for cross references -->
43 * This is what a menu bar might look like:
44 * <p>
45 * <img src="doc-files/MenuBar-1.gif"
46 * alt="Diagram of MenuBar containing 2 menus: Examples and Options.
47 * Examples menu is expanded showing items: Basic, Simple, Check, and More Examples."
48 * style="float:center; margin: 7px 10px;">
49 * <p>
50 * A menu bar handles keyboard shortcuts for menu items, passing them
51 * along to its child menus.
52 * (Keyboard shortcuts, which are optional, provide the user with
53 * an alternative to the mouse for invoking a menu item and the
54 * action that is associated with it.)
55 * Each menu item can maintain an instance of {@code MenuShortcut}.
56 * The {@code MenuBar} class defines several methods,
57 * {@link MenuBar#shortcuts} and
58 * {@link MenuBar#getShortcutMenuItem}
59 * that retrieve information about the shortcuts a given
60 * menu bar is managing.
61 *
62 * @author Sami Shaio
63 * @see java.awt.Frame
64 * @see java.awt.Frame#setMenuBar(java.awt.MenuBar)
65 * @see java.awt.Menu
66 * @see java.awt.MenuItem
67 * @see java.awt.MenuShortcut
68 * @since 1.0
69 */
70 public class MenuBar extends MenuComponent implements MenuContainer, Accessible {
71
72 static {
73 /* ensure that the necessary native libraries are loaded */
74 Toolkit.loadLibraries();
75 if (!GraphicsEnvironment.isHeadless()) {
76 initIDs();
272 if (index >= 0) {
273 remove(index);
274 }
275 }
276 }
277
278 /**
279 * Gets the number of menus on the menu bar.
280 * @return the number of menus on the menu bar.
281 * @since 1.1
282 */
283 public int getMenuCount() {
284 return countMenus();
285 }
286
287 /**
288 * Gets the number of menus on the menu bar.
289 *
290 * @return the number of menus on the menu bar.
291 * @deprecated As of JDK version 1.1,
292 * replaced by {@code getMenuCount()}.
293 */
294 @Deprecated
295 public int countMenus() {
296 return getMenuCountImpl();
297 }
298
299 /*
300 * This is called by the native code, so client code can't
301 * be called on the toolkit thread.
302 */
303 final int getMenuCountImpl() {
304 return menus.size();
305 }
306
307 /**
308 * Gets the specified menu.
309 * @param i the index position of the menu to be returned.
310 * @return the menu at the specified index of this menu bar.
311 */
312 public Menu getMenu(int i) {
325 * Gets an enumeration of all menu shortcuts this menu bar
326 * is managing.
327 * @return an enumeration of menu shortcuts that this
328 * menu bar is managing.
329 * @see java.awt.MenuShortcut
330 * @since 1.1
331 */
332 public synchronized Enumeration<MenuShortcut> shortcuts() {
333 Vector<MenuShortcut> shortcuts = new Vector<>();
334 int nmenus = getMenuCount();
335 for (int i = 0 ; i < nmenus ; i++) {
336 Enumeration<MenuShortcut> e = getMenu(i).shortcuts();
337 while (e.hasMoreElements()) {
338 shortcuts.addElement(e.nextElement());
339 }
340 }
341 return shortcuts.elements();
342 }
343
344 /**
345 * Gets the instance of {@code MenuItem} associated
346 * with the specified {@code MenuShortcut} object,
347 * or {@code null} if none of the menu items being managed
348 * by this menu bar is associated with the specified menu
349 * shortcut.
350 * @param s the specified menu shortcut.
351 * @return the menu item for the specified shortcut.
352 * @see java.awt.MenuItem
353 * @see java.awt.MenuShortcut
354 * @since 1.1
355 */
356 public MenuItem getShortcutMenuItem(MenuShortcut s) {
357 int nmenus = getMenuCount();
358 for (int i = 0 ; i < nmenus ; i++) {
359 MenuItem mi = getMenu(i).getShortcutMenuItem(s);
360 if (mi != null) {
361 return mi;
362 }
363 }
364 return null; // MenuShortcut wasn't found
365 }
366
367 /*
403 int nmenus = getMenuCount();
404 for (int i = 0 ; i < nmenus ; i++) {
405 getMenu(i).deleteShortcut(s);
406 }
407 }
408
409 /* Serialization support. Restore the (transient) parent
410 * fields of Menubar menus here.
411 */
412
413 /**
414 * The MenuBar's serialized data version.
415 *
416 * @serial
417 */
418 private int menuBarSerializedDataVersion = 1;
419
420 /**
421 * Writes default serializable fields to stream.
422 *
423 * @param s the {@code ObjectOutputStream} to write
424 * @see AWTEventMulticaster#save(ObjectOutputStream, String, EventListener)
425 * @see #readObject(java.io.ObjectInputStream)
426 */
427 private void writeObject(java.io.ObjectOutputStream s)
428 throws java.lang.ClassNotFoundException,
429 java.io.IOException
430 {
431 s.defaultWriteObject();
432 }
433
434 /**
435 * Reads the {@code ObjectInputStream}.
436 * Unrecognized keys or values will be ignored.
437 *
438 * @param s the {@code ObjectInputStream} to read
439 * @exception HeadlessException if
440 * {@code GraphicsEnvironment.isHeadless} returns
441 * {@code true}
442 * @see java.awt.GraphicsEnvironment#isHeadless
443 * @see #writeObject(java.io.ObjectOutputStream)
444 */
445 private void readObject(ObjectInputStream s)
446 throws ClassNotFoundException, IOException, HeadlessException
447 {
448 // HeadlessException will be thrown from MenuComponent's readObject
449 s.defaultReadObject();
450 for (int i = 0; i < menus.size(); i++) {
451 Menu m = menus.elementAt(i);
452 m.parent = this;
453 }
454 }
455
456 /**
457 * Initialize JNI field and method IDs
458 */
459 private static native void initIDs();
460
461
477 if (accessibleContext == null) {
478 accessibleContext = new AccessibleAWTMenuBar();
479 }
480 return accessibleContext;
481 }
482
483 /**
484 * Defined in MenuComponent. Overridden here.
485 */
486 int getAccessibleChildIndex(MenuComponent child) {
487 return menus.indexOf(child);
488 }
489
490 /**
491 * Inner class of MenuBar used to provide default support for
492 * accessibility. This class is not meant to be used directly by
493 * application developers, but is instead meant only to be
494 * subclassed by menu component developers.
495 * <p>
496 * This class implements accessibility support for the
497 * {@code MenuBar} class. It provides an implementation of the
498 * Java Accessibility API appropriate to menu bar user-interface elements.
499 * @since 1.3
500 */
501 protected class AccessibleAWTMenuBar extends AccessibleAWTMenuComponent
502 {
503 /*
504 * JDK 1.3 serialVersionUID
505 */
506 private static final long serialVersionUID = -8577604491830083815L;
507
508 /**
509 * Get the role of this object.
510 *
511 * @return an instance of AccessibleRole describing the role of the
512 * object
513 * @since 1.4
514 */
515 public AccessibleRole getAccessibleRole() {
516 return AccessibleRole.MENU_BAR;
517 }
|