Spec-Zone .ru
спецификации, руководства, описания, API
001/*
002 * Copyright (c) 2011, 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.beans.property;
027
028import com.sun.javafx.binding.BidirectionalBinding;
029import javafx.beans.binding.Bindings;
030import javafx.beans.value.ObservableValue;
031import javafx.beans.value.WritableBooleanValue;
032import com.sun.javafx.binding.Logging;
033
034/**
035 * This class provides a full implementation of a {@link Property} wrapping a
036 * {@code boolean} value.
037 * <p>
038 * The value of a {@code BooleanProperty} can be get and set with {@link #get()},
039 * {@link #getValue()}, {@link #set(boolean)}, and {@link #setValue(Boolean)}.
040 * <p>
041 * A property can be bound and unbound unidirectional with
042 * {@link #bind(ObservableValue)} and {@link #unbind()}. Bidirectional bindings
043 * can be created and removed with {@link #bindBidirectional(Property)} and
044 * {@link #unbindBidirectional(Property)}.
045 * <p>
046 * The context of a {@code BooleanProperty} can be read with {@link #getBean()}
047 * and {@link #getName()}.
048 *
049 * <p>
050 * Note: setting or binding this property to a null value will set the property to "false". See {@link #setValue(java.lang.Boolean) }.
051 *
052 * @see javafx.beans.value.ObservableBooleanValue
053 * @see javafx.beans.value.WritableBooleanValue
054 * @see ReadOnlyBooleanProperty
055 * @see Property
056 *
057 */
058public abstract class BooleanProperty extends ReadOnlyBooleanProperty implements
059        Property<Boolean>, WritableBooleanValue {
060
061    /**
062     * {@inheritDoc}
063     */
064    @Override
065    public void setValue(Boolean v) {
066        if (v == null) {
067            Logging.getLogger().info("Attempt to set boolean property to null, using default value instead.", new NullPointerException());
068            set(false);
069        } else {
070            set(v.booleanValue());
071        }
072    }
073
074    /**
075     * {@inheritDoc}
076     */
077    @Override
078    public void bindBidirectional(Property<Boolean> other) {
079        Bindings.bindBidirectional(this, other);
080    }
081
082    /**
083     * {@inheritDoc}
084     */
085    @Override
086    public void unbindBidirectional(Property<Boolean> other) {
087        Bindings.unbindBidirectional(this, other);
088    }
089
090    /**
091     * Returns a string representation of this {@code BooleanProperty} object.
092     * @return a string representation of this {@code BooleanProperty} object.
093     */
094    @Override
095    public String toString() {
096        final Object bean = getBean();
097        final String name = getName();
098        final StringBuilder result = new StringBuilder(
099                "BooleanProperty [");
100        if (bean != null) {
101            result.append("bean: ").append(bean).append(", ");
102        }
103        if ((name != null) && (!name.equals(""))) {
104            result.append("name: ").append(name).append(", ");
105        }
106        result.append("value: ").append(get()).append("]");
107        return result.toString();
108    }
109
110    /**
111     * Returns a {@code BooleanProperty} that wraps a
112     * {@link javafx.beans.property.Property}. If the
113     * {@code Property} is already a {@code BooleanProperty}, it
114     * will be returned. Otherwise a new
115     * {@code BooleanProperty} is created that is bound to
116     * the {@code Property}.
117     *
118     * Note: null values in the source property will be interpreted as "false"
119     *
120     * @param property
121     *            The source {@code Property}
122     * @return A {@code BooleanProperty} that wraps the
123     *         {@code Property} if necessary
124     * @throws NullPointerException
125     *             if {@code value} is {@code null}
126     */
127    public static BooleanProperty booleanProperty(final Property<Boolean> property) {
128        if (property == null) {
129            throw new NullPointerException("Property cannot be null");
130        }
131        return property instanceof BooleanProperty ? (BooleanProperty)property : new BooleanPropertyBase() {
132            {
133                BidirectionalBinding.bind(property, this);
134            }
135
136            @Override
137            public Object getBean() {
138                return null; // Virtual property, no bean
139            }
140
141            @Override
142            public String getName() {
143                return property.getName();
144            }
145
146            @Override
147            protected void finalize() throws Throwable {
148                try {
149                    BidirectionalBinding.unbind(property, this);
150                } finally {
151                    super.finalize();
152                }
153            }
154        };
155    }
156
157    /**
158     * Creates an {@link javafx.beans.property.ObjectProperty} that holds the value
159     * of this {@code BooleanProperty}. If the
160     * value of this {@code BooleanProperty} changes, the value of the
161     * {@code ObjectProperty} will be updated automatically.
162     *
163     * @return the new {@code ObjectProperty}
164     */
165    @Override
166    public ObjectProperty<Boolean> asObject() {
167        return new ObjectPropertyBase<Boolean> () {
168
169            {
170                BidirectionalBinding.bind(this, BooleanProperty.this);
171            }
172
173            @Override
174            public Object getBean() {
175                return null; // Virtual property, does not exist on a bean
176            }
177
178            @Override
179            public String getName() {
180                return BooleanProperty.this.getName();
181            }
182
183            @Override
184            protected void finalize() throws Throwable {
185                try {
186                    BidirectionalBinding.unbind(this, BooleanProperty.this);
187                } finally {
188                    super.finalize();
189                }
190            }
191
192        };
193    }
194}