J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1997-2006 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | package javax.swing; |
| 26 | |
| 27 | import java.util.EventListener; |
| 28 | |
| 29 | import java.awt.*; |
| 30 | import java.awt.event.*; |
| 31 | import java.awt.image.*; |
| 32 | |
| 33 | import java.io.ObjectOutputStream; |
| 34 | import java.io.ObjectInputStream; |
| 35 | import java.io.IOException; |
| 36 | |
| 37 | import javax.swing.plaf.*; |
| 38 | import javax.accessibility.*; |
| 39 | |
| 40 | |
| 41 | /** |
| 42 | * A menu item that can be selected or deselected. If selected, the menu |
| 43 | * item typically appears with a checkmark next to it. If unselected or |
| 44 | * deselected, the menu item appears without a checkmark. Like a regular |
| 45 | * menu item, a check box menu item can have either text or a graphic |
| 46 | * icon associated with it, or both. |
| 47 | * <p> |
| 48 | * Either <code>isSelected</code>/<code>setSelected</code> or |
| 49 | * <code>getState</code>/<code>setState</code> can be used |
| 50 | * to determine/specify the menu item's selection state. The |
| 51 | * preferred methods are <code>isSelected</code> and |
| 52 | * <code>setSelected</code>, which work for all menus and buttons. |
| 53 | * The <code>getState</code> and <code>setState</code> methods exist for |
| 54 | * compatibility with other component sets. |
| 55 | * <p> |
| 56 | * Menu items can be configured, and to some degree controlled, by |
| 57 | * <code><a href="Action.html">Action</a></code>s. Using an |
| 58 | * <code>Action</code> with a menu item has many benefits beyond directly |
| 59 | * configuring a menu item. Refer to <a href="Action.html#buttonActions"> |
| 60 | * Swing Components Supporting <code>Action</code></a> for more |
| 61 | * details, and you can find more information in <a |
| 62 | * href="http://java.sun.com/docs/books/tutorial/uiswing/misc/action.html">How |
| 63 | * to Use Actions</a>, a section in <em>The Java Tutorial</em>. |
| 64 | * <p> |
| 65 | * For further information and examples of using check box menu items, |
| 66 | * see <a |
| 67 | href="http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html">How to Use Menus</a>, |
| 68 | * a section in <em>The Java Tutorial.</em> |
| 69 | * <p> |
| 70 | * <strong>Warning:</strong> Swing is not thread safe. For more |
| 71 | * information see <a |
| 72 | * href="package-summary.html#threading">Swing's Threading |
| 73 | * Policy</a>. |
| 74 | * <p> |
| 75 | * <strong>Warning:</strong> |
| 76 | * Serialized objects of this class will not be compatible with |
| 77 | * future Swing releases. The current serialization support is |
| 78 | * appropriate for short term storage or RMI between applications running |
| 79 | * the same version of Swing. As of 1.4, support for long term storage |
| 80 | * of all JavaBeans<sup><font size="-2">TM</font></sup> |
| 81 | * has been added to the <code>java.beans</code> package. |
| 82 | * Please see {@link java.beans.XMLEncoder}. |
| 83 | * |
| 84 | * @beaninfo |
| 85 | * attribute: isContainer false |
| 86 | * description: A menu item which can be selected or deselected. |
| 87 | * |
| 88 | * @author Georges Saab |
| 89 | * @author David Karlton |
| 90 | */ |
| 91 | public class JCheckBoxMenuItem extends JMenuItem implements SwingConstants, |
| 92 | Accessible |
| 93 | { |
| 94 | /** |
| 95 | * @see #getUIClassID |
| 96 | * @see #readObject |
| 97 | */ |
| 98 | private static final String uiClassID = "CheckBoxMenuItemUI"; |
| 99 | |
| 100 | /** |
| 101 | * Creates an initially unselected check box menu item with no set text or icon. |
| 102 | */ |
| 103 | public JCheckBoxMenuItem() { |
| 104 | this(null, null, false); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Creates an initially unselected check box menu item with an icon. |
| 109 | * |
| 110 | * @param icon the icon of the CheckBoxMenuItem. |
| 111 | */ |
| 112 | public JCheckBoxMenuItem(Icon icon) { |
| 113 | this(null, icon, false); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Creates an initially unselected check box menu item with text. |
| 118 | * |
| 119 | * @param text the text of the CheckBoxMenuItem |
| 120 | */ |
| 121 | public JCheckBoxMenuItem(String text) { |
| 122 | this(text, null, false); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Creates a menu item whose properties are taken from the |
| 127 | * Action supplied. |
| 128 | * |
| 129 | * @since 1.3 |
| 130 | */ |
| 131 | public JCheckBoxMenuItem(Action a) { |
| 132 | this(); |
| 133 | setAction(a); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Creates an initially unselected check box menu item with the specified text and icon. |
| 138 | * |
| 139 | * @param text the text of the CheckBoxMenuItem |
| 140 | * @param icon the icon of the CheckBoxMenuItem |
| 141 | */ |
| 142 | public JCheckBoxMenuItem(String text, Icon icon) { |
| 143 | this(text, icon, false); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Creates a check box menu item with the specified text and selection state. |
| 148 | * |
| 149 | * @param text the text of the check box menu item. |
| 150 | * @param b the selected state of the check box menu item |
| 151 | */ |
| 152 | public JCheckBoxMenuItem(String text, boolean b) { |
| 153 | this(text, null, b); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Creates a check box menu item with the specified text, icon, and selection state. |
| 158 | * |
| 159 | * @param text the text of the check box menu item |
| 160 | * @param icon the icon of the check box menu item |
| 161 | * @param b the selected state of the check box menu item |
| 162 | */ |
| 163 | public JCheckBoxMenuItem(String text, Icon icon, boolean b) { |
| 164 | super(text, icon); |
| 165 | setModel(new JToggleButton.ToggleButtonModel()); |
| 166 | setSelected(b); |
| 167 | setFocusable(false); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Returns the name of the L&F class |
| 172 | * that renders this component. |
| 173 | * |
| 174 | * @return "CheckBoxMenuItemUI" |
| 175 | * @see JComponent#getUIClassID |
| 176 | * @see UIDefaults#getUI |
| 177 | */ |
| 178 | public String getUIClassID() { |
| 179 | return uiClassID; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Returns the selected-state of the item. This method |
| 184 | * exists for AWT compatibility only. New code should |
| 185 | * use isSelected() instead. |
| 186 | * |
| 187 | * @return true if the item is selected |
| 188 | */ |
| 189 | public boolean getState() { |
| 190 | return isSelected(); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Sets the selected-state of the item. This method |
| 195 | * exists for AWT compatibility only. New code should |
| 196 | * use setSelected() instead. |
| 197 | * |
| 198 | * @param b a boolean value indicating the item's |
| 199 | * selected-state, where true=selected |
| 200 | * @beaninfo |
| 201 | * description: The selection state of the check box menu item |
| 202 | * hidden: true |
| 203 | */ |
| 204 | public synchronized void setState(boolean b) { |
| 205 | setSelected(b); |
| 206 | } |
| 207 | |
| 208 | |
| 209 | /** |
| 210 | * Returns an array (length 1) containing the check box menu item |
| 211 | * label or null if the check box is not selected. |
| 212 | * |
| 213 | * @return an array containing one Object -- the text of the menu item |
| 214 | * -- if the item is selected; otherwise null |
| 215 | */ |
| 216 | public Object[] getSelectedObjects() { |
| 217 | if (isSelected() == false) |
| 218 | return null; |
| 219 | Object[] selectedObjects = new Object[1]; |
| 220 | selectedObjects[0] = getText(); |
| 221 | return selectedObjects; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * See readObject() and writeObject() in JComponent for more |
| 226 | * information about serialization in Swing. |
| 227 | */ |
| 228 | private void writeObject(ObjectOutputStream s) throws IOException { |
| 229 | s.defaultWriteObject(); |
| 230 | if (getUIClassID().equals(uiClassID)) { |
| 231 | byte count = JComponent.getWriteObjCounter(this); |
| 232 | JComponent.setWriteObjCounter(this, --count); |
| 233 | if (count == 0 && ui != null) { |
| 234 | ui.installUI(this); |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | |
| 240 | /** |
| 241 | * Returns a string representation of this JCheckBoxMenuItem. This method |
| 242 | * is intended to be used only for debugging purposes, and the |
| 243 | * content and format of the returned string may vary between |
| 244 | * implementations. The returned string may be empty but may not |
| 245 | * be <code>null</code>. |
| 246 | * |
| 247 | * @return a string representation of this JCheckBoxMenuItem. |
| 248 | */ |
| 249 | protected String paramString() { |
| 250 | return super.paramString(); |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Overriden to return true, JCheckBoxMenuItem supports |
| 255 | * the selected state. |
| 256 | */ |
| 257 | boolean shouldUpdateSelectedStateFromAction() { |
| 258 | return true; |
| 259 | } |
| 260 | |
| 261 | ///////////////// |
| 262 | // Accessibility support |
| 263 | //////////////// |
| 264 | |
| 265 | /** |
| 266 | * Gets the AccessibleContext associated with this JCheckBoxMenuItem. |
| 267 | * For JCheckBoxMenuItems, the AccessibleContext takes the form of an |
| 268 | * AccessibleJCheckBoxMenuItem. |
| 269 | * A new AccessibleJCheckBoxMenuItem instance is created if necessary. |
| 270 | * |
| 271 | * @return an AccessibleJCheckBoxMenuItem that serves as the |
| 272 | * AccessibleContext of this AccessibleJCheckBoxMenuItem |
| 273 | */ |
| 274 | public AccessibleContext getAccessibleContext() { |
| 275 | if (accessibleContext == null) { |
| 276 | accessibleContext = new AccessibleJCheckBoxMenuItem(); |
| 277 | } |
| 278 | return accessibleContext; |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * This class implements accessibility support for the |
| 283 | * <code>JCheckBoxMenuItem</code> class. It provides an implementation |
| 284 | * of the Java Accessibility API appropriate to checkbox menu item |
| 285 | * user-interface elements. |
| 286 | * <p> |
| 287 | * <strong>Warning:</strong> |
| 288 | * Serialized objects of this class will not be compatible with |
| 289 | * future Swing releases. The current serialization support is |
| 290 | * appropriate for short term storage or RMI between applications running |
| 291 | * the same version of Swing. As of 1.4, support for long term storage |
| 292 | * of all JavaBeans<sup><font size="-2">TM</font></sup> |
| 293 | * has been added to the <code>java.beans</code> package. |
| 294 | * Please see {@link java.beans.XMLEncoder}. |
| 295 | */ |
| 296 | protected class AccessibleJCheckBoxMenuItem extends AccessibleJMenuItem { |
| 297 | /** |
| 298 | * Get the role of this object. |
| 299 | * |
| 300 | * @return an instance of AccessibleRole describing the role of the |
| 301 | * object |
| 302 | */ |
| 303 | public AccessibleRole getAccessibleRole() { |
| 304 | return AccessibleRole.CHECK_BOX; |
| 305 | } |
| 306 | } // inner class AccessibleJCheckBoxMenuItem |
| 307 | } |