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.adapter; 027 028import com.sun.javafx.binding.ExpressionHelper; 029import com.sun.javafx.property.adapter.PropertyDescriptor; 030import javafx.beans.InvalidationListener; 031import javafx.beans.property.DoubleProperty; 032import javafx.beans.value.ChangeListener; 033import javafx.beans.value.ObservableValue; 034 035import java.lang.reflect.InvocationTargetException; 036import java.lang.reflect.UndeclaredThrowableException; 037import sun.misc.Cleaner; 038 039import java.security.AccessControlContext; 040import java.security.AccessController; 041import java.security.PrivilegedAction; 042 043import sun.reflect.misc.MethodUtil; 044 045/** 046 * A {@code JavaBeanDoubleProperty} provides an adapter between a regular 047 * Java Bean property of type {@code double} or {@code Double} and a JavaFX 048 * {@code DoubleProperty}. It cannot be created directly, but a 049 * {@link JavaBeanDoublePropertyBuilder} has to be used. 050 * <p> 051 * As a minimum, the Java Bean must implement a getter and a setter for the 052 * property. If the getter of an instance of this class is called, the property of 053 * the Java Bean is returned. If the setter is called, the value will be passed 054 * to the Java Bean property. If the Java Bean property is bound (i.e. it supports 055 * PropertyChangeListeners), this {@code JavaBeanDoubleProperty} will be 056 * aware of changes in the Java Bean. Otherwise it can be notified about 057 * changes by calling {@link #fireValueChangedEvent()}. If the Java Bean property 058 * is also constrained (i.e. it supports VetoableChangeListeners), this 059 * {@code JavaBeanDoubleProperty} will reject changes, if it is bound to an 060 * {@link javafx.beans.value.ObservableValue ObservableValue<Double>}. 061 * 062 * @see javafx.beans.property.DoubleProperty 063 * @see JavaBeanDoublePropertyBuilder 064 */ 065public final class JavaBeanDoubleProperty extends DoubleProperty implements JavaBeanProperty<Number> { 066 067 private final PropertyDescriptor descriptor; 068 private final PropertyDescriptor.Listener<Number> listener; 069 070 private ObservableValue<? extends Number> observable = null; 071 private ExpressionHelper<Number> helper = null; 072 073 private final AccessControlContext acc = AccessController.getContext(); 074 075 JavaBeanDoubleProperty(PropertyDescriptor descriptor, Object bean) { 076 this.descriptor = descriptor; 077 this.listener = descriptor.new Listener<Number>(bean, this); 078 descriptor.addListener(listener); 079 Cleaner.create(this, new Runnable() { 080 @Override 081 public void run() { 082 JavaBeanDoubleProperty.this.descriptor.removeListener(listener); 083 } 084 }); 085 } 086 087 /** 088 * {@inheritDoc} 089 * 090 * @throws UndeclaredThrowableException if calling the getter of the Java Bean 091 * property throws an {@code IllegalAccessException} or an 092 * {@code InvocationTargetException}. 093 */ 094 @Override 095 public double get() { 096 return AccessController.doPrivileged(new PrivilegedAction<Double>() { 097 public Double run() { 098 try { 099 return ((Number)MethodUtil.invoke( 100 descriptor.getGetter(), getBean(), (Object[])null)).doubleValue(); 101 } catch (IllegalAccessException e) { 102 throw new UndeclaredThrowableException(e); 103 } catch (InvocationTargetException e) { 104 throw new UndeclaredThrowableException(e); 105 } 106 } 107 }, acc); 108 } 109 110 /** 111 * {@inheritDoc} 112 * 113 * @throws UndeclaredThrowableException if calling the getter of the Java Bean 114 * property throws an {@code IllegalAccessException} or an 115 * {@code InvocationTargetException}. 116 */ 117 @Override 118 public void set(final double value) { 119 if (isBound()) { 120 throw new RuntimeException("A bound value cannot be set."); 121 } 122 123 AccessController.doPrivileged(new PrivilegedAction<Void>() { 124 public Void run() { 125 try { 126 MethodUtil.invoke(descriptor.getSetter(), getBean(), new Object[] {value}); 127 ExpressionHelper.fireValueChangedEvent(helper); 128 } catch (IllegalAccessException e) { 129 throw new UndeclaredThrowableException(e); 130 } catch (InvocationTargetException e) { 131 throw new UndeclaredThrowableException(e); 132 } 133 return null; 134 } 135 }, acc); 136 } 137 138 /** 139 * {@inheritDoc} 140 */ 141 @Override 142 public void bind(ObservableValue<? extends Number> observable) { 143 if (observable == null) { 144 throw new NullPointerException("Cannot bind to null"); 145 } 146 147 if (!observable.equals(this.observable)) { 148 unbind(); 149 set(observable.getValue().doubleValue()); 150 this.observable = observable; 151 this.observable.addListener(listener); 152 } 153 } 154 155 /** 156 * {@inheritDoc} 157 */ 158 @Override 159 public void unbind() { 160 if (observable != null) { 161 observable.removeListener(listener); 162 observable = null; 163 } 164 } 165 166 /** 167 * {@inheritDoc} 168 */ 169 @Override 170 public boolean isBound() { 171 return observable != null; 172 } 173 174 /** 175 * {@inheritDoc} 176 */ 177 @Override 178 public Object getBean() { 179 return listener.getBean(); 180 } 181 182 /** 183 * {@inheritDoc} 184 */ 185 @Override 186 public String getName() { 187 return descriptor.getName(); 188 } 189 190 /** 191 * {@inheritDoc} 192 */ 193 @Override 194 public void addListener(ChangeListener<? super Number> listener) { 195 helper = ExpressionHelper.addListener(helper, this, listener); 196 } 197 198 /** 199 * {@inheritDoc} 200 */ 201 @Override 202 public void removeListener(ChangeListener<? super Number> listener) { 203 helper = ExpressionHelper.removeListener(helper, listener); 204 } 205 206 /** 207 * {@inheritDoc} 208 */ 209 @Override 210 public void addListener(InvalidationListener listener) { 211 helper = ExpressionHelper.addListener(helper, this, listener); 212 } 213 214 /** 215 * {@inheritDoc} 216 */ 217 @Override 218 public void removeListener(InvalidationListener listener) { 219 helper = ExpressionHelper.removeListener(helper, listener); 220 } 221 222 /** 223 * {@inheritDoc} 224 */ 225 @Override 226 public void fireValueChangedEvent() { 227 ExpressionHelper.fireValueChangedEvent(helper); 228 } 229 230 /** 231 * {@inheritDoc} 232 */ 233 @Override 234 public void dispose() { 235 descriptor.removeListener(listener); 236 237 } 238 239 /** 240 * Returns a string representation of this {@code JavaBeanDoubleProperty} object. 241 * @return a string representation of this {@code JavaBeanDoubleProperty} object. 242 */ 243 @Override 244 public String toString() { 245 final Object bean = getBean(); 246 final String name = getName(); 247 final StringBuilder result = new StringBuilder("DoubleProperty ["); 248 if (bean != null) { 249 result.append("bean: ").append(bean).append(", "); 250 } 251 if ((name != null) && (!name.equals(""))) { 252 result.append("name: ").append(name).append(", "); 253 } 254 if (isBound()) { 255 result.append("bound, "); 256 } 257 result.append("value: ").append(get()); 258 result.append("]"); 259 return result.toString(); 260 } 261}