1 /*
   2  * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  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 javax.swing;
  26 
  27 import java.awt.Component;
  28 import java.awt.Graphics;
  29 import java.awt.Insets;
  30 import java.awt.event.*;
  31 import java.beans.JavaBean;
  32 import java.beans.BeanProperty;
  33 import java.beans.Transient;
  34 import java.util.Vector;
  35 
  36 import java.io.Serializable;
  37 import java.io.ObjectOutputStream;
  38 import java.io.ObjectInputStream;
  39 import java.io.IOException;
  40 import java.lang.reflect.Method;
  41 
  42 import javax.swing.plaf.*;
  43 import javax.accessibility.*;
  44 
  45 import java.security.AccessController;
  46 import java.security.PrivilegedAction;
  47 import sun.awt.OSInfo;
  48 
  49 /**
  50  * An implementation of a menu bar. You add <code>JMenu</code> objects to the
  51  * menu bar to construct a menu. When the user selects a <code>JMenu</code>
  52  * object, its associated <code>JPopupMenu</code> is displayed, allowing the
  53  * user to select one of the <code>JMenuItems</code> on it.
  54  * <p>
  55  * For information and examples of using menu bars see
  56  * <a
  57  href="http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html">How to Use Menus</a>,
  58  * a section in <em>The Java Tutorial.</em>
  59  * <p>
  60  * <strong>Warning:</strong> Swing is not thread safe. For more
  61  * information see <a
  62  * href="package-summary.html#threading">Swing's Threading
  63  * Policy</a>.
  64  * <p>
  65  * <strong>Warning:</strong>
  66  * Serialized objects of this class will not be compatible with
  67  * future Swing releases. The current serialization support is
  68  * appropriate for short term storage or RMI between applications running
  69  * the same version of Swing.  As of 1.4, support for long term storage
  70  * of all JavaBeans&trade;
  71  * has been added to the <code>java.beans</code> package.
  72  * Please see {@link java.beans.XMLEncoder}.
  73  * <p>
  74  * <strong>Warning:</strong>
  75  * By default, pressing the Tab key does not transfer focus from a <code>
  76  * JMenuBar</code> which is added to a container together with other Swing
  77  * components, because the <code>focusTraversalKeysEnabled</code> property
  78  * of <code>JMenuBar</code> is set to <code>false</code>. To resolve this,
  79  * you should call the <code>JMenuBar.setFocusTraversalKeysEnabled(true)</code>
  80  * method.
  81  *
  82  * @author Georges Saab
  83  * @author David Karlton
  84  * @author Arnaud Weber
  85  * @see JMenu
  86  * @see JPopupMenu
  87  * @see JMenuItem
  88  * @since 1.2
  89  */
  90 @JavaBean(defaultProperty = "UI", description = "A container for holding and displaying menus.")
  91 @SwingContainer
  92 @SuppressWarnings("serial")
  93 public class JMenuBar extends JComponent implements Accessible,MenuElement
  94 {
  95     /**
  96      * @see #getUIClassID
  97      * @see #readObject
  98      */
  99     private static final String uiClassID = "MenuBarUI";
 100 
 101     /*
 102      * Model for the selected subcontrol.
 103      */
 104     private transient SingleSelectionModel selectionModel;
 105 
 106     private boolean paintBorder           = true;
 107     private Insets     margin             = null;
 108 
 109     /* diagnostic aids -- should be false for production builds. */
 110     private static final boolean TRACE =   false; // trace creates and disposes
 111     private static final boolean VERBOSE = false; // show reuse hits/misses
 112     private static final boolean DEBUG =   false;  // show bad params, misc.
 113 
 114     private static boolean disableGlobalMenuBar = true;
 115 
 116 
 117     static {
 118         AccessController
 119             .doPrivileged((PrivilegedAction<Void>) () -> {
 120                 if (OSInfo.getOSType() == OSInfo.OSType.MACOSX
 121                         && !Boolean.getBoolean("jdk.swing.disableForcedGlobalMenuBar")) {
 122                     System.loadLibrary("osxui");
 123                     disableGlobalMenuBar = false;
 124                 }
 125                 return null;
 126             });
 127     }
 128 
 129 
 130     private static boolean getScreenMenuBarProperty() {
 131         try {
 132             Class<?> cls = Class.forName("com.apple.laf.AquaMenuBarUI");
 133             Method m = cls.getDeclaredMethod("getScreenMenuBarProperty");
 134             m.setAccessible(true);
 135 
 136             return ((boolean) m.invoke(null));
 137         } catch (Exception ignored) {}
 138 
 139         return false;
 140     }
 141 
 142 
 143 
 144     /**
 145      * Creates a new menu bar.
 146      */
 147     public JMenuBar() {
 148         super();
 149         setFocusTraversalKeysEnabled(false);
 150         setSelectionModel(new DefaultSingleSelectionModel());
 151         updateUI();
 152     }
 153 
 154     /**
 155      * Returns the menubar's current UI.
 156      *
 157      * @return a {@code MenuBarUI} which is the menubar's current L&amp;F object
 158      * @see #setUI
 159      */
 160     public MenuBarUI getUI() {
 161         return (MenuBarUI)ui;
 162     }
 163 
 164     /**
 165      * Sets the L&amp;F object that renders this component.
 166      *
 167      * @param ui the new MenuBarUI L&amp;F object
 168      * @see UIDefaults#getUI
 169      */
 170     @BeanProperty(hidden = true, visualUpdate = true, description
 171             = "The UI object that implements the Component's LookAndFeel.")
 172     public void setUI(MenuBarUI ui) {
 173         super.setUI(ui);
 174     }
 175 
 176     /**
 177      * Resets the UI property with a value from the current look and feel.
 178      *
 179      * @see JComponent#updateUI
 180      */
 181     public void updateUI() {
 182         if (!disableGlobalMenuBar) {
 183             if (getScreenMenuBarProperty())  {
 184                 UIManager.put("MenuBarUI", "com.apple.laf.AquaMenuBarUI");
 185             } else {
 186                 UIManager.put("MenuBarUI", null);
 187             }
 188         }
 189         setUI((MenuBarUI)UIManager.getUI(this));
 190     }
 191 
 192 
 193     /**
 194      * Returns the name of the L&amp;F class that renders this component.
 195      *
 196      * @return the string "MenuBarUI"
 197      * @see JComponent#getUIClassID
 198      * @see UIDefaults#getUI
 199      */
 200     @BeanProperty(bound = false)
 201     public String getUIClassID() {
 202         return uiClassID;
 203     }
 204 
 205 
 206     /**
 207      * Returns the model object that handles single selections.
 208      *
 209      * @return the <code>SingleSelectionModel</code> property
 210      * @see SingleSelectionModel
 211      */
 212     public SingleSelectionModel getSelectionModel() {
 213         return selectionModel;
 214     }
 215 
 216     /**
 217      * Sets the model object to handle single selections.
 218      *
 219      * @param model the <code>SingleSelectionModel</code> to use
 220      * @see SingleSelectionModel
 221      */
 222     @BeanProperty(description = "The selection model, recording which child is selected.")
 223     public void setSelectionModel(SingleSelectionModel model) {
 224         SingleSelectionModel oldValue = selectionModel;
 225         this.selectionModel = model;
 226         firePropertyChange("selectionModel", oldValue, selectionModel);
 227     }
 228 
 229 
 230     /**
 231      * Appends the specified menu to the end of the menu bar.
 232      *
 233      * @param c the <code>JMenu</code> component to add
 234      * @return the menu component
 235      */
 236     public JMenu add(JMenu c) {
 237         super.add(c);
 238         return c;
 239     }
 240 
 241     /**
 242      * Returns the menu at the specified position in the menu bar.
 243      *
 244      * @param index  an integer giving the position in the menu bar, where
 245      *               0 is the first position
 246      * @return the <code>JMenu</code> at that position, or <code>null</code> if
 247      *          if there is no <code>JMenu</code> at that position (ie. if
 248      *          it is a <code>JMenuItem</code>)
 249      */
 250     public JMenu getMenu(int index) {
 251         Component c = getComponentAtIndex(index);
 252         if (c instanceof JMenu)
 253             return (JMenu) c;
 254         return null;
 255     }
 256 
 257     /**
 258      * Returns the number of items in the menu bar.
 259      *
 260      * @return the number of items in the menu bar
 261      */
 262     @BeanProperty(bound = false)
 263     public int getMenuCount() {
 264         return getComponentCount();
 265     }
 266 
 267     /**
 268      * Sets the help menu that appears when the user selects the
 269      * "help" option in the menu bar. This method is not yet implemented
 270      * and will throw an exception.
 271      *
 272      * @param menu the JMenu that delivers help to the user
 273      */
 274     public void setHelpMenu(JMenu menu) {
 275         throw new Error("setHelpMenu() not yet implemented.");
 276     }
 277 
 278     /**
 279      * Gets the help menu for the menu bar.  This method is not yet
 280      * implemented and will throw an exception.
 281      *
 282      * @return the <code>JMenu</code> that delivers help to the user
 283      */
 284     @Transient
 285     public JMenu getHelpMenu() {
 286         throw new Error("getHelpMenu() not yet implemented.");
 287     }
 288 
 289     /**
 290      * Returns the component at the specified index.
 291      *
 292      * @param i an integer specifying the position, where 0 is first
 293      * @return the <code>Component</code> at the position,
 294      *          or <code>null</code> for an invalid index
 295      * @deprecated replaced by <code>getComponent(int i)</code>
 296      */
 297     @Deprecated
 298     public Component getComponentAtIndex(int i) {
 299         if(i < 0 || i >= getComponentCount()) {
 300             return null;
 301         }
 302         return getComponent(i);
 303     }
 304 
 305     /**
 306      * Returns the index of the specified component.
 307      *
 308      * @param c  the <code>Component</code> to find
 309      * @return an integer giving the component's position, where 0 is first;
 310      *          or -1 if it can't be found
 311      */
 312     public int getComponentIndex(Component c) {
 313         int ncomponents = this.getComponentCount();
 314         Component[] component = this.getComponents();
 315         for (int i = 0 ; i < ncomponents ; i++) {
 316             Component comp = component[i];
 317             if (comp == c)
 318                 return i;
 319         }
 320         return -1;
 321     }
 322 
 323     /**
 324      * Sets the currently selected component, producing a
 325      * a change to the selection model.
 326      *
 327      * @param sel the <code>Component</code> to select
 328      */
 329     public void setSelected(Component sel) {
 330         SingleSelectionModel model = getSelectionModel();
 331         int index = getComponentIndex(sel);
 332         model.setSelectedIndex(index);
 333     }
 334 
 335     /**
 336      * Returns true if the menu bar currently has a component selected.
 337      *
 338      * @return true if a selection has been made, else false
 339      */
 340     @BeanProperty(bound = false)
 341     public boolean isSelected() {
 342         return selectionModel.isSelected();
 343     }
 344 
 345     /**
 346      * Returns true if the menu bars border should be painted.
 347      *
 348      * @return  true if the border should be painted, else false
 349      */
 350     public boolean isBorderPainted() {
 351         return paintBorder;
 352     }
 353 
 354     /**
 355      * Sets whether the border should be painted.
 356      *
 357      * @param b if true and border property is not <code>null</code>,
 358      *          the border is painted.
 359      * @see #isBorderPainted
 360      */
 361     @BeanProperty(visualUpdate = true, description
 362             = "Whether the border should be painted.")
 363     public void setBorderPainted(boolean b) {
 364         boolean oldValue = paintBorder;
 365         paintBorder = b;
 366         firePropertyChange("borderPainted", oldValue, paintBorder);
 367         if (b != oldValue) {
 368             revalidate();
 369             repaint();
 370         }
 371     }
 372 
 373     /**
 374      * Paints the menubar's border if <code>BorderPainted</code>
 375      * property is true.
 376      *
 377      * @param g the <code>Graphics</code> context to use for painting
 378      * @see JComponent#paint
 379      * @see JComponent#setBorder
 380      */
 381     protected void paintBorder(Graphics g) {
 382         if (isBorderPainted()) {
 383             super.paintBorder(g);
 384         }
 385     }
 386 
 387     /**
 388      * Sets the margin between the menubar's border and
 389      * its menus. Setting to <code>null</code> will cause the menubar to
 390      * use the default margins.
 391      *
 392      * @param m an Insets object containing the margin values
 393      * @see Insets
 394      */
 395     @BeanProperty(visualUpdate = true, description
 396             = "The space between the menubar's border and its contents")
 397     public void setMargin(Insets m) {
 398         Insets old = margin;
 399         this.margin = m;
 400         firePropertyChange("margin", old, m);
 401         if (old == null || !old.equals(m)) {
 402             revalidate();
 403             repaint();
 404         }
 405     }
 406 
 407     /**
 408      * Returns the margin between the menubar's border and
 409      * its menus.  If there is no previous margin, it will create
 410      * a default margin with zero size.
 411      *
 412      * @return an <code>Insets</code> object containing the margin values
 413      * @see Insets
 414      */
 415     public Insets getMargin() {
 416         if(margin == null) {
 417             return new Insets(0,0,0,0);
 418         } else {
 419             return margin;
 420         }
 421     }
 422 
 423 
 424     /**
 425      * Implemented to be a <code>MenuElement</code> -- does nothing.
 426      *
 427      * @see #getSubElements
 428      */
 429     public void processMouseEvent(MouseEvent event,MenuElement path[],MenuSelectionManager manager) {
 430     }
 431 
 432     /**
 433      * Implemented to be a <code>MenuElement</code> -- does nothing.
 434      *
 435      * @see #getSubElements
 436      */
 437     public void processKeyEvent(KeyEvent e,MenuElement path[],MenuSelectionManager manager) {
 438     }
 439 
 440     /**
 441      * Implemented to be a <code>MenuElement</code> -- does nothing.
 442      *
 443      * @see #getSubElements
 444      */
 445     public void menuSelectionChanged(boolean isIncluded) {
 446     }
 447 
 448     /**
 449      * Implemented to be a <code>MenuElement</code> -- returns the
 450      * menus in this menu bar.
 451      * This is the reason for implementing the <code>MenuElement</code>
 452      * interface -- so that the menu bar can be treated the same as
 453      * other menu elements.
 454      * @return an array of menu items in the menu bar.
 455      */
 456     @BeanProperty(bound = false)
 457     public MenuElement[] getSubElements() {
 458         MenuElement result[];
 459         Vector<MenuElement> tmp = new Vector<MenuElement>();
 460         int c = getComponentCount();
 461         int i;
 462         Component m;
 463 
 464         for(i=0 ; i < c ; i++) {
 465             m = getComponent(i);
 466             if(m instanceof MenuElement)
 467                 tmp.addElement((MenuElement) m);
 468         }
 469 
 470         result = new MenuElement[tmp.size()];
 471         for(i=0,c=tmp.size() ; i < c ; i++)
 472             result[i] = tmp.elementAt(i);
 473         return result;
 474     }
 475 
 476     /**
 477      * Implemented to be a <code>MenuElement</code>. Returns this object.
 478      *
 479      * @return the current <code>Component</code> (this)
 480      * @see #getSubElements
 481      */
 482     public Component getComponent() {
 483         return this;
 484     }
 485 
 486 
 487     /**
 488      * Returns a string representation of this <code>JMenuBar</code>.
 489      * This method
 490      * is intended to be used only for debugging purposes, and the
 491      * content and format of the returned string may vary between
 492      * implementations. The returned string may be empty but may not
 493      * be <code>null</code>.
 494      *
 495      * @return  a string representation of this <code>JMenuBar</code>
 496      */
 497     protected String paramString() {
 498         String paintBorderString = (paintBorder ?
 499                                     "true" : "false");
 500         String marginString = (margin != null ?
 501                                margin.toString() : "");
 502 
 503         return super.paramString() +
 504         ",margin=" + marginString +
 505         ",paintBorder=" + paintBorderString;
 506     }
 507 
 508 /////////////////
 509 // Accessibility support
 510 ////////////////
 511 
 512     /**
 513      * Gets the AccessibleContext associated with this JMenuBar.
 514      * For JMenuBars, the AccessibleContext takes the form of an
 515      * AccessibleJMenuBar.
 516      * A new AccessibleJMenuBar instance is created if necessary.
 517      *
 518      * @return an AccessibleJMenuBar that serves as the
 519      *         AccessibleContext of this JMenuBar
 520      */
 521     @BeanProperty(bound = false)
 522     public AccessibleContext getAccessibleContext() {
 523         if (accessibleContext == null) {
 524             accessibleContext = new AccessibleJMenuBar();
 525         }
 526         return accessibleContext;
 527     }
 528 
 529     /**
 530      * This class implements accessibility support for the
 531      * <code>JMenuBar</code> class.  It provides an implementation of the
 532      * Java Accessibility API appropriate to menu bar user-interface
 533      * elements.
 534      * <p>
 535      * <strong>Warning:</strong>
 536      * Serialized objects of this class will not be compatible with
 537      * future Swing releases. The current serialization support is
 538      * appropriate for short term storage or RMI between applications running
 539      * the same version of Swing.  As of 1.4, support for long term storage
 540      * of all JavaBeans&trade;
 541      * has been added to the <code>java.beans</code> package.
 542      * Please see {@link java.beans.XMLEncoder}.
 543      */
 544     @SuppressWarnings("serial")
 545     protected class AccessibleJMenuBar extends AccessibleJComponent
 546         implements AccessibleSelection {
 547 
 548         /**
 549          * Get the accessible state set of this object.
 550          *
 551          * @return an instance of AccessibleState containing the current state
 552          *         of the object
 553          */
 554         public AccessibleStateSet getAccessibleStateSet() {
 555             AccessibleStateSet states = super.getAccessibleStateSet();
 556             return states;
 557         }
 558 
 559         /**
 560          * Get the role of this object.
 561          *
 562          * @return an instance of AccessibleRole describing the role of the
 563          * object
 564          */
 565         public AccessibleRole getAccessibleRole() {
 566             return AccessibleRole.MENU_BAR;
 567         }
 568 
 569         /**
 570          * Get the AccessibleSelection associated with this object.  In the
 571          * implementation of the Java Accessibility API for this class,
 572          * return this object, which is responsible for implementing the
 573          * AccessibleSelection interface on behalf of itself.
 574          *
 575          * @return this object
 576          */
 577         public AccessibleSelection getAccessibleSelection() {
 578             return this;
 579         }
 580 
 581         /**
 582          * Returns 1 if a menu is currently selected in this menu bar.
 583          *
 584          * @return 1 if a menu is currently selected, else 0
 585          */
 586          public int getAccessibleSelectionCount() {
 587             if (isSelected()) {
 588                 return 1;
 589             } else {
 590                 return 0;
 591             }
 592          }
 593 
 594         /**
 595          * Returns the currently selected menu if one is selected,
 596          * otherwise null.
 597          */
 598          public Accessible getAccessibleSelection(int i) {
 599             if (isSelected()) {
 600                 if (i != 0) {   // single selection model for JMenuBar
 601                     return null;
 602                 }
 603                 int j = getSelectionModel().getSelectedIndex();
 604                 if (getComponentAtIndex(j) instanceof Accessible) {
 605                     return (Accessible) getComponentAtIndex(j);
 606                 }
 607             }
 608             return null;
 609          }
 610 
 611         /**
 612          * Returns true if the current child of this object is selected.
 613          *
 614          * @param i the zero-based index of the child in this Accessible
 615          * object.
 616          * @see AccessibleContext#getAccessibleChild
 617          */
 618         public boolean isAccessibleChildSelected(int i) {
 619             return (i == getSelectionModel().getSelectedIndex());
 620         }
 621 
 622         /**
 623          * Selects the nth menu in the menu bar, forcing it to
 624          * pop up.  If another menu is popped up, this will force
 625          * it to close.  If the nth menu is already selected, this
 626          * method has no effect.
 627          *
 628          * @param i the zero-based index of selectable items
 629          * @see #getAccessibleStateSet
 630          */
 631         public void addAccessibleSelection(int i) {
 632             // first close up any open menu
 633             int j = getSelectionModel().getSelectedIndex();
 634             if (i == j) {
 635                 return;
 636             }
 637             if (j >= 0 && j < getMenuCount()) {
 638                 JMenu menu = getMenu(j);
 639                 if (menu != null) {
 640                     MenuSelectionManager.defaultManager().setSelectedPath(null);
 641 //                  menu.setPopupMenuVisible(false);
 642                 }
 643             }
 644             // now popup the new menu
 645             getSelectionModel().setSelectedIndex(i);
 646             JMenu menu = getMenu(i);
 647             if (menu != null) {
 648                 MenuElement me[] = new MenuElement[3];
 649                 me[0] = JMenuBar.this;
 650                 me[1] = menu;
 651                 me[2] = menu.getPopupMenu();
 652                 MenuSelectionManager.defaultManager().setSelectedPath(me);
 653 //              menu.setPopupMenuVisible(true);
 654             }
 655         }
 656 
 657         /**
 658          * Removes the nth selected item in the object from the object's
 659          * selection.  If the nth item isn't currently selected, this
 660          * method has no effect.  Otherwise, it closes the popup menu.
 661          *
 662          * @param i the zero-based index of selectable items
 663          */
 664         public void removeAccessibleSelection(int i) {
 665             if (i >= 0 && i < getMenuCount()) {
 666                 JMenu menu = getMenu(i);
 667                 if (menu != null) {
 668                     MenuSelectionManager.defaultManager().setSelectedPath(null);
 669 //                  menu.setPopupMenuVisible(false);
 670                 }
 671                 getSelectionModel().setSelectedIndex(-1);
 672             }
 673         }
 674 
 675         /**
 676          * Clears the selection in the object, so that nothing in the
 677          * object is selected.  This will close any open menu.
 678          */
 679         public void clearAccessibleSelection() {
 680             int i = getSelectionModel().getSelectedIndex();
 681             if (i >= 0 && i < getMenuCount()) {
 682                 JMenu menu = getMenu(i);
 683                 if (menu != null) {
 684                     MenuSelectionManager.defaultManager().setSelectedPath(null);
 685 //                  menu.setPopupMenuVisible(false);
 686                 }
 687             }
 688             getSelectionModel().setSelectedIndex(-1);
 689         }
 690 
 691         /**
 692          * Normally causes every selected item in the object to be selected
 693          * if the object supports multiple selections.  This method
 694          * makes no sense in a menu bar, and so does nothing.
 695          */
 696         public void selectAllAccessibleSelection() {
 697         }
 698     } // internal class AccessibleJMenuBar
 699 
 700 
 701     /**
 702      * Subclassed to check all the child menus.
 703      * @since 1.3
 704      */
 705     protected boolean processKeyBinding(KeyStroke ks, KeyEvent e,
 706                                         int condition, boolean pressed) {
 707         // See if we have a local binding.
 708         boolean retValue = super.processKeyBinding(ks, e, condition, pressed);
 709         if (!retValue) {
 710             MenuElement[] subElements = getSubElements();
 711             for (MenuElement subElement : subElements) {
 712                 if (processBindingForKeyStrokeRecursive(
 713                         subElement, ks, e, condition, pressed)) {
 714                     return true;
 715                 }
 716             }
 717         }
 718         return retValue;
 719     }
 720 
 721     static boolean processBindingForKeyStrokeRecursive(MenuElement elem,
 722                                                        KeyStroke ks, KeyEvent e, int condition, boolean pressed) {
 723         if (elem == null) {
 724             return false;
 725         }
 726 
 727         Component c = elem.getComponent();
 728 
 729         if ( !(c.isVisible() || (c instanceof JPopupMenu)) || !c.isEnabled() ) {
 730             return false;
 731         }
 732 
 733         if (c != null && c instanceof JComponent &&
 734             ((JComponent)c).processKeyBinding(ks, e, condition, pressed)) {
 735 
 736             return true;
 737         }
 738 
 739         MenuElement[] subElements = elem.getSubElements();
 740         for (MenuElement subElement : subElements) {
 741             if (processBindingForKeyStrokeRecursive(subElement, ks, e, condition, pressed)) {
 742                 return true;
 743                 // We don't, pass along to children JMenu's
 744             }
 745         }
 746         return false;
 747     }
 748 
 749     /**
 750      * Overrides <code>JComponent.addNotify</code> to register this
 751      * menu bar with the current keyboard manager.
 752      */
 753     public void addNotify() {
 754         super.addNotify();
 755         KeyboardManager.getCurrentManager().registerMenuBar(this);
 756     }
 757 
 758     /**
 759      * Overrides <code>JComponent.removeNotify</code> to unregister this
 760      * menu bar with the current keyboard manager.
 761      */
 762     public void removeNotify() {
 763         super.removeNotify();
 764         KeyboardManager.getCurrentManager().unregisterMenuBar(this);
 765     }
 766 
 767 
 768     private void writeObject(ObjectOutputStream s) throws IOException {
 769         s.defaultWriteObject();
 770         if (getUIClassID().equals(uiClassID)) {
 771             byte count = JComponent.getWriteObjCounter(this);
 772             JComponent.setWriteObjCounter(this, --count);
 773             if (count == 0 && ui != null) {
 774                 ui.installUI(this);
 775             }
 776         }
 777 
 778         Object[] kvData = new Object[4];
 779         int n = 0;
 780 
 781         if (selectionModel instanceof Serializable) {
 782             kvData[n++] = "selectionModel";
 783             kvData[n++] = selectionModel;
 784         }
 785 
 786         s.writeObject(kvData);
 787     }
 788 
 789 
 790     /**
 791      * See JComponent.readObject() for information about serialization
 792      * in Swing.
 793      */
 794     private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException
 795     {
 796         s.defaultReadObject();
 797         Object[] kvData = (Object[])(s.readObject());
 798 
 799         for(int i = 0; i < kvData.length; i += 2) {
 800             if (kvData[i] == null) {
 801                 break;
 802             }
 803             else if (kvData[i].equals("selectionModel")) {
 804                 selectionModel = (SingleSelectionModel)kvData[i + 1];
 805             }
 806         }
 807 
 808     }
 809 }