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.ObjectProperty; 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.AccessController; 41 import java.security.AccessControlContext; 42 import java.security.PrivilegedAction; 43 44 /** 45 * A {@code JavaBeanObjectProperty} provides an adapter between a regular 46 * Java Bean property of type {@code T} and a JavaFX 47 * {@code ObjectProperty<T>}. It cannot be created directly, but a 48 * {@link JavaBeanObjectPropertyBuilder} 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 JavaBeanObjectProperty} 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 JavaBeanObjectProperty} will reject changes, if it is bound to an 59 * {@link javafx.beans.value.ObservableValue ObservableValue<Object>}. 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.ObjectProperty 70 * @see JavaBeanObjectPropertyBuilder 71 * 72 * @param T type of the wrapped {@code Object} 73 * @since JavaFX 2.1 74 */ 75 public final class JavaBeanObjectProperty<T> extends ObjectProperty<T> implements JavaBeanProperty<T> { 76 77 private final PropertyDescriptor descriptor; 78 private final PropertyDescriptor.Listener<T> listener; 79 80 private ObservableValue<? extends T> observable = null; 81 private ExpressionHelper<T> helper = null; 82 83 private final AccessControlContext acc = AccessController.getContext(); 84 85 JavaBeanObjectProperty(PropertyDescriptor descriptor, Object bean) { 86 this.descriptor = descriptor; 87 this.listener = descriptor.new Listener<T>(bean, this); 88 descriptor.addListener(listener); 89 Disposer.addRecord(this, new DescriptorListenerCleaner(descriptor, listener)); 90 } 91 92 /** 93 * {@inheritDoc} 94 * 95 * @throws UndeclaredThrowableException if calling the getter of the Java Bean 96 * property throws an {@code IllegalAccessException} or an 97 * {@code InvocationTargetException}. 98 */ 99 @SuppressWarnings("unchecked") 100 @Override 101 public T get() { 102 return AccessController.doPrivileged((PrivilegedAction<T>) () -> { 103 try { 104 return (T)MethodHelper.invoke(descriptor.getGetter(), getBean(), (Object[])null); 105 } catch (IllegalAccessException e) { 106 throw new UndeclaredThrowableException(e); 107 } catch (InvocationTargetException e) { 108 throw new UndeclaredThrowableException(e); 109 } 110 }, acc); 111 } 112 113 /** 114 * {@inheritDoc} 115 * 116 * @throws UndeclaredThrowableException if calling the getter of the Java Bean 117 * property throws an {@code IllegalAccessException} or an 118 * {@code InvocationTargetException}. 119 */ 120 @Override 121 public void set(final T value) { 122 if (isBound()) { 123 throw new RuntimeException("A bound value cannot be set."); 124 } 125 126 AccessController.doPrivileged((PrivilegedAction<Void>) () -> { 127 try { 128 MethodHelper.invoke(descriptor.getSetter(), getBean(), new Object[] {value}); 129 ExpressionHelper.fireValueChangedEvent(helper); 130 } catch (IllegalAccessException e) { 131 throw new UndeclaredThrowableException(e); 132 } catch (InvocationTargetException e) { 133 throw new UndeclaredThrowableException(e); 134 } 135 return null; 136 }, acc); 137 } 138 139 140 /** 141 * {@inheritDoc} 142 */ 143 @Override 144 public void bind(ObservableValue<? extends T> observable) { 145 if (observable == null) { 146 throw new NullPointerException("Cannot bind to null"); 147 } 148 149 if (!observable.equals(this.observable)) { 150 unbind(); 151 set(observable.getValue()); 152 this.observable = observable; 153 this.observable.addListener(listener); 154 } 155 } 156 157 /** 158 * {@inheritDoc} 159 */ 160 @Override 161 public void unbind() { 162 if (observable != null) { 163 observable.removeListener(listener); 164 observable = null; 165 } 166 } 167 168 /** 169 * {@inheritDoc} 170 */ 171 @Override 172 public boolean isBound() { 173 return observable != null; 174 } 175 176 /** 177 * {@inheritDoc} 178 */ 179 @Override 180 public Object getBean() { 181 return listener.getBean(); 182 } 183 184 /** 185 * {@inheritDoc} 186 */ 187 @Override 188 public String getName() { 189 return descriptor.getName(); 190 } 191 192 /** 193 * {@inheritDoc} 194 */ 195 @Override 196 public void addListener(ChangeListener<? super T> listener) { 197 helper = ExpressionHelper.addListener(helper, this, listener); 198 } 199 200 /** 201 * {@inheritDoc} 202 */ 203 @Override 204 public void removeListener(ChangeListener<? super T> listener) { 205 helper = ExpressionHelper.removeListener(helper, listener); 206 } 207 208 /** 209 * {@inheritDoc} 210 */ 211 @Override 212 public void addListener(InvalidationListener listener) { 213 helper = ExpressionHelper.addListener(helper, this, listener); 214 } 215 216 /** 217 * {@inheritDoc} 218 */ 219 @Override 220 public void removeListener(InvalidationListener listener) { 221 helper = ExpressionHelper.removeListener(helper, listener); 222 } 223 224 /** 225 * {@inheritDoc} 226 */ 227 @Override 228 public void fireValueChangedEvent() { 229 ExpressionHelper.fireValueChangedEvent(helper); 230 } 231 232 /** 233 * {@inheritDoc} 234 */ 235 @Override 236 public void dispose() { 237 descriptor.removeListener(listener); 238 239 } 240 241 /** 242 * Returns a string representation of this {@code JavaBeanObjectProperty} object. 243 * @return a string representation of this {@code JavaBeanObjectProperty} object. 244 */ 245 @Override 246 public String toString() { 247 final Object bean = getBean(); 248 final String name = getName(); 249 final StringBuilder result = new StringBuilder("ObjectProperty ["); 250 if (bean != null) { 251 result.append("bean: ").append(bean).append(", "); 252 } 253 if ((name != null) && (!name.equals(""))) { 254 result.append("name: ").append(name).append(", "); 255 } 256 if (isBound()) { 257 result.append("bound, "); 258 } 259 result.append("value: ").append(get()); 260 result.append("]"); 261 return result.toString(); 262 } 263 }