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