JCheckBoxMenuItem.java revision 0
0N/A/*
0N/A * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation. Sun designates this
0N/A * particular file as subject to the "Classpath" exception as provided
0N/A * by Sun in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any questions.
0N/A */
0N/Apackage javax.swing;
0N/A
0N/Aimport java.util.EventListener;
0N/A
0N/Aimport java.awt.*;
0N/Aimport java.awt.event.*;
0N/Aimport java.awt.image.*;
0N/A
0N/Aimport java.io.ObjectOutputStream;
0N/Aimport java.io.ObjectInputStream;
0N/Aimport java.io.IOException;
0N/A
0N/Aimport javax.swing.plaf.*;
0N/Aimport javax.accessibility.*;
0N/A
0N/A
0N/A/**
0N/A * A menu item that can be selected or deselected. If selected, the menu
0N/A * item typically appears with a checkmark next to it. If unselected or
0N/A * deselected, the menu item appears without a checkmark. Like a regular
0N/A * menu item, a check box menu item can have either text or a graphic
0N/A * icon associated with it, or both.
0N/A * <p>
0N/A * Either <code>isSelected</code>/<code>setSelected</code> or
0N/A * <code>getState</code>/<code>setState</code> can be used
0N/A * to determine/specify the menu item's selection state. The
0N/A * preferred methods are <code>isSelected</code> and
0N/A * <code>setSelected</code>, which work for all menus and buttons.
0N/A * The <code>getState</code> and <code>setState</code> methods exist for
0N/A * compatibility with other component sets.
0N/A * <p>
0N/A * Menu items can be configured, and to some degree controlled, by
0N/A * <code><a href="Action.html">Action</a></code>s. Using an
0N/A * <code>Action</code> with a menu item has many benefits beyond directly
0N/A * configuring a menu item. Refer to <a href="Action.html#buttonActions">
0N/A * Swing Components Supporting <code>Action</code></a> for more
0N/A * details, and you can find more information in <a
0N/A * href="http://java.sun.com/docs/books/tutorial/uiswing/misc/action.html">How
0N/A * to Use Actions</a>, a section in <em>The Java Tutorial</em>.
0N/A * <p>
0N/A * For further information and examples of using check box menu items,
0N/A * see <a
0N/A href="http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html">How to Use Menus</a>,
0N/A * a section in <em>The Java Tutorial.</em>
0N/A * <p>
0N/A * <strong>Warning:</strong> Swing is not thread safe. For more
0N/A * information see <a
0N/A * href="package-summary.html#threading">Swing's Threading
0N/A * Policy</a>.
0N/A * <p>
0N/A * <strong>Warning:</strong>
0N/A * Serialized objects of this class will not be compatible with
0N/A * future Swing releases. The current serialization support is
0N/A * appropriate for short term storage or RMI between applications running
0N/A * the same version of Swing. As of 1.4, support for long term storage
0N/A * of all JavaBeans<sup><font size="-2">TM</font></sup>
0N/A * has been added to the <code>java.beans</code> package.
0N/A * Please see {@link java.beans.XMLEncoder}.
0N/A *
0N/A * @beaninfo
0N/A * attribute: isContainer false
0N/A * description: A menu item which can be selected or deselected.
0N/A *
0N/A * @author Georges Saab
0N/A * @author David Karlton
0N/A */
0N/Apublic class JCheckBoxMenuItem extends JMenuItem implements SwingConstants,
0N/A Accessible
0N/A{
0N/A /**
0N/A * @see #getUIClassID
0N/A * @see #readObject
0N/A */
0N/A private static final String uiClassID = "CheckBoxMenuItemUI";
0N/A
0N/A /**
0N/A * Creates an initially unselected check box menu item with no set text or icon.
0N/A */
0N/A public JCheckBoxMenuItem() {
0N/A this(null, null, false);
0N/A }
0N/A
0N/A /**
0N/A * Creates an initially unselected check box menu item with an icon.
0N/A *
0N/A * @param icon the icon of the CheckBoxMenuItem.
0N/A */
0N/A public JCheckBoxMenuItem(Icon icon) {
0N/A this(null, icon, false);
0N/A }
0N/A
0N/A /**
0N/A * Creates an initially unselected check box menu item with text.
0N/A *
0N/A * @param text the text of the CheckBoxMenuItem
0N/A */
0N/A public JCheckBoxMenuItem(String text) {
0N/A this(text, null, false);
0N/A }
0N/A
0N/A /**
0N/A * Creates a menu item whose properties are taken from the
0N/A * Action supplied.
0N/A *
0N/A * @since 1.3
0N/A */
0N/A public JCheckBoxMenuItem(Action a) {
0N/A this();
0N/A setAction(a);
0N/A }
0N/A
0N/A /**
0N/A * Creates an initially unselected check box menu item with the specified text and icon.
0N/A *
0N/A * @param text the text of the CheckBoxMenuItem
0N/A * @param icon the icon of the CheckBoxMenuItem
0N/A */
0N/A public JCheckBoxMenuItem(String text, Icon icon) {
0N/A this(text, icon, false);
0N/A }
0N/A
0N/A /**
0N/A * Creates a check box menu item with the specified text and selection state.
0N/A *
0N/A * @param text the text of the check box menu item.
0N/A * @param b the selected state of the check box menu item
0N/A */
0N/A public JCheckBoxMenuItem(String text, boolean b) {
0N/A this(text, null, b);
0N/A }
0N/A
0N/A /**
0N/A * Creates a check box menu item with the specified text, icon, and selection state.
0N/A *
0N/A * @param text the text of the check box menu item
0N/A * @param icon the icon of the check box menu item
0N/A * @param b the selected state of the check box menu item
0N/A */
0N/A public JCheckBoxMenuItem(String text, Icon icon, boolean b) {
0N/A super(text, icon);
0N/A setModel(new JToggleButton.ToggleButtonModel());
0N/A setSelected(b);
0N/A setFocusable(false);
0N/A }
0N/A
0N/A /**
0N/A * Returns the name of the L&F class
0N/A * that renders this component.
0N/A *
0N/A * @return "CheckBoxMenuItemUI"
0N/A * @see JComponent#getUIClassID
0N/A * @see UIDefaults#getUI
0N/A */
0N/A public String getUIClassID() {
0N/A return uiClassID;
0N/A }
0N/A
0N/A /**
0N/A * Returns the selected-state of the item. This method
0N/A * exists for AWT compatibility only. New code should
0N/A * use isSelected() instead.
0N/A *
0N/A * @return true if the item is selected
0N/A */
0N/A public boolean getState() {
0N/A return isSelected();
0N/A }
0N/A
0N/A /**
0N/A * Sets the selected-state of the item. This method
0N/A * exists for AWT compatibility only. New code should
0N/A * use setSelected() instead.
0N/A *
0N/A * @param b a boolean value indicating the item's
0N/A * selected-state, where true=selected
0N/A * @beaninfo
0N/A * description: The selection state of the check box menu item
0N/A * hidden: true
0N/A */
0N/A public synchronized void setState(boolean b) {
0N/A setSelected(b);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns an array (length 1) containing the check box menu item
0N/A * label or null if the check box is not selected.
0N/A *
0N/A * @return an array containing one Object -- the text of the menu item
0N/A * -- if the item is selected; otherwise null
0N/A */
0N/A public Object[] getSelectedObjects() {
0N/A if (isSelected() == false)
0N/A return null;
0N/A Object[] selectedObjects = new Object[1];
0N/A selectedObjects[0] = getText();
0N/A return selectedObjects;
0N/A }
0N/A
0N/A /**
0N/A * See readObject() and writeObject() in JComponent for more
0N/A * information about serialization in Swing.
0N/A */
0N/A private void writeObject(ObjectOutputStream s) throws IOException {
0N/A s.defaultWriteObject();
0N/A if (getUIClassID().equals(uiClassID)) {
0N/A byte count = JComponent.getWriteObjCounter(this);
0N/A JComponent.setWriteObjCounter(this, --count);
0N/A if (count == 0 && ui != null) {
0N/A ui.installUI(this);
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns a string representation of this JCheckBoxMenuItem. This method
0N/A * is intended to be used only for debugging purposes, and the
0N/A * content and format of the returned string may vary between
0N/A * implementations. The returned string may be empty but may not
0N/A * be <code>null</code>.
0N/A *
0N/A * @return a string representation of this JCheckBoxMenuItem.
0N/A */
0N/A protected String paramString() {
0N/A return super.paramString();
0N/A }
0N/A
0N/A /**
0N/A * Overriden to return true, JCheckBoxMenuItem supports
0N/A * the selected state.
0N/A */
0N/A boolean shouldUpdateSelectedStateFromAction() {
0N/A return true;
0N/A }
0N/A
0N/A/////////////////
0N/A// Accessibility support
0N/A////////////////
0N/A
0N/A /**
0N/A * Gets the AccessibleContext associated with this JCheckBoxMenuItem.
0N/A * For JCheckBoxMenuItems, the AccessibleContext takes the form of an
0N/A * AccessibleJCheckBoxMenuItem.
0N/A * A new AccessibleJCheckBoxMenuItem instance is created if necessary.
0N/A *
0N/A * @return an AccessibleJCheckBoxMenuItem that serves as the
0N/A * AccessibleContext of this AccessibleJCheckBoxMenuItem
0N/A */
0N/A public AccessibleContext getAccessibleContext() {
0N/A if (accessibleContext == null) {
0N/A accessibleContext = new AccessibleJCheckBoxMenuItem();
0N/A }
0N/A return accessibleContext;
0N/A }
0N/A
0N/A /**
0N/A * This class implements accessibility support for the
0N/A * <code>JCheckBoxMenuItem</code> class. It provides an implementation
0N/A * of the Java Accessibility API appropriate to checkbox menu item
0N/A * user-interface elements.
0N/A * <p>
0N/A * <strong>Warning:</strong>
0N/A * Serialized objects of this class will not be compatible with
0N/A * future Swing releases. The current serialization support is
0N/A * appropriate for short term storage or RMI between applications running
0N/A * the same version of Swing. As of 1.4, support for long term storage
0N/A * of all JavaBeans<sup><font size="-2">TM</font></sup>
0N/A * has been added to the <code>java.beans</code> package.
0N/A * Please see {@link java.beans.XMLEncoder}.
0N/A */
0N/A protected class AccessibleJCheckBoxMenuItem extends AccessibleJMenuItem {
0N/A /**
0N/A * Get the role of this object.
0N/A *
0N/A * @return an instance of AccessibleRole describing the role of the
0N/A * object
0N/A */
0N/A public AccessibleRole getAccessibleRole() {
0N/A return AccessibleRole.CHECK_BOX;
0N/A }
0N/A } // inner class AccessibleJCheckBoxMenuItem
0N/A}
0N/A