Spec-Zone .ru
спецификации, руководства, описания, API
|
001/* 002 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 004 * 005 * This code is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU General Public License version 2 only, as 007 * published by the Free Software Foundation. Oracle designates this 008 * particular file as subject to the "Classpath" exception as provided 009 * by Oracle in the LICENSE file that accompanied this code. 010 * 011 * This code is distributed in the hope that it will be useful, but WITHOUT 012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 014 * version 2 for more details (a copy is included in the LICENSE file that 015 * accompanied this code). 016 * 017 * You should have received a copy of the GNU General Public License version 018 * 2 along with this work; if not, write to the Free Software Foundation, 019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 020 * 021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 022 * or visit www.oracle.com if you need additional information or have any 023 * questions. 024 */ 025 026package javafx.scene.control; 027 028import javafx.beans.property.BooleanProperty; 029import javafx.beans.property.BooleanPropertyBase; 030import javafx.beans.property.SimpleBooleanProperty; 031 032import javafx.event.ActionEvent; 033import javafx.geometry.Pos; 034import com.sun.javafx.scene.control.accessible.AccessibleCheckBox; 035import com.sun.javafx.accessible.providers.AccessibleProvider; 036import javafx.css.PseudoClass; 037import com.sun.javafx.scene.control.skin.CheckBoxSkin; 038 039/** 040 * A tri-state selection Control typically skinned as a box with a checkmark or 041 * tick mark when checked. A CheckBox control can be in one of three states: 042 * <ul> 043 * <li><em>checked</em>: indeterminate == false, checked == true</li> 044 * <li><em>unchecked</em>: indeterminate == false, checked == false</li> 045 * <li><em>undefined</em>: indeterminate == true</li> 046 * </ul> 047 * If a CheckBox is checked, then it is also by definition defined. When 048 * checked the CheckBox is typically rendered with a "check" or "tick" mark. 049 * A CheckBox is in this state if selected is true and indeterminate is false. 050 * <p> 051 * A CheckBox is unchecked if selected is false and indeterminate is false. 052 * <p> 053 * A CheckBox is undefined if indeterminate is true, regardless of the state 054 * of selected. A typical rendering would be with a minus or dash, to 055 * indicate an undefined or indeterminate state of the CheckBox. 056 * This is convenient for constructing tri-state checkbox 057 * based trees, for example, where undefined check boxes typically mean "inherit 058 * settings from the parent". 059 * <p> 060 * The allowIndeterminate variable, if true, allows the user 061 * to cycle through the undefined state. If false, the CheckBox is 062 * not in the indeterminate state, and the user is allowed to change only the checked 063 * state. 064 * 065 * <p>Example: 066 * <pre><code>CheckBox cb = new CheckBox("a checkbox"); 067 * cb.setIndeterminate(false);</code></pre> 068 * 069 * <p> 070 * MnemonicParsing is enabled by default for CheckBox. 071 * </p> 072 * 073 */ 074 075public class CheckBox extends ButtonBase { 076 077 /*************************************************************************** 078 * * 079 * Constructors * 080 * * 081 **************************************************************************/ 082 083 /** 084 * Creates a check box with an empty string for its label. 085 */ 086 public CheckBox() { 087 initialize(); 088 } 089 090 /** 091 * Creates a check box with the specified text as its label. 092 * 093 * @param text A text string for its label. 094 */ 095 public CheckBox(String text) { 096 setText(text); 097 initialize(); 098 } 099 100 private void initialize() { 101 getStyleClass().setAll(DEFAULT_STYLE_CLASS); 102 setAlignment(Pos.CENTER_LEFT); 103 setMnemonicParsing(true); // enable mnemonic auto-parsing by default 104 105 // initialize pseudo-class state 106 pseudoClassStateChanged(PSEUDO_CLASS_DETERMINATE, true); 107 } 108 109 /*************************************************************************** 110 * * 111 * Properties * 112 * * 113 **************************************************************************/ 114 /** 115 * Determines whether the CheckBox is in the indeterminate state. 116 */ 117 private BooleanProperty indeterminate; 118 public final void setIndeterminate(boolean value) { 119 indeterminateProperty().set(value); 120 } 121 122 public final boolean isIndeterminate() { 123 return indeterminate == null ? false : indeterminate.get(); 124 } 125 126 public final BooleanProperty indeterminateProperty() { 127 if (indeterminate == null) { 128 indeterminate = new BooleanPropertyBase(false) { 129 @Override protected void invalidated() { 130 final boolean active = get(); 131 pseudoClassStateChanged(PSEUDO_CLASS_DETERMINATE, !active); 132 pseudoClassStateChanged(PSEUDO_CLASS_INDETERMINATE, active); 133 } 134 135 @Override 136 public Object getBean() { 137 return CheckBox.this; 138 } 139 140 @Override 141 public String getName() { 142 return "indeterminate"; 143 } 144 }; 145 } 146 return indeterminate; 147 } 148 /** 149 * Indicates whether this CheckBox is checked. 150 */ 151 private BooleanProperty selected; 152 public final void setSelected(boolean value) { 153 selectedProperty().set(value); 154 } 155 156 public final boolean isSelected() { 157 return selected == null ? false : selected.get(); 158 } 159 160 public final BooleanProperty selectedProperty() { 161 if (selected == null) { 162 selected = new BooleanPropertyBase() { 163 @Override protected void invalidated() { 164 pseudoClassStateChanged(PSEUDO_CLASS_SELECTED, get()); 165 } 166 167 @Override 168 public Object getBean() { 169 return CheckBox.this; 170 } 171 172 @Override 173 public String getName() { 174 return "selected"; 175 } 176 }; 177 } 178 return selected; 179 } 180 /** 181 * Determines whether the user toggling the CheckBox should cycle through 182 * all three states: <em>checked</em>, <em>unchecked</em>, and 183 * <em>undefined</em>. If {@code true} then all three states will be 184 * cycled through; if {@code false} then only <em>checked</em> and 185 * <em>unchecked</em> will be cycled. 186 */ 187 private BooleanProperty allowIndeterminate; 188 189 public final void setAllowIndeterminate(boolean value) { 190 allowIndeterminateProperty().set(value); 191 } 192 193 public final boolean isAllowIndeterminate() { 194 return allowIndeterminate == null ? false : allowIndeterminate.get(); 195 } 196 197 public final BooleanProperty allowIndeterminateProperty() { 198 if (allowIndeterminate == null) { 199 allowIndeterminate = 200 new SimpleBooleanProperty(this, "allowIndeterminate"); 201 } 202 return allowIndeterminate; 203 } 204 205 /*************************************************************************** 206 * * 207 * Methods * 208 * * 209 **************************************************************************/ 210 211 /** 212 * Toggles the state of the {@code CheckBox}. If allowIndeterminate is 213 * true, then each invocation of this function will advance the CheckBox 214 * through the states checked, unchecked, and undefined. If 215 * allowIndeterminate is false, then the CheckBox will only cycle through 216 * the checked and unchecked states, and forcing indeterminate to equal to 217 * false. 218 */ 219 @Override public void fire() { 220 if (isAllowIndeterminate()) { 221 if (!isSelected() && !isIndeterminate()) { 222 setIndeterminate(true); 223 } else if (isSelected() && !isIndeterminate()) { 224 setSelected(false); 225 } else if (isIndeterminate()) { 226 setSelected(true); 227 setIndeterminate(false); 228 } 229 } else { 230 setSelected(!isSelected()); 231 setIndeterminate(false); 232 } 233 fireEvent(new ActionEvent()); 234 } 235 236 /** {@inheritDoc} */ 237 @Override protected Skin<?> createDefaultSkin() { 238 return new CheckBoxSkin(this); 239 } 240 241 242 /*************************************************************************** 243 * * 244 * Stylesheet Handling * 245 * * 246 **************************************************************************/ 247 248 private static final String DEFAULT_STYLE_CLASS = "check-box"; 249 private static final PseudoClass PSEUDO_CLASS_DETERMINATE = 250 PseudoClass.getPseudoClass("determinate"); 251 private static final PseudoClass PSEUDO_CLASS_INDETERMINATE = 252 PseudoClass.getPseudoClass("indeterminate"); 253 private static final PseudoClass PSEUDO_CLASS_SELECTED = 254 PseudoClass.getPseudoClass("selected"); 255 256 private AccessibleCheckBox accCheckBox ; 257 /** 258 * @treatAsPrivate implementation detail 259 * @deprecated This is an internal API that is not intended for use and will be removed in the next version 260 */ 261 @Deprecated @Override public AccessibleProvider impl_getAccessible() { 262 if( accCheckBox == null) 263 accCheckBox = new AccessibleCheckBox(this); 264 return (AccessibleProvider)accCheckBox ; 265 } 266 267}