Spec-Zone .ru
спецификации, руководства, описания, API
|
001/* 002 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. 003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 004 * 005 * This code is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU General Public License version 2 only, as 007 * published by the Free Software Foundation. Oracle designates this 008 * particular file as subject to the "Classpath" exception as provided 009 * by Oracle in the LICENSE file that accompanied this code. 010 * 011 * This code is distributed in the hope that it will be useful, but WITHOUT 012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 014 * version 2 for more details (a copy is included in the LICENSE file that 015 * accompanied this code). 016 * 017 * You should have received a copy of the GNU General Public License version 018 * 2 along with this work; if not, write to the Free Software Foundation, 019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 020 * 021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 022 * or visit www.oracle.com if you need additional information or have any 023 * questions. 024 */ 025 026package javafx.beans.property.adapter; 027 028import com.sun.javafx.binding.ExpressionHelper; 029import com.sun.javafx.property.adapter.PropertyDescriptor; 030import javafx.beans.InvalidationListener; 031import javafx.beans.property.StringProperty; 032import javafx.beans.value.ChangeListener; 033import javafx.beans.value.ObservableValue; 034 035import java.lang.reflect.InvocationTargetException; 036import java.lang.reflect.UndeclaredThrowableException; 037import sun.misc.Cleaner; 038 039import java.security.AccessController; 040import java.security.AccessControlContext; 041import java.security.PrivilegedAction; 042 043import sun.reflect.misc.MethodUtil; 044 045/** 046 * A {@code JavaBeanStringProperty} provides an adapter between a regular 047 * Java Bean property of type {@code String} and a JavaFX 048 * {@code StringProperty}. It cannot be created directly, but a 049 * {@link JavaBeanStringPropertyBuilder} has to be used. 050 * <p> 051 * As a minimum, the Java Bean must implement a getter and a setter for the 052 * property. If the getter of an instance of this class is called, the property of 053 * the Java Bean is returned. If the setter is called, the value will be passed 054 * to the Java Bean property. If the Java Bean property is bound (i.e. it supports 055 * PropertyChangeListeners), this {@code JavaBeanStringProperty} will be 056 * aware of changes in the Java Bean. Otherwise it can be notified about 057 * changes by calling {@link #fireValueChangedEvent()}. If the Java Bean property 058 * is also constrained (i.e. it supports VetoableChangeListeners), this 059 * {@code JavaBeanStringProperty} will reject changes, if it is bound to an 060 * {@link javafx.beans.value.ObservableValue ObservableValue<String>}. 061 * 062 * @see javafx.beans.property.StringProperty 063 * @see JavaBeanStringPropertyBuilder 064 */ 065public final class JavaBeanStringProperty extends StringProperty implements JavaBeanProperty<String> { 066 067 private final PropertyDescriptor descriptor; 068 private final PropertyDescriptor.Listener<String> listener; 069 070 private ObservableValue<? extends String> observable = null; 071 private ExpressionHelper<String> helper = null; 072 073 private final AccessControlContext acc = AccessController.getContext(); 074 075 JavaBeanStringProperty(PropertyDescriptor descriptor, Object bean) { 076 this.descriptor = descriptor; 077 this.listener = descriptor.new Listener<String>(bean, this); 078 descriptor.addListener(listener); 079 Cleaner.create(this, new Runnable() { 080 @Override 081 public void run() { 082 JavaBeanStringProperty.this.descriptor.removeListener(listener); 083 } 084 }); 085 } 086 087 /** 088 * {@inheritDoc} 089 * 090 * @throws UndeclaredThrowableException if calling the getter of the Java Bean 091 * property throws an {@code IllegalAccessException} or an 092 * {@code InvocationTargetException}. 093 */ 094 @Override 095 public String get() { 096 return AccessController.doPrivileged(new PrivilegedAction<String>() { 097 public String run() { 098 try { 099 return (String)MethodUtil.invoke(descriptor.getGetter(), getBean(), (Object[])null); 100 } catch (IllegalAccessException e) { 101 throw new UndeclaredThrowableException(e); 102 } catch (InvocationTargetException e) { 103 throw new UndeclaredThrowableException(e); 104 } 105 } 106 }, acc); 107 } 108 109 /** 110 * {@inheritDoc} 111 * 112 * @throws UndeclaredThrowableException if calling the getter of the Java Bean 113 * property throws an {@code IllegalAccessException} or an 114 * {@code InvocationTargetException}. 115 */ 116 @Override 117 public void set(final String value) { 118 if (isBound()) { 119 throw new RuntimeException("A bound value cannot be set."); 120 } 121 AccessController.doPrivileged(new PrivilegedAction<Void>() { 122 public Void run() { 123 try { 124 MethodUtil.invoke(descriptor.getSetter(), getBean(), new Object[] {value}); 125 ExpressionHelper.fireValueChangedEvent(helper); 126 } catch (IllegalAccessException e) { 127 throw new UndeclaredThrowableException(e); 128 } catch (InvocationTargetException e) { 129 throw new UndeclaredThrowableException(e); 130 } 131 return null; 132 } 133 }, acc); 134 } 135 136 /** 137 * {@inheritDoc} 138 */ 139 @Override 140 public void bind(ObservableValue<? extends String> 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()); 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 String> listener) { 193 helper = ExpressionHelper.addListener(helper, this, listener); 194 } 195 196 /** 197 * {@inheritDoc} 198 */ 199 @Override 200 public void removeListener(ChangeListener<? super String> 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}