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