< prev index next >

src/java.desktop/share/classes/javax/swing/JMenuBar.java

Print this page


   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


 374      * its menus.  If there is no previous margin, it will create
 375      * a default margin with zero size.
 376      *
 377      * @return an <code>Insets</code> object containing the margin values
 378      * @see Insets
 379      */
 380     public Insets getMargin() {
 381         if(margin == null) {
 382             return new Insets(0,0,0,0);
 383         } else {
 384             return margin;
 385         }
 386     }
 387 
 388 
 389     /**
 390      * Implemented to be a <code>MenuElement</code> -- does nothing.
 391      *
 392      * @see #getSubElements
 393      */
 394     public void processMouseEvent(MouseEvent event,MenuElement path[],MenuSelectionManager manager) {
 395     }
 396 
 397     /**
 398      * Implemented to be a <code>MenuElement</code> -- does nothing.
 399      *
 400      * @see #getSubElements
 401      */
 402     public void processKeyEvent(KeyEvent e,MenuElement path[],MenuSelectionManager manager) {
 403     }
 404 
 405     /**
 406      * Implemented to be a <code>MenuElement</code> -- does nothing.
 407      *
 408      * @see #getSubElements
 409      */
 410     public void menuSelectionChanged(boolean isIncluded) {
 411     }
 412 
 413     /**
 414      * Implemented to be a <code>MenuElement</code> -- returns the
 415      * menus in this menu bar.
 416      * This is the reason for implementing the <code>MenuElement</code>
 417      * interface -- so that the menu bar can be treated the same as
 418      * other menu elements.
 419      * @return an array of menu items in the menu bar.
 420      */
 421     @BeanProperty(bound = false)
 422     public MenuElement[] getSubElements() {
 423         MenuElement result[];
 424         Vector<MenuElement> tmp = new Vector<MenuElement>();
 425         int c = getComponentCount();
 426         int i;
 427         Component m;
 428 
 429         for(i=0 ; i < c ; i++) {
 430             m = getComponent(i);
 431             if(m instanceof MenuElement)
 432                 tmp.addElement((MenuElement) m);
 433         }
 434 
 435         result = new MenuElement[tmp.size()];
 436         for(i=0,c=tmp.size() ; i < c ; i++)
 437             result[i] = tmp.elementAt(i);
 438         return result;
 439     }
 440 
 441     /**
 442      * Implemented to be a <code>MenuElement</code>. Returns this object.
 443      *


 593          * @param i the zero-based index of selectable items
 594          * @see #getAccessibleStateSet
 595          */
 596         public void addAccessibleSelection(int i) {
 597             // first close up any open menu
 598             int j = getSelectionModel().getSelectedIndex();
 599             if (i == j) {
 600                 return;
 601             }
 602             if (j >= 0 && j < getMenuCount()) {
 603                 JMenu menu = getMenu(j);
 604                 if (menu != null) {
 605                     MenuSelectionManager.defaultManager().setSelectedPath(null);
 606 //                  menu.setPopupMenuVisible(false);
 607                 }
 608             }
 609             // now popup the new menu
 610             getSelectionModel().setSelectedIndex(i);
 611             JMenu menu = getMenu(i);
 612             if (menu != null) {
 613                 MenuElement me[] = new MenuElement[3];
 614                 me[0] = JMenuBar.this;
 615                 me[1] = menu;
 616                 me[2] = menu.getPopupMenu();
 617                 MenuSelectionManager.defaultManager().setSelectedPath(me);
 618 //              menu.setPopupMenuVisible(true);
 619             }
 620         }
 621 
 622         /**
 623          * Removes the nth selected item in the object from the object's
 624          * selection.  If the nth item isn't currently selected, this
 625          * method has no effect.  Otherwise, it closes the popup menu.
 626          *
 627          * @param i the zero-based index of selectable items
 628          */
 629         public void removeAccessibleSelection(int i) {
 630             if (i >= 0 && i < getMenuCount()) {
 631                 JMenu menu = getMenu(i);
 632                 if (menu != null) {
 633                     MenuSelectionManager.defaultManager().setSelectedPath(null);


   1 /*
   2  * Copyright (c) 1997, 2018, 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


 374      * its menus.  If there is no previous margin, it will create
 375      * a default margin with zero size.
 376      *
 377      * @return an <code>Insets</code> object containing the margin values
 378      * @see Insets
 379      */
 380     public Insets getMargin() {
 381         if(margin == null) {
 382             return new Insets(0,0,0,0);
 383         } else {
 384             return margin;
 385         }
 386     }
 387 
 388 
 389     /**
 390      * Implemented to be a <code>MenuElement</code> -- does nothing.
 391      *
 392      * @see #getSubElements
 393      */
 394     public void processMouseEvent(MouseEvent event,MenuElement[] path,MenuSelectionManager manager) {
 395     }
 396 
 397     /**
 398      * Implemented to be a <code>MenuElement</code> -- does nothing.
 399      *
 400      * @see #getSubElements
 401      */
 402     public void processKeyEvent(KeyEvent e,MenuElement[] path,MenuSelectionManager manager) {
 403     }
 404 
 405     /**
 406      * Implemented to be a <code>MenuElement</code> -- does nothing.
 407      *
 408      * @see #getSubElements
 409      */
 410     public void menuSelectionChanged(boolean isIncluded) {
 411     }
 412 
 413     /**
 414      * Implemented to be a <code>MenuElement</code> -- returns the
 415      * menus in this menu bar.
 416      * This is the reason for implementing the <code>MenuElement</code>
 417      * interface -- so that the menu bar can be treated the same as
 418      * other menu elements.
 419      * @return an array of menu items in the menu bar.
 420      */
 421     @BeanProperty(bound = false)
 422     public MenuElement[] getSubElements() {
 423         MenuElement[] result;
 424         Vector<MenuElement> tmp = new Vector<MenuElement>();
 425         int c = getComponentCount();
 426         int i;
 427         Component m;
 428 
 429         for(i=0 ; i < c ; i++) {
 430             m = getComponent(i);
 431             if(m instanceof MenuElement)
 432                 tmp.addElement((MenuElement) m);
 433         }
 434 
 435         result = new MenuElement[tmp.size()];
 436         for(i=0,c=tmp.size() ; i < c ; i++)
 437             result[i] = tmp.elementAt(i);
 438         return result;
 439     }
 440 
 441     /**
 442      * Implemented to be a <code>MenuElement</code>. Returns this object.
 443      *


 593          * @param i the zero-based index of selectable items
 594          * @see #getAccessibleStateSet
 595          */
 596         public void addAccessibleSelection(int i) {
 597             // first close up any open menu
 598             int j = getSelectionModel().getSelectedIndex();
 599             if (i == j) {
 600                 return;
 601             }
 602             if (j >= 0 && j < getMenuCount()) {
 603                 JMenu menu = getMenu(j);
 604                 if (menu != null) {
 605                     MenuSelectionManager.defaultManager().setSelectedPath(null);
 606 //                  menu.setPopupMenuVisible(false);
 607                 }
 608             }
 609             // now popup the new menu
 610             getSelectionModel().setSelectedIndex(i);
 611             JMenu menu = getMenu(i);
 612             if (menu != null) {
 613                 MenuElement[] me = new MenuElement[3];
 614                 me[0] = JMenuBar.this;
 615                 me[1] = menu;
 616                 me[2] = menu.getPopupMenu();
 617                 MenuSelectionManager.defaultManager().setSelectedPath(me);
 618 //              menu.setPopupMenuVisible(true);
 619             }
 620         }
 621 
 622         /**
 623          * Removes the nth selected item in the object from the object's
 624          * selection.  If the nth item isn't currently selected, this
 625          * method has no effect.  Otherwise, it closes the popup menu.
 626          *
 627          * @param i the zero-based index of selectable items
 628          */
 629         public void removeAccessibleSelection(int i) {
 630             if (i >= 0 && i < getMenuCount()) {
 631                 JMenu menu = getMenu(i);
 632                 if (menu != null) {
 633                     MenuSelectionManager.defaultManager().setSelectedPath(null);


< prev index next >