/*
* Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package java.awt;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Vector;
import java.util.Enumeration;
import java.awt.peer.MenuPeer;
import java.awt.event.KeyEvent;
import javax.accessibility.*;
import sun.awt.AWTAccessor;
/**
* A Menu
object is a pull-down menu component
* that is deployed from a menu bar.
*
* A menu can optionally be a tear-off menu. A tear-off menu * can be opened and dragged away from its parent menu bar or menu. * It remains on the screen after the mouse button has been released. * The mechanism for tearing off a menu is platform dependent, since * the look and feel of the tear-off menu is determined by its peer. * On platforms that do not support tear-off menus, the tear-off * property is ignored. *
* Each item in a menu must belong to the
* Tear-off functionality may not be supported by all
* implementations of AWT. If a particular implementation doesn't
* support tear-off menus, this value is silently ignored.
* @param label the menu's label in the menu bar, or in
* another menu of which this menu is a submenu.
* @param tearOff if
* Tear-off functionality may not be supported by all
* implementations of AWT. If a particular implementation doesn't
* support tear-off menus, this value is silently ignored.
* @return MenuItem
* class. It can be an instance of MenuItem
, a submenu
* (an instance of Menu
), or a check box (an instance of
* CheckboxMenuItem
).
*
* @author Sami Shaio
* @see java.awt.MenuItem
* @see java.awt.CheckboxMenuItem
* @since JDK1.0
*/
public class Menu extends MenuItem implements MenuContainer, Accessible {
static {
/* ensure that the necessary native libraries are loaded */
Toolkit.loadLibraries();
if (!GraphicsEnvironment.isHeadless()) {
initIDs();
}
AWTAccessor.setMenuAccessor(
new AWTAccessor.MenuAccessor() {
public Vectortrue
if the menu has the tear off
* property and it will be set to false
* if it does not.
* A torn off menu can be deleted by a user when
* it is no longer needed.
*
* @serial
* @see #isTearOff()
*/
boolean tearOff;
/**
* This field will be set to true
* if the Menu in question is actually a help
* menu. Otherwise it will be set to
* false
.
*
* @serial
*/
boolean isHelpMenu;
private static final String base = "menu";
private static int nameCounter = 0;
/*
* JDK 1.1 serialVersionUID
*/
private static final long serialVersionUID = -8809584163345499784L;
/**
* Constructs a new menu with an empty label. This menu is not
* a tear-off menu.
* @exception HeadlessException if GraphicsEnvironment.isHeadless()
* returns true.
* @see java.awt.GraphicsEnvironment#isHeadless
* @since JDK1.1
*/
public Menu() throws HeadlessException {
this("", false);
}
/**
* Constructs a new menu with the specified label. This menu is not
* a tear-off menu.
* @param label the menu's label in the menu bar, or in
* another menu of which this menu is a submenu.
* @exception HeadlessException if GraphicsEnvironment.isHeadless()
* returns true.
* @see java.awt.GraphicsEnvironment#isHeadless
*/
public Menu(String label) throws HeadlessException {
this(label, false);
}
/**
* Constructs a new menu with the specified label,
* indicating whether the menu can be torn off.
* true
, the menu
* is a tear-off menu.
* @exception HeadlessException if GraphicsEnvironment.isHeadless()
* returns true.
* @see java.awt.GraphicsEnvironment#isHeadless
* @since JDK1.0.
*/
public Menu(String label, boolean tearOff) throws HeadlessException {
super(label);
this.tearOff = tearOff;
}
/**
* Construct a name for this MenuComponent. Called by getName() when
* the name is null.
*/
String constructComponentName() {
synchronized (Menu.class) {
return base + nameCounter++;
}
}
/**
* Creates the menu's peer. The peer allows us to modify the
* appearance of the menu without changing its functionality.
*/
public void addNotify() {
synchronized (getTreeLock()) {
if (peer == null)
peer = Toolkit.getDefaultToolkit().createMenu(this);
int nitems = getItemCount();
for (int i = 0 ; i < nitems ; i++) {
MenuItem mi = getItem(i);
mi.parent = this;
mi.addNotify();
}
}
}
/**
* Removes the menu's peer. The peer allows us to modify the appearance
* of the menu without changing its functionality.
*/
public void removeNotify() {
synchronized (getTreeLock()) {
int nitems = getItemCount();
for (int i = 0 ; i < nitems ; i++) {
getItem(i).removeNotify();
}
super.removeNotify();
}
}
/**
* Indicates whether this menu is a tear-off menu.
* true
if this is a tear-off menu;
* false
otherwise.
*/
public boolean isTearOff() {
return tearOff;
}
/**
* Get the number of items in this menu.
* @return the number of items in this menu.
* @since JDK1.1
*/
public int getItemCount() {
return countItems();
}
/**
* @deprecated As of JDK version 1.1,
* replaced by getItemCount()
.
*/
@Deprecated
public int countItems() {
return countItemsImpl();
}
/*
* This is called by the native code, so client code can't
* be called on the toolkit thread.
*/
final int countItemsImpl() {
return items.size();
}
/**
* Gets the item located at the specified index of this menu.
* @param index the position of the item to be returned.
* @return the item located at the specified index.
*/
public MenuItem getItem(int index) {
return getItemImpl(index);
}
/*
* This is called by the native code, so client code can't
* be called on the toolkit thread.
*/
final MenuItem getItemImpl(int index) {
return (MenuItem)items.elementAt(index);
}
/**
* Adds the specified menu item to this menu. If the
* menu item has been part of another menu, removes it
* from that menu.
*
* @param mi the menu item to be added
* @return the menu item added
* @see java.awt.Menu#insert(java.lang.String, int)
* @see java.awt.Menu#insert(java.awt.MenuItem, int)
*/
public MenuItem add(MenuItem mi) {
synchronized (getTreeLock()) {
if (mi.parent != null) {
mi.parent.remove(mi);
}
items.addElement(mi);
mi.parent = this;
MenuPeer peer = (MenuPeer)this.peer;
if (peer != null) {
mi.addNotify();
peer.addItem(mi);
}
return mi;
}
}
/**
* Adds an item with the specified label to this menu.
*
* @param label the text on the item
* @see java.awt.Menu#insert(java.lang.String, int)
* @see java.awt.Menu#insert(java.awt.MenuItem, int)
*/
public void add(String label) {
add(new MenuItem(label));
}
/**
* Inserts a menu item into this menu
* at the specified position.
*
* @param menuitem the menu item to be inserted.
* @param index the position at which the menu
* item should be inserted.
* @see java.awt.Menu#add(java.lang.String)
* @see java.awt.Menu#add(java.awt.MenuItem)
* @exception IllegalArgumentException if the value of
* index
is less than zero
* @since JDK1.1
*/
public void insert(MenuItem menuitem, int index) {
synchronized (getTreeLock()) {
if (index < 0) {
throw new IllegalArgumentException("index less than zero.");
}
int nitems = getItemCount();
Vector