1 /*
   2  * Copyright (c) 2011, 2017, 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.adapter;
  27 
  28 import com.sun.javafx.binding.ExpressionHelper;
  29 import com.sun.javafx.property.MethodHelper;
  30 import com.sun.javafx.property.adapter.Disposer;
  31 import com.sun.javafx.property.adapter.PropertyDescriptor;
  32 import javafx.beans.InvalidationListener;
  33 import javafx.beans.property.DoubleProperty;
  34 import javafx.beans.value.ChangeListener;
  35 import javafx.beans.value.ObservableValue;
  36 
  37 import java.lang.reflect.InvocationTargetException;
  38 import java.lang.reflect.UndeclaredThrowableException;
  39 
  40 import java.security.AccessControlContext;
  41 import java.security.AccessController;
  42 import java.security.PrivilegedAction;
  43 
  44 /**
  45  * A {@code JavaBeanDoubleProperty} provides an adapter between a regular
  46  * Java Bean property of type {@code double} or {@code Double} and a JavaFX
  47  * {@code DoubleProperty}. It cannot be created directly, but a
  48  * {@link JavaBeanDoublePropertyBuilder} has to be used.
  49  * <p>
  50  * As a minimum, the Java Bean class must implement a getter and a setter for the
  51  * property.
  52  * The class, as well as the getter and a setter methods, must be declared public.
  53  * If the getter of an instance of this class is called, the property of
  54  * the Java Bean is returned. If the setter is called, the value will be passed
  55  * to the Java Bean property. If the Java Bean property is bound (i.e. it supports
  56  * PropertyChangeListeners), this {@code JavaBeanDoubleProperty} will be
  57  * aware of changes in the Java Bean. Otherwise it can be notified about
  58  * changes by calling {@link #fireValueChangedEvent()}. If the Java Bean property
  59  * is also constrained (i.e. it supports VetoableChangeListeners), this
  60  * {@code JavaBeanDoubleProperty} will reject changes, if it is bound to an
  61  * {@link javafx.beans.value.ObservableValue ObservableValue&lt;Double&gt;}.
  62  * </p>
  63  * <p><b>Deploying an Application as a Module</b></p>
  64  * <p>
  65  * If the Java Bean class is in a named module, then it must be reflectively
  66  * accessible to the {@code javafx.base} module.
  67  * A class is reflectively accessible if the module
  68  * {@link Module#isOpen(String,Module) opens} the containing package to at
  69  * least the {@code javafx.base} module.
  70  * </p>
  71  * <p>
  72  * For example, if {@code com.foo.MyBeanClass} is in the {@code foo.app} module,
  73  * the {@code module-info.java} might
  74  * look like this:
  75  * </p>
  76  *
  77 <pre>{@code module foo.app {
  78     opens com.foo to javafx.base;
  79 }}</pre>
  80  *
  81  * <p>
  82  * Alternatively, a class is reflectively accessible if the module
  83  * {@link Module#isExported(String) exports} the containing package
  84  * unconditionally.
  85  * </p>
  86  *
  87  * @see javafx.beans.property.DoubleProperty
  88  * @see JavaBeanDoublePropertyBuilder
  89  * @since JavaFX 2.1
  90  */
  91 public final class JavaBeanDoubleProperty extends DoubleProperty implements JavaBeanProperty<Number> {
  92 
  93     private final PropertyDescriptor descriptor;
  94     private final PropertyDescriptor.Listener<Number> listener;
  95 
  96     private ObservableValue<? extends Number> observable = null;
  97     private ExpressionHelper<Number> helper = null;
  98 
  99     private final AccessControlContext acc = AccessController.getContext();
 100 
 101     JavaBeanDoubleProperty(PropertyDescriptor descriptor, Object bean) {
 102         this.descriptor = descriptor;
 103         this.listener = descriptor.new Listener<Number>(bean, this);
 104         descriptor.addListener(listener);
 105         Disposer.addRecord(this, new DescriptorListenerCleaner(descriptor, listener));
 106     }
 107 
 108     /**
 109      * {@inheritDoc}
 110      *
 111      * @throws UndeclaredThrowableException if calling the getter of the Java Bean
 112      * property throws an {@code IllegalAccessException} or an
 113      * {@code InvocationTargetException}.
 114      */
 115     @Override
 116     public double get() {
 117         return AccessController.doPrivileged((PrivilegedAction<Double>) () -> {
 118             try {
 119                 return ((Number)MethodHelper.invoke(
 120                     descriptor.getGetter(), getBean(), (Object[])null)).doubleValue();
 121             } catch (IllegalAccessException e) {
 122                 throw new UndeclaredThrowableException(e);
 123             } catch (InvocationTargetException e) {
 124                 throw new UndeclaredThrowableException(e);
 125             }
 126         }, acc);
 127     }
 128 
 129     /**
 130      * {@inheritDoc}
 131      *
 132      * @throws UndeclaredThrowableException if calling the getter of the Java Bean
 133      * property throws an {@code IllegalAccessException} or an
 134      * {@code InvocationTargetException}.
 135      */
 136     @Override
 137     public void set(final double value) {
 138         if (isBound()) {
 139             throw new RuntimeException("A bound value cannot be set.");
 140         }
 141 
 142         AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
 143             try {
 144                 MethodHelper.invoke(descriptor.getSetter(), getBean(), new Object[] {value});
 145                 ExpressionHelper.fireValueChangedEvent(helper);
 146             } catch (IllegalAccessException e) {
 147                 throw new UndeclaredThrowableException(e);
 148             } catch (InvocationTargetException e) {
 149                 throw new UndeclaredThrowableException(e);
 150             }
 151             return null;
 152         }, acc);
 153     }
 154 
 155     /**
 156      * {@inheritDoc}
 157      */
 158     @Override
 159     public void bind(ObservableValue<? extends Number> observable) {
 160         if (observable == null) {
 161             throw new NullPointerException("Cannot bind to null");
 162         }
 163 
 164         if (!observable.equals(this.observable)) {
 165             unbind();
 166             set(observable.getValue().doubleValue());
 167             this.observable = observable;
 168             this.observable.addListener(listener);
 169         }
 170     }
 171 
 172     /**
 173      * {@inheritDoc}
 174      */
 175     @Override
 176     public void unbind() {
 177         if (observable != null) {
 178             observable.removeListener(listener);
 179             observable = null;
 180         }
 181     }
 182 
 183     /**
 184      * {@inheritDoc}
 185      */
 186     @Override
 187     public boolean isBound() {
 188         return observable != null;
 189     }
 190 
 191     /**
 192      * {@inheritDoc}
 193      */
 194     @Override
 195     public Object getBean() {
 196         return listener.getBean();
 197     }
 198 
 199     /**
 200      * {@inheritDoc}
 201      */
 202     @Override
 203     public String getName() {
 204         return descriptor.getName();
 205     }
 206 
 207     /**
 208      * {@inheritDoc}
 209      */
 210     @Override
 211     public void addListener(ChangeListener<? super Number> listener) {
 212         helper = ExpressionHelper.addListener(helper, this, listener);
 213     }
 214 
 215     /**
 216      * {@inheritDoc}
 217      */
 218     @Override
 219     public void removeListener(ChangeListener<? super Number> listener) {
 220         helper = ExpressionHelper.removeListener(helper, listener);
 221     }
 222 
 223     /**
 224      * {@inheritDoc}
 225      */
 226     @Override
 227     public void addListener(InvalidationListener listener) {
 228         helper = ExpressionHelper.addListener(helper, this, listener);
 229     }
 230 
 231     /**
 232      * {@inheritDoc}
 233      */
 234     @Override
 235     public void removeListener(InvalidationListener listener) {
 236         helper = ExpressionHelper.removeListener(helper, listener);
 237     }
 238 
 239     /**
 240      * {@inheritDoc}
 241      */
 242     @Override
 243     public void fireValueChangedEvent() {
 244         ExpressionHelper.fireValueChangedEvent(helper);
 245     }
 246 
 247     /**
 248      * {@inheritDoc}
 249      */
 250     @Override
 251     public void dispose() {
 252         descriptor.removeListener(listener);
 253 
 254     }
 255 
 256     /**
 257      * Returns a string representation of this {@code JavaBeanDoubleProperty} object.
 258      * @return a string representation of this {@code JavaBeanDoubleProperty} object.
 259      */
 260     @Override
 261     public String toString() {
 262         final Object bean = getBean();
 263         final String name = getName();
 264         final StringBuilder result = new StringBuilder("DoubleProperty [");
 265         if (bean != null) {
 266             result.append("bean: ").append(bean).append(", ");
 267         }
 268         if ((name != null) && (!name.equals(""))) {
 269             result.append("name: ").append(name).append(", ");
 270         }
 271         if (isBound()) {
 272             result.append("bound, ");
 273         }
 274         result.append("value: ").append(get());
 275         result.append("]");
 276         return result.toString();
 277     }
 278 }