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