1 /* 2 * Copyright (c) 2011, 2015, 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.adapter.Disposer; 30 import com.sun.javafx.property.adapter.PropertyDescriptor; 31 import javafx.beans.InvalidationListener; 32 import javafx.beans.property.StringProperty; 33 import javafx.beans.value.ChangeListener; 34 import javafx.beans.value.ObservableValue; 35 36 import java.lang.reflect.InvocationTargetException; 37 import java.lang.reflect.UndeclaredThrowableException; 38 39 import java.security.AccessController; 40 import java.security.AccessControlContext; 41 import java.security.PrivilegedAction; 42 43 import sun.reflect.misc.MethodUtil; 44 45 /** 46 * A {@code JavaBeanStringProperty} provides an adapter between a regular 47 * Java Bean property of type {@code String} and a JavaFX 48 * {@code StringProperty}. It cannot be created directly, but a 49 * {@link JavaBeanStringPropertyBuilder} has to be used. 50 * <p> 51 * As a minimum, the Java Bean must implement a getter and a setter for the 52 * property. If the getter of an instance of this class is called, the property of 53 * the Java Bean is returned. If the setter is called, the value will be passed 54 * to the Java Bean property. If the Java Bean property is bound (i.e. it supports 55 * PropertyChangeListeners), this {@code JavaBeanStringProperty} will be 56 * aware of changes in the Java Bean. Otherwise it can be notified about 57 * changes by calling {@link #fireValueChangedEvent()}. If the Java Bean property 58 * is also constrained (i.e. it supports VetoableChangeListeners), this 59 * {@code JavaBeanStringProperty} will reject changes, if it is bound to an 60 * {@link javafx.beans.value.ObservableValue ObservableValue<String>}. 61 * 62 * @see javafx.beans.property.StringProperty 63 * @see JavaBeanStringPropertyBuilder 64 * @since JavaFX 2.1 65 */ 66 public final class JavaBeanStringProperty extends StringProperty implements JavaBeanProperty<String> { 67 68 private final PropertyDescriptor descriptor; 69 private final PropertyDescriptor.Listener<String> listener; 70 71 private ObservableValue<? extends String> observable = null; 72 private ExpressionHelper<String> helper = null; 73 74 private final AccessControlContext acc = AccessController.getContext(); 75 76 JavaBeanStringProperty(PropertyDescriptor descriptor, Object bean) { 77 this.descriptor = descriptor; 78 this.listener = descriptor.new Listener<String>(bean, this); 79 descriptor.addListener(listener); 80 Disposer.addRecord(this, new DescriptorListenerCleaner(descriptor, listener)); 81 } 82 83 /** 84 * {@inheritDoc} 85 * 86 * @throws UndeclaredThrowableException if calling the getter of the Java Bean 87 * property throws an {@code IllegalAccessException} or an 88 * {@code InvocationTargetException}. 89 */ 90 @Override 91 public String get() { 92 return AccessController.doPrivileged((PrivilegedAction<String>) () -> { 93 try { 94 return (String)MethodUtil.invoke(descriptor.getGetter(), getBean(), (Object[])null); 95 } catch (IllegalAccessException e) { 96 throw new UndeclaredThrowableException(e); 97 } catch (InvocationTargetException e) { 98 throw new UndeclaredThrowableException(e); 99 } 100 }, acc); 101 } 102 103 /** 104 * {@inheritDoc} 105 * 106 * @throws UndeclaredThrowableException if calling the getter of the Java Bean 107 * property throws an {@code IllegalAccessException} or an 108 * {@code InvocationTargetException}. 109 */ 110 @Override 111 public void set(final String value) { 112 if (isBound()) { 113 throw new RuntimeException("A bound value cannot be set."); 114 } 115 AccessController.doPrivileged((PrivilegedAction<Void>) () -> { 116 try { 117 MethodUtil.invoke(descriptor.getSetter(), getBean(), new Object[] {value}); 118 ExpressionHelper.fireValueChangedEvent(helper); 119 } catch (IllegalAccessException e) { 120 throw new UndeclaredThrowableException(e); 121 } catch (InvocationTargetException e) { 122 throw new UndeclaredThrowableException(e); 123 } 124 return null; 125 }, acc); 126 } 127 128 /** 129 * {@inheritDoc} 130 */ 131 @Override 132 public void bind(ObservableValue<? extends String> observable) { 133 if (observable == null) { 134 throw new NullPointerException("Cannot bind to null"); 135 } 136 137 if (!observable.equals(this.observable)) { 138 unbind(); 139 set(observable.getValue()); 140 this.observable = observable; 141 this.observable.addListener(listener); 142 } 143 } 144 145 /** 146 * {@inheritDoc} 147 */ 148 @Override 149 public void unbind() { 150 if (observable != null) { 151 observable.removeListener(listener); 152 observable = null; 153 } 154 } 155 156 /** 157 * {@inheritDoc} 158 */ 159 @Override 160 public boolean isBound() { 161 return observable != null; 162 } 163 164 /** 165 * {@inheritDoc} 166 */ 167 @Override 168 public Object getBean() { 169 return listener.getBean(); 170 } 171 172 /** 173 * {@inheritDoc} 174 */ 175 @Override 176 public String getName() { 177 return descriptor.getName(); 178 } 179 180 /** 181 * {@inheritDoc} 182 */ 183 @Override 184 public void addListener(ChangeListener<? super String> listener) { 185 helper = ExpressionHelper.addListener(helper, this, listener); 186 } 187 188 /** 189 * {@inheritDoc} 190 */ 191 @Override 192 public void removeListener(ChangeListener<? super String> listener) { 193 helper = ExpressionHelper.removeListener(helper, listener); 194 } 195 196 /** 197 * {@inheritDoc} 198 */ 199 @Override 200 public void addListener(InvalidationListener listener) { 201 helper = ExpressionHelper.addListener(helper, this, listener); 202 } 203 204 /** 205 * {@inheritDoc} 206 */ 207 @Override 208 public void removeListener(InvalidationListener listener) { 209 helper = ExpressionHelper.removeListener(helper, listener); 210 } 211 212 /** 213 * {@inheritDoc} 214 */ 215 @Override 216 public void fireValueChangedEvent() { 217 ExpressionHelper.fireValueChangedEvent(helper); 218 } 219 220 /** 221 * {@inheritDoc} 222 */ 223 @Override 224 public void dispose() { 225 descriptor.removeListener(listener); 226 227 } 228 }