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