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.property.adapter.ReadOnlyPropertyDescriptor; 029import javafx.beans.property.ReadOnlyBooleanPropertyBase; 030 031import java.lang.reflect.InvocationTargetException; 032import java.lang.reflect.UndeclaredThrowableException; 033import sun.misc.Cleaner; 034 035import java.security.AccessController; 036import java.security.AccessControlContext; 037import java.security.PrivilegedAction; 038 039import sun.reflect.misc.MethodUtil; 040 041/** 042 * A {@code ReadOnlyJavaBeanBooleanProperty} provides an adapter between a regular 043 * read only Java Bean property of type {@code boolean} or {@code Boolean} and a JavaFX 044 * {@code ReadOnlyBooleanProperty}. It cannot be created directly, but a 045 * {@link ReadOnlyJavaBeanBooleanPropertyBuilder} has to be used. 046 * <p> 047 * As a minimum, the Java Bean must implement a getter for the 048 * property. If the getter of an instance of this class is called, the property of 049 * the Java Bean is returned. If the Java Bean property is bound (i.e. it supports 050 * PropertyChangeListeners), this {@code ReadOnlyJavaBeanBooleanProperty} will be 051 * aware of changes in the Java Bean. Otherwise it can be notified about 052 * changes by calling {@link #fireValueChangedEvent()}. 053 * 054 * @see javafx.beans.property.ReadOnlyBooleanProperty 055 * @see ReadOnlyJavaBeanBooleanPropertyBuilder 056 */ 057public final class ReadOnlyJavaBeanBooleanProperty extends ReadOnlyBooleanPropertyBase implements ReadOnlyJavaBeanProperty<Boolean> { 058 059 private final ReadOnlyPropertyDescriptor descriptor; 060 private final ReadOnlyPropertyDescriptor.ReadOnlyListener<Boolean> listener; 061 062 private final AccessControlContext acc = AccessController.getContext(); 063 064 ReadOnlyJavaBeanBooleanProperty(ReadOnlyPropertyDescriptor descriptor, Object bean) { 065 this.descriptor = descriptor; 066 this.listener = descriptor.new ReadOnlyListener<Boolean>(bean, this); 067 descriptor.addListener(listener); 068 Cleaner.create(this, new Runnable() { 069 @Override 070 public void run() { 071 ReadOnlyJavaBeanBooleanProperty.this.descriptor.removeListener(listener); 072 } 073 }); 074 } 075 076 /** 077 * {@inheritDoc} 078 * 079 * @throws UndeclaredThrowableException if calling the getter of the Java Bean 080 * property throws an {@code IllegalAccessException} or an 081 * {@code InvocationTargetException}. 082 */ 083 @Override 084 public boolean get() { 085 return AccessController.doPrivileged(new PrivilegedAction<Boolean>() { 086 public Boolean run() { 087 try { 088 return (Boolean)MethodUtil.invoke(descriptor.getGetter(), getBean(), (Object[])null); 089 } catch (IllegalAccessException e) { 090 throw new UndeclaredThrowableException(e); 091 } catch (InvocationTargetException e) { 092 throw new UndeclaredThrowableException(e); 093 } 094 } 095 }, acc); 096 } 097 098 /** 099 * {@inheritDoc} 100 */ 101 @Override 102 public Object getBean() { 103 return listener.getBean(); 104 } 105 106 /** 107 * {@inheritDoc} 108 */ 109 @Override 110 public String getName() { 111 return descriptor.getName(); 112 } 113 114 /** 115 * {@inheritDoc} 116 */ 117 @Override 118 public void fireValueChangedEvent() { 119 super.fireValueChangedEvent(); 120 } 121 122 /** 123 * {@inheritDoc} 124 */ 125 @Override 126 public void dispose() { 127 descriptor.removeListener(listener); 128 } 129}