1 /*
   2  * Copyright (c) 2011, 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 
  26 package javafx.beans.property;
  27 
  28 import com.sun.javafx.binding.BidirectionalBinding;
  29 import javafx.beans.binding.Bindings;
  30 import javafx.beans.value.ObservableValue;
  31 import javafx.beans.value.WritableIntegerValue;
  32 import com.sun.javafx.binding.Logging;
  33 
  34 /**
  35  * This class defines a {@link Property} wrapping an {@code int} value.
  36  * <p>
  37  * The value of an {@code IntegerProperty} can be get and set with {@link #get()},
  38  * {@link #getValue()}, {@link #set(int)}, and {@link #setValue(Number)}.
  39  * <p>
  40  * A property can be bound and unbound unidirectional with
  41  * {@link #bind(ObservableValue)} and {@link #unbind()}. Bidirectional bindings
  42  * can be created and removed with {@link #bindBidirectional(Property)} and
  43  * {@link #unbindBidirectional(Property)}.
  44  * <p>
  45  * The context of a {@code IntegerProperty} can be read with {@link #getBean()}
  46  * and {@link #getName()}.
  47  * <p>
  48  * Note: setting or binding this property to a null value will set the property to "0.0". See {@link #setValue(java.lang.Number) }.
  49  *
  50  * @see javafx.beans.value.ObservableIntegerValue
  51  * @see javafx.beans.value.WritableIntegerValue
  52  * @see ReadOnlyIntegerProperty
  53  * @see Property
  54  *
  55  * @since JavaFX 2.0
  56  */
  57 public abstract class IntegerProperty extends ReadOnlyIntegerProperty implements
  58         Property<Number>, WritableIntegerValue {
  59 
  60     /**
  61      * {@inheritDoc}
  62      */
  63     @Override
  64     public void setValue(Number v) {
  65         if (v == null) {
  66             Logging.getLogger().fine("Attempt to set integer property to null, using default value instead.", new NullPointerException());
  67             set(0);
  68         } else {
  69             set(v.intValue());
  70         }
  71     }
  72 
  73     /**
  74      * {@inheritDoc}
  75      */
  76     @Override
  77     public void bindBidirectional(Property<Number> other) {
  78         Bindings.bindBidirectional(this, other);
  79     }
  80 
  81     /**
  82      * {@inheritDoc}
  83      */
  84     @Override
  85     public void unbindBidirectional(Property<Number> other) {
  86         Bindings.unbindBidirectional(this, other);
  87     }
  88 
  89     /**
  90      * Returns a string representation of this {@code IntegerProperty} object.
  91      * @return a string representation of this {@code IntegerProperty} object.
  92      */
  93     @Override
  94     public String toString() {
  95         final Object bean = getBean();
  96         final String name = getName();
  97         final StringBuilder result = new StringBuilder(
  98                 "IntegerProperty [");
  99         if (bean != null) {
 100             result.append("bean: ").append(bean).append(", ");
 101         }
 102         if ((name != null) && (!name.equals(""))) {
 103             result.append("name: ").append(name).append(", ");
 104         }
 105         result.append("value: ").append(get()).append("]");
 106         return result.toString();
 107     }
 108 
 109     /**
 110      * Returns a {@code IntegerProperty} that wraps a
 111      * {@link javafx.beans.property.Property} and is
 112      * bidirectionally bound to it.
 113      * Changing this property will result in a change of the original property.
 114      *
 115      * <p>
 116      * This is very useful when bidirectionally binding an ObjectProperty<Integer> and
 117      * a IntegerProperty.
 118      *
 119      * <blockquote><pre>
 120      *   IntegerProperty integerProperty = new SimpleIntegerProperty(1);
 121      *   ObjectProperty&lt;Integer&gt; objectProperty = new SimpleObjectProperty&lt;&gt;(2);
 122      *
 123      *   // Need to keep the reference as bidirectional binding uses weak references
 124      *   IntegerProperty objectAsInteger = IntegerProperty.integerProperty(objectProperty);
 125      *
 126      *   integerProperty.bindBidirectional(objectAsInteger);
 127      *
 128      * </pre></blockquote>
 129      *
 130      * Another approach is to convert the IntegerProperty to ObjectProperty using
 131      * {@link #asObject()} method.
 132      *
 133      * <p>
 134      * Note: null values in the source property will be interpreted as 0
 135      *
 136      * @param property
 137      *            The source {@code Property}
 138      * @return A {@code IntegerProperty} that wraps the
 139      *         {@code Property}
 140      * @throws NullPointerException
 141      *             if {@code property} is {@code null}
 142      * @see #asObject()
 143      * @since JavaFX 8.0
 144      */
 145      public static IntegerProperty integerProperty(final Property<Integer> property) {
 146         if (property == null) {
 147             throw new NullPointerException("Property cannot be null");
 148         }
 149         return new IntegerPropertyBase() {
 150             {
 151                 BidirectionalBinding.bindNumber(this, property);
 152             }
 153 
 154             @Override
 155             public Object getBean() {
 156                 return null; // Virtual property, no bean
 157             }
 158 
 159             @Override
 160             public String getName() {
 161                 return property.getName();
 162             }
 163 
 164             @Override
 165             protected void finalize() throws Throwable {
 166                 try {
 167                     BidirectionalBinding.unbindNumber(property, this);
 168                 } finally {
 169                     super.finalize();
 170                 }
 171             }
 172         };
 173     }
 174 
 175      /**
 176      * Creates an {@link javafx.beans.property.ObjectProperty}
 177      * that bidirectionally bound to this {@code IntegerProperty}. If the
 178      * value of this {@code IntegerProperty} changes, the value of the
 179      * {@code ObjectProperty} will be updated automatically and vice-versa.
 180      *
 181      * <p>
 182      * Can be used for binding an ObjectProperty to IntegerProperty.
 183      *
 184      * <blockquote><pre>
 185      *   IntegerProperty integerProperty = new SimpleIntegerProperty(1);
 186      *   ObjectProperty&lt;Integer&gt; objectProperty = new SimpleObjectProperty&lt;&gt;(2);
 187      *
 188      *   objectProperty.bind(integerProperty.asObject());
 189      * </pre></blockquote>
 190      *
 191      * @return the new {@code ObjectProperty}
 192      * @since JavaFX 8.0
 193      */
 194     @Override
 195     public ObjectProperty<Integer> asObject() {
 196         return new ObjectPropertyBase<Integer> () {
 197 
 198             {
 199                 BidirectionalBinding.bindNumber(this, IntegerProperty.this);
 200             }
 201 
 202             @Override
 203             public Object getBean() {
 204                 return null; // Virtual property, does not exist on a bean
 205             }
 206 
 207             @Override
 208             public String getName() {
 209                 return IntegerProperty.this.getName();
 210             }
 211 
 212             @Override
 213             protected void finalize() throws Throwable {
 214                 try {
 215                     BidirectionalBinding.unbindNumber(this, IntegerProperty.this);
 216                 } finally {
 217                     super.finalize();
 218                 }
 219             }
 220 
 221         };
 222     }
 223 }