JMenuBar.java revision 625
/*
* Copyright 1997-2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
import javax.accessibility.*;
/**
* An implementation of a menu bar. You add <code>JMenu</code> objects to the
* menu bar to construct a menu. When the user selects a <code>JMenu</code>
* object, its associated <code>JPopupMenu</code> is displayed, allowing the
* user to select one of the <code>JMenuItems</code> on it.
* <p>
* For information and examples of using menu bars see
* <a
href="http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html">How to Use Menus</a>,
* a section in <em>The Java Tutorial.</em>
* <p>
* <strong>Warning:</strong> Swing is not thread safe. For more
* information see <a
* href="package-summary.html#threading">Swing's Threading
* Policy</a>.
* <p>
* <strong>Warning:</strong>
* Serialized objects of this class will not be compatible with
* future Swing releases. The current serialization support is
* appropriate for short term storage or RMI between applications running
* the same version of Swing. As of 1.4, support for long term storage
* of all JavaBeans<sup><font size="-2">TM</font></sup>
* has been added to the <code>java.beans</code> package.
* Please see {@link java.beans.XMLEncoder}.
*
* @beaninfo
* attribute: isContainer true
* description: A container for holding and displaying menus.
*
* @author Georges Saab
* @author David Karlton
* @author Arnaud Weber
* @see JMenu
* @see JPopupMenu
* @see JMenuItem
*/
{
/**
* @see #getUIClassID
* @see #readObject
*/
/*
* Model for the selected subcontrol.
*/
private transient SingleSelectionModel selectionModel;
private boolean paintBorder = true;
/* diagnostic aids -- should be false for production builds. */
private static final boolean TRACE = false; // trace creates and disposes
private static final boolean DEBUG = false; // show bad params, misc.
/**
* Creates a new menu bar.
*/
public JMenuBar() {
super();
setFocusTraversalKeysEnabled(false);
updateUI();
}
/**
* Returns the menubar's current UI.
* @see #setUI
*/
}
/**
* Sets the L&F object that renders this component.
*
* @param ui the new MenuBarUI L&F object
* @see UIDefaults#getUI
* @beaninfo
* bound: true
* hidden: true
* attribute: visualUpdate true
* description: The UI object that implements the Component's LookAndFeel.
*/
}
/**
* Resets the UI property with a value from the current look and feel.
*
* @see JComponent#updateUI
*/
public void updateUI() {
}
/**
* Returns the name of the L&F class that renders this component.
*
* @return the string "MenuBarUI"
* @see JComponent#getUIClassID
* @see UIDefaults#getUI
*/
public String getUIClassID() {
return uiClassID;
}
/**
* Returns the model object that handles single selections.
*
* @return the <code>SingleSelectionModel</code> property
* @see SingleSelectionModel
*/
public SingleSelectionModel getSelectionModel() {
return selectionModel;
}
/**
* Sets the model object to handle single selections.
*
* @param model the <code>SingleSelectionModel</code> to use
* @see SingleSelectionModel
* @beaninfo
* bound: true
* description: The selection model, recording which child is selected.
*/
this.selectionModel = model;
}
/**
* Appends the specified menu to the end of the menu bar.
*
* @param c the <code>JMenu</code> component to add
* @return the menu component
*/
super.add(c);
return c;
}
/**
* Returns the menu at the specified position in the menu bar.
*
* @param index an integer giving the position in the menu bar, where
* 0 is the first position
* @return the <code>JMenu</code> at that position, or <code>null</code> if
* if there is no <code>JMenu</code> at that position (ie. if
* it is a <code>JMenuItem</code>)
*/
if (c instanceof JMenu)
return (JMenu) c;
return null;
}
/**
* Returns the number of items in the menu bar.
*
* @return the number of items in the menu bar
*/
public int getMenuCount() {
return getComponentCount();
}
/**
* Sets the help menu that appears when the user selects the
* "help" option in the menu bar. This method is not yet implemented
* and will throw an exception.
*
* @param menu the JMenu that delivers help to the user
*/
throw new Error("setHelpMenu() not yet implemented.");
}
/**
* Gets the help menu for the menu bar. This method is not yet
* implemented and will throw an exception.
*
* @return the <code>JMenu</code> that delivers help to the user
*/
public JMenu getHelpMenu() {
throw new Error("getHelpMenu() not yet implemented.");
}
/**
* Returns the component at the specified index.
*
* @param i an integer specifying the position, where 0 is first
* @return the <code>Component</code> at the position,
* or <code>null</code> for an invalid index
* @deprecated replaced by <code>getComponent(int i)</code>
*/
public Component getComponentAtIndex(int i) {
if(i < 0 || i >= getComponentCount()) {
return null;
}
return getComponent(i);
}
/**
* Returns the index of the specified component.
*
* @param c the <code>Component</code> to find
* @return an integer giving the component's position, where 0 is first;
* or -1 if it can't be found
*/
public int getComponentIndex(Component c) {
int ncomponents = this.getComponentCount();
for (int i = 0 ; i < ncomponents ; i++) {
if (comp == c)
return i;
}
return -1;
}
/**
* Sets the currently selected component, producing a
* a change to the selection model.
*
* @param sel the <code>Component</code> to select
*/
}
/**
* Returns true if the menu bar currently has a component selected.
*
* @return true if a selection has been made, else false
*/
public boolean isSelected() {
return selectionModel.isSelected();
}
/**
* Returns true if the menu bars border should be painted.
*
* @return true if the border should be painted, else false
*/
public boolean isBorderPainted() {
return paintBorder;
}
/**
* Sets whether the border should be painted.
*
* @param b if true and border property is not <code>null</code>,
* the border is painted.
* @see #isBorderPainted
* @beaninfo
* bound: true
* attribute: visualUpdate true
* description: Whether the border should be painted.
*/
public void setBorderPainted(boolean b) {
boolean oldValue = paintBorder;
paintBorder = b;
if (b != oldValue) {
revalidate();
repaint();
}
}
/**
* Paints the menubar's border if <code>BorderPainted</code>
* property is true.
*
* @param g the <code>Graphics</code> context to use for painting
* @see JComponent#paint
* @see JComponent#setBorder
*/
protected void paintBorder(Graphics g) {
if (isBorderPainted()) {
super.paintBorder(g);
}
}
/**
* Sets the margin between the menubar's border and
* its menus. Setting to <code>null</code> will cause the menubar to
* use the default margins.
*
* @param m an Insets object containing the margin values
* @see Insets
* @beaninfo
* bound: true
* attribute: visualUpdate true
* description: The space between the menubar's border and its contents
*/
this.margin = m;
revalidate();
repaint();
}
}
/**
* Returns the margin between the menubar's border and
* its menus. If there is no previous margin, it will create
* a default margin with zero size.
*
* @return an <code>Insets</code> object containing the margin values
* @see Insets
*/
} else {
return margin;
}
}
/**
* Implemented to be a <code>MenuElement</code> -- does nothing.
*
* @see #getSubElements
*/
}
/**
* Implemented to be a <code>MenuElement</code> -- does nothing.
*
* @see #getSubElements
*/
}
/**
* Implemented to be a <code>MenuElement</code> -- does nothing.
*
* @see #getSubElements
*/
public void menuSelectionChanged(boolean isIncluded) {
}
/**
* Implemented to be a <code>MenuElement</code> -- returns the
* menus in this menu bar.
* This is the reason for implementing the <code>MenuElement</code>
* interface -- so that the menu bar can be treated the same as
* other menu elements.
* @return an array of menu items in the menu bar.
*/
public MenuElement[] getSubElements() {
int c = getComponentCount();
int i;
Component m;
for(i=0 ; i < c ; i++) {
m = getComponent(i);
if(m instanceof MenuElement)
}
return result;
}
/**
* Implemented to be a <code>MenuElement</code>. Returns this object.
*
* @return the current <code>Component</code> (this)
* @see #getSubElements
*/
public Component getComponent() {
return this;
}
/**
* Returns a string representation of this <code>JMenuBar</code>.
* This method
* is intended to be used only for debugging purposes, and the
* content and format of the returned string may vary between
* implementations. The returned string may be empty but may not
* be <code>null</code>.
*
* @return a string representation of this <code>JMenuBar</code>
*/
protected String paramString() {
"true" : "false");
return super.paramString() +
",margin=" + marginString +
",paintBorder=" + paintBorderString;
}
/////////////////
// Accessibility support
////////////////
/**
* Gets the AccessibleContext associated with this JMenuBar.
* For JMenuBars, the AccessibleContext takes the form of an
* AccessibleJMenuBar.
* A new AccessibleJMenuBar instance is created if necessary.
*
* @return an AccessibleJMenuBar that serves as the
* AccessibleContext of this JMenuBar
*/
public AccessibleContext getAccessibleContext() {
if (accessibleContext == null) {
accessibleContext = new AccessibleJMenuBar();
}
return accessibleContext;
}
/**
* This class implements accessibility support for the
* <code>JMenuBar</code> class. It provides an implementation of the
* Java Accessibility API appropriate to menu bar user-interface
* elements.
* <p>
* <strong>Warning:</strong>
* Serialized objects of this class will not be compatible with
* future Swing releases. The current serialization support is
* appropriate for short term storage or RMI between applications running
* the same version of Swing. As of 1.4, support for long term storage
* of all JavaBeans<sup><font size="-2">TM</font></sup>
* has been added to the <code>java.beans</code> package.
* Please see {@link java.beans.XMLEncoder}.
*/
protected class AccessibleJMenuBar extends AccessibleJComponent
implements AccessibleSelection {
/**
* Get the accessible state set of this object.
*
* @return an instance of AccessibleState containing the current state
* of the object
*/
public AccessibleStateSet getAccessibleStateSet() {
return states;
}
/**
* Get the role of this object.
*
* @return an instance of AccessibleRole describing the role of the
* object
*/
public AccessibleRole getAccessibleRole() {
return AccessibleRole.MENU_BAR;
}
/**
* Get the AccessibleSelection associated with this object. In the
* implementation of the Java Accessibility API for this class,
* return this object, which is responsible for implementing the
* AccessibleSelection interface on behalf of itself.
*
* @return this object
*/
public AccessibleSelection getAccessibleSelection() {
return this;
}
/**
* Returns 1 if a menu is currently selected in this menu bar.
*
* @return 1 if a menu is currently selected, else 0
*/
public int getAccessibleSelectionCount() {
if (isSelected()) {
return 1;
} else {
return 0;
}
}
/**
* Returns the currently selected menu if one is selected,
* otherwise null.
*/
public Accessible getAccessibleSelection(int i) {
if (isSelected()) {
if (i != 0) { // single selection model for JMenuBar
return null;
}
int j = getSelectionModel().getSelectedIndex();
if (getComponentAtIndex(j) instanceof Accessible) {
return (Accessible) getComponentAtIndex(j);
}
}
return null;
}
/**
* Returns true if the current child of this object is selected.
*
* @param i the zero-based index of the child in this Accessible
* object.
* @see AccessibleContext#getAccessibleChild
*/
public boolean isAccessibleChildSelected(int i) {
return (i == getSelectionModel().getSelectedIndex());
}
/**
* Selects the nth menu in the menu bar, forcing it to
* pop up. If another menu is popped up, this will force
* it to close. If the nth menu is already selected, this
* method has no effect.
*
* @param i the zero-based index of selectable items
* @see #getAccessibleStateSet
*/
public void addAccessibleSelection(int i) {
// first close up any open menu
int j = getSelectionModel().getSelectedIndex();
if (i == j) {
return;
}
if (j >= 0 && j < getMenuCount()) {
// menu.setPopupMenuVisible(false);
}
}
// now popup the new menu
// menu.setPopupMenuVisible(true);
}
}
/**
* Removes the nth selected item in the object from the object's
* selection. If the nth item isn't currently selected, this
* method has no effect. Otherwise, it closes the popup menu.
*
* @param i the zero-based index of selectable items
*/
public void removeAccessibleSelection(int i) {
if (i >= 0 && i < getMenuCount()) {
// menu.setPopupMenuVisible(false);
}
}
}
/**
* Clears the selection in the object, so that nothing in the
* object is selected. This will close any open menu.
*/
public void clearAccessibleSelection() {
int i = getSelectionModel().getSelectedIndex();
if (i >= 0 && i < getMenuCount()) {
// menu.setPopupMenuVisible(false);
}
}
}
/**
* Normally causes every selected item in the object to be selected
* if the object supports multiple selections. This method
* makes no sense in a menu bar, and so does nothing.
*/
public void selectAllAccessibleSelection() {
}
} // internal class AccessibleJMenuBar
/**
* Subclassed to check all the child menus.
* @since 1.3
*/
// See if we have a local binding.
if (!retValue) {
return true;
}
}
}
return retValue;
}
return false;
}
return false;
}
if (c != null && c instanceof JComponent &&
return true;
}
return true;
// We don't, pass along to children JMenu's
}
}
return false;
}
/**
* Overrides <code>JComponent.addNotify</code> to register this
* menu bar with the current keyboard manager.
*/
public void addNotify() {
super.addNotify();
}
/**
* Overrides <code>JComponent.removeNotify</code> to unregister this
* menu bar with the current keyboard manager.
*/
public void removeNotify() {
super.removeNotify();
}
s.defaultWriteObject();
}
}
int n = 0;
if (selectionModel instanceof Serializable) {
kvData[n++] = "selectionModel";
kvData[n++] = selectionModel;
}
s.writeObject(kvData);
}
/**
* See JComponent.readObject() for information about serialization
* in Swing.
*/
{
s.defaultReadObject();
break;
}
}
}
}
}