1 /*
   2  * Copyright (c) 1997, 2014, 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 javax.swing;
  26 
  27 import java.awt.*;
  28 import java.awt.event.*;
  29 import java.beans.*;
  30 
  31 import javax.swing.plaf.*;
  32 import javax.accessibility.*;
  33 
  34 import java.io.ObjectOutputStream;
  35 import java.io.ObjectInputStream;
  36 import java.io.IOException;
  37 
  38 
  39 /**
  40  * An implementation of a check box -- an item that can be selected or
  41  * deselected, and which displays its state to the user.
  42  * By convention, any number of check boxes in a group can be selected.
  43  * See <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/button.html">How to Use Buttons, Check Boxes, and Radio Buttons</a>
  44  * in <em>The Java Tutorial</em>
  45  * for examples and information on using check boxes.
  46  * <p>
  47  * Buttons can be configured, and to some degree controlled, by
  48  * <code><a href="Action.html">Action</a></code>s.  Using an
  49  * <code>Action</code> with a button has many benefits beyond directly
  50  * configuring a button.  Refer to <a href="Action.html#buttonActions">
  51  * Swing Components Supporting <code>Action</code></a> for more
  52  * details, and you can find more information in <a
  53  * href="http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html">How
  54  * to Use Actions</a>, a section in <em>The Java Tutorial</em>.
  55  * <p>
  56  * <strong>Warning:</strong> Swing is not thread safe. For more
  57  * information see <a
  58  * href="package-summary.html#threading">Swing's Threading
  59  * Policy</a>.
  60  * <p>
  61  * <strong>Warning:</strong>
  62  * Serialized objects of this class will not be compatible with
  63  * future Swing releases. The current serialization support is
  64  * appropriate for short term storage or RMI between applications running
  65  * the same version of Swing.  As of 1.4, support for long term storage
  66  * of all JavaBeans&trade;
  67  * has been added to the <code>java.beans</code> package.
  68  * Please see {@link java.beans.XMLEncoder}.
  69  *
  70  * @see JRadioButton
  71  *
  72  * @beaninfo
  73  *   attribute: isContainer false
  74  * description: A component which can be selected or deselected.
  75  *
  76  * @author Jeff Dinkins
  77  * @since 1.2
  78  */
  79 @SuppressWarnings("serial") // Same-version serialization only
  80 public class JCheckBox extends JToggleButton implements Accessible {
  81 
  82     /** Identifies a change to the flat property. */
  83     public static final String BORDER_PAINTED_FLAT_CHANGED_PROPERTY = "borderPaintedFlat";
  84 
  85     private boolean flat = false;
  86 
  87     /**
  88      * @see #getUIClassID
  89      * @see #readObject
  90      */
  91     private static final String uiClassID = "CheckBoxUI";
  92 
  93 
  94     /**
  95      * Creates an initially unselected check box button with no text, no icon.
  96      */
  97     public JCheckBox () {
  98         this(null, null, false);
  99     }
 100 
 101     /**
 102      * Creates an initially unselected check box with an icon.
 103      *
 104      * @param icon  the Icon image to display
 105      */
 106     public JCheckBox(Icon icon) {
 107         this(null, icon, false);
 108     }
 109 
 110     /**
 111      * Creates a check box with an icon and specifies whether
 112      * or not it is initially selected.
 113      *
 114      * @param icon  the Icon image to display
 115      * @param selected a boolean value indicating the initial selection
 116      *        state. If <code>true</code> the check box is selected
 117      */
 118     public JCheckBox(Icon icon, boolean selected) {
 119         this(null, icon, selected);
 120     }
 121 
 122     /**
 123      * Creates an initially unselected check box with text.
 124      *
 125      * @param text the text of the check box.
 126      */
 127     public JCheckBox (String text) {
 128         this(text, null, false);
 129     }
 130 
 131     /**
 132      * Creates a check box where properties are taken from the
 133      * Action supplied.
 134      *
 135      * @param a the {@code Action} used to specify the new check box
 136      * @since 1.3
 137      */
 138     public JCheckBox(Action a) {
 139         this();
 140         setAction(a);
 141     }
 142 
 143 
 144     /**
 145      * Creates a check box with text and specifies whether
 146      * or not it is initially selected.
 147      *
 148      * @param text the text of the check box.
 149      * @param selected a boolean value indicating the initial selection
 150      *        state. If <code>true</code> the check box is selected
 151      */
 152     public JCheckBox (String text, boolean selected) {
 153         this(text, null, selected);
 154     }
 155 
 156     /**
 157      * Creates an initially unselected check box with
 158      * the specified text and icon.
 159      *
 160      * @param text the text of the check box.
 161      * @param icon  the Icon image to display
 162      */
 163     public JCheckBox(String text, Icon icon) {
 164         this(text, icon, false);
 165     }
 166 
 167     /**
 168      * Creates a check box with text and icon,
 169      * and specifies whether or not it is initially selected.
 170      *
 171      * @param text the text of the check box.
 172      * @param icon  the Icon image to display
 173      * @param selected a boolean value indicating the initial selection
 174      *        state. If <code>true</code> the check box is selected
 175      */
 176     public JCheckBox (String text, Icon icon, boolean selected) {
 177         super(text, icon, selected);
 178         setUIProperty("borderPainted", Boolean.FALSE);
 179         setHorizontalAlignment(LEADING);
 180     }
 181 
 182     /**
 183      * Sets the <code>borderPaintedFlat</code> property,
 184      * which gives a hint to the look and feel as to the
 185      * appearance of the check box border.
 186      * This is usually set to <code>true</code> when a
 187      * <code>JCheckBox</code> instance is used as a
 188      * renderer in a component such as a <code>JTable</code> or
 189      * <code>JTree</code>.  The default value for the
 190      * <code>borderPaintedFlat</code> property is <code>false</code>.
 191      * This method fires a property changed event.
 192      * Some look and feels might not implement flat borders;
 193      * they will ignore this property.
 194      *
 195      * @param b <code>true</code> requests that the border be painted flat;
 196      *          <code>false</code> requests normal borders
 197      * @see #isBorderPaintedFlat
 198      * @beaninfo
 199      *        bound: true
 200      *    attribute: visualUpdate true
 201      *  description: Whether the border is painted flat.
 202      * @since 1.3
 203      */
 204     public void setBorderPaintedFlat(boolean b) {
 205         boolean oldValue = flat;
 206         flat = b;
 207         firePropertyChange(BORDER_PAINTED_FLAT_CHANGED_PROPERTY, oldValue, flat);
 208         if (b != oldValue) {
 209             revalidate();
 210             repaint();
 211         }
 212     }
 213 
 214     /**
 215      * Gets the value of the <code>borderPaintedFlat</code> property.
 216      *
 217      * @return the value of the <code>borderPaintedFlat</code> property
 218      * @see #setBorderPaintedFlat
 219      * @since 1.3
 220      */
 221     public boolean isBorderPaintedFlat() {
 222         return flat;
 223     }
 224 
 225     /**
 226      * Resets the UI property to a value from the current look and feel.
 227      *
 228      * @see JComponent#updateUI
 229      */
 230     public void updateUI() {
 231         setUI((ButtonUI)UIManager.getUI(this));
 232     }
 233 
 234 
 235     /**
 236      * Returns a string that specifies the name of the L&amp;F class
 237      * that renders this component.
 238      *
 239      * @return the string "CheckBoxUI"
 240      * @see JComponent#getUIClassID
 241      * @see UIDefaults#getUI
 242      * @beaninfo
 243      *        expert: true
 244      *   description: A string that specifies the name of the L&amp;F class
 245      */
 246     public String getUIClassID() {
 247         return uiClassID;
 248     }
 249 
 250 
 251     /**
 252      * The icon for checkboxs comes from the look and feel,
 253      * not the Action; this is overriden to do nothing.
 254      */
 255     void setIconFromAction(Action a) {
 256     }
 257 
 258      /*
 259       * See readObject and writeObject in JComponent for more
 260       * information about serialization in Swing.
 261       */
 262      private void writeObject(ObjectOutputStream s) throws IOException {
 263         s.defaultWriteObject();
 264         if (getUIClassID().equals(uiClassID)) {
 265             byte count = JComponent.getWriteObjCounter(this);
 266             JComponent.setWriteObjCounter(this, --count);
 267             if (count == 0 && ui != null) {
 268                 ui.installUI(this);
 269             }
 270         }
 271      }
 272 
 273 
 274     /**
 275      * See JComponent.readObject() for information about serialization
 276      * in Swing.
 277      */
 278     private void readObject(ObjectInputStream s)
 279         throws IOException, ClassNotFoundException
 280     {
 281         s.defaultReadObject();
 282         if (getUIClassID().equals(uiClassID)) {
 283             updateUI();
 284         }
 285     }
 286 
 287 
 288     /**
 289      * Returns a string representation of this JCheckBox. This method
 290      * is intended to be used only for debugging purposes, and the
 291      * content and format of the returned string may vary between
 292      * implementations. The returned string may be empty but may not
 293      * be <code>null</code>.
 294      * specific new aspects of the JFC components.
 295      *
 296      * @return  a string representation of this JCheckBox.
 297      */
 298     protected String paramString() {
 299         return super.paramString();
 300     }
 301 
 302 /////////////////
 303 // Accessibility support
 304 ////////////////
 305 
 306     /**
 307      * Gets the AccessibleContext associated with this JCheckBox.
 308      * For JCheckBoxes, the AccessibleContext takes the form of an
 309      * AccessibleJCheckBox.
 310      * A new AccessibleJCheckBox instance is created if necessary.
 311      *
 312      * @return an AccessibleJCheckBox that serves as the
 313      *         AccessibleContext of this JCheckBox
 314      * @beaninfo
 315      *       expert: true
 316      *  description: The AccessibleContext associated with this CheckBox.
 317      */
 318     public AccessibleContext getAccessibleContext() {
 319         if (accessibleContext == null) {
 320             accessibleContext = new AccessibleJCheckBox();
 321         }
 322         return accessibleContext;
 323     }
 324 
 325     /**
 326      * This class implements accessibility support for the
 327      * <code>JCheckBox</code> class.  It provides an implementation of the
 328      * Java Accessibility API appropriate to check box user-interface
 329      * elements.
 330      * <p>
 331      * <strong>Warning:</strong>
 332      * Serialized objects of this class will not be compatible with
 333      * future Swing releases. The current serialization support is
 334      * appropriate for short term storage or RMI between applications running
 335      * the same version of Swing.  As of 1.4, support for long term storage
 336      * of all JavaBeans&trade;
 337      * has been added to the <code>java.beans</code> package.
 338      * Please see {@link java.beans.XMLEncoder}.
 339      */
 340     @SuppressWarnings("serial") // Same-version serialization only
 341     protected class AccessibleJCheckBox extends AccessibleJToggleButton {
 342 
 343         /**
 344          * Get the role of this object.
 345          *
 346          * @return an instance of AccessibleRole describing the role of the object
 347          * @see AccessibleRole
 348          */
 349         public AccessibleRole getAccessibleRole() {
 350             return AccessibleRole.CHECK_BOX;
 351         }
 352 
 353     } // inner class AccessibleJCheckBox
 354 }