< prev index next >

src/java.desktop/share/classes/java/awt/Menu.java

Print this page


   1 /*
   2  * Copyright (c) 1995, 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 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 java.awt.peer.MenuPeer;
  32 import java.awt.event.KeyEvent;
  33 import javax.accessibility.*;



  34 import sun.awt.AWTAccessor;
  35 
  36 /**
  37  * A {@code Menu} object is a pull-down menu component
  38  * that is deployed from a menu bar.
  39  * <p>
  40  * A menu can optionally be a <i>tear-off</i> menu. A tear-off menu
  41  * can be opened and dragged away from its parent menu bar or menu.
  42  * It remains on the screen after the mouse button has been released.
  43  * The mechanism for tearing off a menu is platform dependent, since
  44  * the look and feel of the tear-off menu is determined by its peer.
  45  * On platforms that do not support tear-off menus, the tear-off
  46  * property is ignored.
  47  * <p>
  48  * Each item in a menu must belong to the {@code MenuItem}
  49  * class. It can be an instance of {@code MenuItem}, a submenu
  50  * (an instance of {@code Menu}), or a check box (an instance of
  51  * {@code CheckboxMenuItem}).
  52  *
  53  * @author Sami Shaio


  61         /* ensure that the necessary native libraries are loaded */
  62         Toolkit.loadLibraries();
  63         if (!GraphicsEnvironment.isHeadless()) {
  64             initIDs();
  65         }
  66 
  67         AWTAccessor.setMenuAccessor(
  68             new AWTAccessor.MenuAccessor() {
  69                 public Vector<MenuItem> getItems(Menu menu) {
  70                     return menu.items;
  71                 }
  72             });
  73     }
  74 
  75     /**
  76      * A vector of the items that will be part of the Menu.
  77      *
  78      * @serial
  79      * @see #countItems()
  80      */
  81     Vector<MenuItem> items = new Vector<>();
  82 
  83     /**
  84      * This field indicates whether the menu has the
  85      * tear of property or not.  It will be set to
  86      * {@code true} if the menu has the tear off
  87      * property and it will be set to {@code false}
  88      * if it does not.
  89      * A torn off menu can be deleted by a user when
  90      * it is no longer needed.
  91      *
  92      * @serial
  93      * @see #isTearOff()
  94      */
  95     boolean             tearOff;
  96 
  97     /**
  98      * This field will be set to {@code true}
  99      * if the Menu in question is actually a help
 100      * menu.  Otherwise it will be set to
 101      * {@code false}.
 102      *
 103      * @serial
 104      */
 105     boolean             isHelpMenu;
 106 
 107     private static final String base = "menu";
 108     private static int nameCounter = 0;
 109 
 110     /*
 111      * JDK 1.1 serialVersionUID
 112      */
 113      private static final long serialVersionUID = -8809584163345499784L;
 114 
 115     /**
 116      * Constructs a new menu with an empty label. This menu is not
 117      * a tear-off menu.
 118      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
 119      * returns true.
 120      * @see java.awt.GraphicsEnvironment#isHeadless
 121      * @since      1.1
 122      */
 123     public Menu() throws HeadlessException {
 124         this("", false);
 125     }


 398                already in the correct order in the temp vector.
 399             */
 400             for (int i = 0; i < tempItems.size()  ; i++) {
 401                 add(tempItems.elementAt(i));
 402             }
 403         }
 404     }
 405 
 406     /**
 407      * Removes the menu item at the specified index from this menu.
 408      * @param       index the position of the item to be removed.
 409      */
 410     public void remove(int index) {
 411         synchronized (getTreeLock()) {
 412             MenuItem mi = getItem(index);
 413             items.removeElementAt(index);
 414             MenuPeer peer = (MenuPeer)this.peer;
 415             if (peer != null) {
 416                 peer.delItem(index);
 417                 mi.removeNotify();
 418                 mi.parent = null;
 419             }

 420         }
 421     }
 422 
 423     /**
 424      * Removes the specified menu item from this menu.
 425      * @param  item the item to be removed from the menu.
 426      *         If {@code item} is {@code null}
 427      *         or is not in this menu, this method does
 428      *         nothing.
 429      */
 430     public void remove(MenuComponent item) {
 431         synchronized (getTreeLock()) {
 432             int index = items.indexOf(item);
 433             if (index >= 0) {
 434                 remove(index);
 435             }
 436         }
 437     }
 438 
 439     /**


   1 /*
   2  * Copyright (c) 1995, 2016, 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 
  26 package java.awt;
  27 
  28 import java.awt.event.KeyEvent;
  29 import java.awt.peer.MenuPeer;
  30 import java.io.IOException;
  31 import java.io.ObjectInputStream;

  32 import java.util.Enumeration;
  33 import java.util.Vector;
  34 
  35 import javax.accessibility.Accessible;
  36 import javax.accessibility.AccessibleContext;
  37 import javax.accessibility.AccessibleRole;
  38 
  39 import sun.awt.AWTAccessor;
  40 
  41 /**
  42  * A {@code Menu} object is a pull-down menu component
  43  * that is deployed from a menu bar.
  44  * <p>
  45  * A menu can optionally be a <i>tear-off</i> menu. A tear-off menu
  46  * can be opened and dragged away from its parent menu bar or menu.
  47  * It remains on the screen after the mouse button has been released.
  48  * The mechanism for tearing off a menu is platform dependent, since
  49  * the look and feel of the tear-off menu is determined by its peer.
  50  * On platforms that do not support tear-off menus, the tear-off
  51  * property is ignored.
  52  * <p>
  53  * Each item in a menu must belong to the {@code MenuItem}
  54  * class. It can be an instance of {@code MenuItem}, a submenu
  55  * (an instance of {@code Menu}), or a check box (an instance of
  56  * {@code CheckboxMenuItem}).
  57  *
  58  * @author Sami Shaio


  66         /* ensure that the necessary native libraries are loaded */
  67         Toolkit.loadLibraries();
  68         if (!GraphicsEnvironment.isHeadless()) {
  69             initIDs();
  70         }
  71 
  72         AWTAccessor.setMenuAccessor(
  73             new AWTAccessor.MenuAccessor() {
  74                 public Vector<MenuItem> getItems(Menu menu) {
  75                     return menu.items;
  76                 }
  77             });
  78     }
  79 
  80     /**
  81      * A vector of the items that will be part of the Menu.
  82      *
  83      * @serial
  84      * @see #countItems()
  85      */
  86     private final Vector<MenuItem> items = new Vector<>();
  87 
  88     /**
  89      * This field indicates whether the menu has the
  90      * tear of property or not.  It will be set to
  91      * {@code true} if the menu has the tear off
  92      * property and it will be set to {@code false}
  93      * if it does not.
  94      * A torn off menu can be deleted by a user when
  95      * it is no longer needed.
  96      *
  97      * @serial
  98      * @see #isTearOff()
  99      */
 100     private final boolean tearOff;
 101 
 102     /**
 103      * This field will be set to {@code true}
 104      * if the Menu in question is actually a help
 105      * menu.  Otherwise it will be set to
 106      * {@code false}.
 107      *
 108      * @serial
 109      */
 110     volatile boolean isHelpMenu;
 111 
 112     private static final String base = "menu";
 113     private static int nameCounter = 0;
 114 
 115     /*
 116      * JDK 1.1 serialVersionUID
 117      */
 118      private static final long serialVersionUID = -8809584163345499784L;
 119 
 120     /**
 121      * Constructs a new menu with an empty label. This menu is not
 122      * a tear-off menu.
 123      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
 124      * returns true.
 125      * @see java.awt.GraphicsEnvironment#isHeadless
 126      * @since      1.1
 127      */
 128     public Menu() throws HeadlessException {
 129         this("", false);
 130     }


 403                already in the correct order in the temp vector.
 404             */
 405             for (int i = 0; i < tempItems.size()  ; i++) {
 406                 add(tempItems.elementAt(i));
 407             }
 408         }
 409     }
 410 
 411     /**
 412      * Removes the menu item at the specified index from this menu.
 413      * @param       index the position of the item to be removed.
 414      */
 415     public void remove(int index) {
 416         synchronized (getTreeLock()) {
 417             MenuItem mi = getItem(index);
 418             items.removeElementAt(index);
 419             MenuPeer peer = (MenuPeer)this.peer;
 420             if (peer != null) {
 421                 peer.delItem(index);
 422                 mi.removeNotify();

 423             }
 424             mi.parent = null;
 425         }
 426     }
 427 
 428     /**
 429      * Removes the specified menu item from this menu.
 430      * @param  item the item to be removed from the menu.
 431      *         If {@code item} is {@code null}
 432      *         or is not in this menu, this method does
 433      *         nothing.
 434      */
 435     public void remove(MenuComponent item) {
 436         synchronized (getTreeLock()) {
 437             int index = items.indexOf(item);
 438             if (index >= 0) {
 439                 remove(index);
 440             }
 441         }
 442     }
 443 
 444     /**


< prev index next >