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