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 com.sun.javafx.binding.ExpressionHelper;
030import javafx.beans.binding.Bindings;
031import javafx.beans.value.ObservableValue;
032import javafx.beans.value.WritableDoubleValue;
033import com.sun.javafx.binding.Logging;
034import javafx.beans.InvalidationListener;
035import javafx.beans.Observable;
036import javafx.beans.WeakInvalidationListener;
037import javafx.beans.value.ChangeListener;
038import javafx.beans.value.ObservableDoubleValue;
039
040/**
041 * This class defines a {@link Property} wrapping a {@code double} value.
042 * <p>
043 * The value of a {@code DoubleProperty} can be get and set with {@link #get()},
044 * {@link #getValue()}, {@link #set(double)}, and {@link #setValue(Number)}.
045 * <p>
046 * A property can be bound and unbound unidirectional with
047 * {@link #bind(ObservableValue)} and {@link #unbind()}. Bidirectional bindings
048 * can be created and removed with {@link #bindBidirectional(Property)} and
049 * {@link #unbindBidirectional(Property)}.
050 * <p>
051 * The context of a {@code DoubleProperty} can be read with {@link #getBean()}
052 * and {@link #getName()}.
053 * <p>
054 * Note: setting or binding this property to a null value will set the property to "0.0". See {@link #setValue(java.lang.Number) }.
055 *
056 * @see javafx.beans.value.ObservableDoubleValue
057 * @see javafx.beans.value.WritableDoubleValue
058 * @see ReadOnlyDoubleProperty
059 * @see Property
060 *
061 */
062public abstract class DoubleProperty extends ReadOnlyDoubleProperty implements
063        Property<Number>, WritableDoubleValue {
064
065    /**
066     * {@inheritDoc}
067     */
068    @Override
069    public void setValue(Number v) {
070        if (v == null) {
071            Logging.getLogger().info("Attempt to set double property to null, using default value instead.", new NullPointerException());
072            set(0.0);
073        } else {
074            set(v.doubleValue());
075        }
076    }
077
078    /**
079     * {@inheritDoc}
080     */
081    @Override
082    public void bindBidirectional(Property<Number> other) {
083        Bindings.bindBidirectional(this, other);
084    }
085
086    /**
087     * {@inheritDoc}
088     */
089    @Override
090    public void unbindBidirectional(Property<Number> other) {
091        Bindings.unbindBidirectional(this, other);
092    }
093
094    /**
095     * Returns a string representation of this {@code DoubleProperty} object.
096     * @return a string representation of this {@code DoubleProperty} object.
097     */
098    @Override
099    public String toString() {
100        final Object bean = getBean();
101        final String name = getName();
102        final StringBuilder result = new StringBuilder(
103                "DoubleProperty [");
104        if (bean != null) {
105            result.append("bean: ").append(bean).append(", ");
106        }
107        if ((name != null) && (!name.equals(""))) {
108            result.append("name: ").append(name).append(", ");
109        }
110        result.append("value: ").append(get()).append("]");
111        return result.toString();
112    }
113
114    /**
115     * Returns a {@code DoubleProperty} that wraps a
116     * {@link javafx.beans.property.Property} and is
117     * bidirectionally bound to it.
118     * Changing this property will result in a change of the original property.
119     *
120     * <p>
121     * This is very useful when bidirectionally binding an ObjectProperty<Double> and
122     * a DoubleProperty.
123     *
124     * <blockquote><pre>
125     *   DoubleProperty doubleProperty = new SimpleDoubleProperty(1.0);
126     *   ObjectProperty&lt;Double&gt; objectProperty = new SimpleObjectProperty&lt;&gt;(2.0);
127     *
128     *   // Need to keep the reference as bidirectional binding uses weak references
129     *   DoubleProperty objectAsDouble = DoubleProperty.doubleProperty(objectProperty);
130     *
131     *   doubleProperty.bindBidirectional(objectAsDouble);
132     *
133     * </pre></blockquote>
134     *
135     * Another approach is to convert the DoubleProperty to ObjectProperty using
136     * {@link #asObject()} method.
137     * <p>
138     * Note: null values in the source property will be interpreted as 0.0
139     *
140     * @param property
141     *            The source {@code Property}
142     * @return A {@code DoubleProperty} that wraps the
143     *         {@code Property}
144     * @throws NullPointerException
145     *             if {@code value} is {@code null}
146     * @see #asObject()
147     */
148    public static DoubleProperty doubleProperty(final Property<Double> property) {
149        if (property == null) {
150            throw new NullPointerException("Property cannot be null");
151        }
152        return new DoublePropertyBase() {
153            {
154                BidirectionalBinding.bindNumber(property, this);
155            }
156
157            @Override
158            public Object getBean() {
159                return null; // Virtual property, no bean
160            }
161
162            @Override
163            public String getName() {
164                return property.getName();
165            }
166
167            @Override
168            protected void finalize() throws Throwable {
169                try {
170                    BidirectionalBinding.unbindNumber(property, this);
171                } finally {
172                    super.finalize();
173                }
174            }
175        };
176    }
177
178    /**
179     * Creates an {@link javafx.beans.property.ObjectProperty}
180     * that bidirectionally bound to this {@code DoubleProperty}. If the
181     * value of this {@code DoubleProperty} changes, the value of the
182     * {@code ObjectProperty} will be updated automatically and vice-versa.
183     *
184     * <p>
185     * Can be used for binding an ObjectProperty to DoubleProperty.
186     *
187     * <blockquote><pre>
188     *   DoubleProperty doubleProperty = new SimpleDoubleProperty(1.0);
189     *   ObjectProperty&lt;Double&gt; objectProperty = new SimpleObjectProperty&lt;&gt;(2.0);
190     *
191     *   objectProperty.bind(doubleProperty.asObject());
192     * </pre></blockquote>
193     *
194     * @return the new {@code ObjectProperty}
195     */
196    @Override
197    public ObjectProperty<Double> asObject() {
198        return new ObjectPropertyBase<Double> () {
199
200            {
201                BidirectionalBinding.bindNumber(this, DoubleProperty.this);
202            }
203
204            @Override
205            public Object getBean() {
206                return null; // Virtual property, does not exist on a bean
207            }
208
209            @Override
210            public String getName() {
211                return DoubleProperty.this.getName();
212            }
213
214            @Override
215            protected void finalize() throws Throwable {
216                try {
217                    BidirectionalBinding.unbindNumber(this, DoubleProperty.this);
218                } finally {
219                    super.finalize();
220                }
221            }
222
223        };
224    }
225
226
227}