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.property.adapter.Disposer;
29 import com.sun.javafx.property.adapter.ReadOnlyPropertyDescriptor;
30 import javafx.beans.property.ReadOnlyBooleanPropertyBase;
31
32 import java.lang.reflect.InvocationTargetException;
33 import java.lang.reflect.UndeclaredThrowableException;
34
35 import java.security.AccessController;
36 import java.security.AccessControlContext;
37 import java.security.PrivilegedAction;
38
39 import sun.reflect.misc.MethodUtil;
40
41 /**
42 * A {@code ReadOnlyJavaBeanBooleanProperty} provides an adapter between a regular
43 * read only Java Bean property of type {@code boolean} or {@code Boolean} and a JavaFX
44 * {@code ReadOnlyBooleanProperty}. It cannot be created directly, but a
45 * {@link ReadOnlyJavaBeanBooleanPropertyBuilder} has to be used.
46 * <p>
47 * As a minimum, the Java Bean must implement a getter for the
48 * property. If the getter of an instance of this class is called, the property of
49 * the Java Bean is returned. If the Java Bean property is bound (i.e. it supports
50 * PropertyChangeListeners), this {@code ReadOnlyJavaBeanBooleanProperty} will be
51 * aware of changes in the Java Bean. Otherwise it can be notified about
52 * changes by calling {@link #fireValueChangedEvent()}.
53 *
54 * @see javafx.beans.property.ReadOnlyBooleanProperty
55 * @see ReadOnlyJavaBeanBooleanPropertyBuilder
56 * @since JavaFX 2.1
57 */
58 public final class ReadOnlyJavaBeanBooleanProperty extends ReadOnlyBooleanPropertyBase implements ReadOnlyJavaBeanProperty<Boolean> {
59
60 private final ReadOnlyPropertyDescriptor descriptor;
61 private final ReadOnlyPropertyDescriptor.ReadOnlyListener<Boolean> listener;
62
63 private final AccessControlContext acc = AccessController.getContext();
64
65 ReadOnlyJavaBeanBooleanProperty(ReadOnlyPropertyDescriptor descriptor, Object bean) {
66 this.descriptor = descriptor;
67 this.listener = descriptor.new ReadOnlyListener<Boolean>(bean, this);
68 descriptor.addListener(listener);
69 Disposer.addRecord(this, new DescriptorListenerCleaner(descriptor, listener));
70 }
71
72 /**
73 * {@inheritDoc}
74 *
75 * @throws UndeclaredThrowableException if calling the getter of the Java Bean
76 * property throws an {@code IllegalAccessException} or an
77 * {@code InvocationTargetException}.
78 */
79 @Override
80 public boolean get() {
81 return AccessController.doPrivileged((PrivilegedAction<Boolean>) () -> {
82 try {
83 return (Boolean)MethodUtil.invoke(descriptor.getGetter(), getBean(), (Object[])null);
84 } catch (IllegalAccessException e) {
85 throw new UndeclaredThrowableException(e);
86 } catch (InvocationTargetException e) {
87 throw new UndeclaredThrowableException(e);
88 }
89 }, acc);
90 }
91
92 /**
93 * {@inheritDoc}
94 */
95 @Override
96 public Object getBean() {
97 return listener.getBean();
98 }
99
100 /**
101 * {@inheritDoc}
102 */
103 @Override
|
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.property.MethodHelper;
29 import com.sun.javafx.property.adapter.Disposer;
30 import com.sun.javafx.property.adapter.ReadOnlyPropertyDescriptor;
31 import javafx.beans.property.ReadOnlyBooleanPropertyBase;
32
33 import java.lang.reflect.InvocationTargetException;
34 import java.lang.reflect.UndeclaredThrowableException;
35
36 import java.security.AccessController;
37 import java.security.AccessControlContext;
38 import java.security.PrivilegedAction;
39
40 /**
41 * A {@code ReadOnlyJavaBeanBooleanProperty} provides an adapter between a regular
42 * read only Java Bean property of type {@code boolean} or {@code Boolean} and a JavaFX
43 * {@code ReadOnlyBooleanProperty}. It cannot be created directly, but a
44 * {@link ReadOnlyJavaBeanBooleanPropertyBuilder} has to be used.
45 * <p>
46 * As a minimum, the Java Bean class must implement a getter for the
47 * property. If the getter of an instance of this class is called, the property of
48 * the Java Bean is returned. If the Java Bean property is bound (i.e. it supports
49 * PropertyChangeListeners), this {@code ReadOnlyJavaBeanBooleanProperty} will be
50 * aware of changes in the Java Bean. Otherwise it can be notified about
51 * changes by calling {@link #fireValueChangedEvent()}.
52 * </p>
53 * <p>
54 * The Java Bean class must be declared public. If that class is in a named
55 * module, then the module must {@link Module#isOpen(String,Module) open}
56 * the containing package to at least the {@code javafx.base} module
57 * (or {@link Module#isExported(String) export} the containing package
58 * unconditionally).
59 * </p>
60 *
61 * @see javafx.beans.property.ReadOnlyBooleanProperty
62 * @see ReadOnlyJavaBeanBooleanPropertyBuilder
63 * @since JavaFX 2.1
64 */
65 public final class ReadOnlyJavaBeanBooleanProperty extends ReadOnlyBooleanPropertyBase implements ReadOnlyJavaBeanProperty<Boolean> {
66
67 private final ReadOnlyPropertyDescriptor descriptor;
68 private final ReadOnlyPropertyDescriptor.ReadOnlyListener<Boolean> listener;
69
70 private final AccessControlContext acc = AccessController.getContext();
71
72 ReadOnlyJavaBeanBooleanProperty(ReadOnlyPropertyDescriptor descriptor, Object bean) {
73 this.descriptor = descriptor;
74 this.listener = descriptor.new ReadOnlyListener<Boolean>(bean, this);
75 descriptor.addListener(listener);
76 Disposer.addRecord(this, new DescriptorListenerCleaner(descriptor, listener));
77 }
78
79 /**
80 * {@inheritDoc}
81 *
82 * @throws UndeclaredThrowableException if calling the getter of the Java Bean
83 * property throws an {@code IllegalAccessException} or an
84 * {@code InvocationTargetException}.
85 */
86 @Override
87 public boolean get() {
88 return AccessController.doPrivileged((PrivilegedAction<Boolean>) () -> {
89 try {
90 return (Boolean)MethodHelper.invoke(descriptor.getGetter(), getBean(), (Object[])null);
91 } catch (IllegalAccessException e) {
92 throw new UndeclaredThrowableException(e);
93 } catch (InvocationTargetException e) {
94 throw new UndeclaredThrowableException(e);
95 }
96 }, acc);
97 }
98
99 /**
100 * {@inheritDoc}
101 */
102 @Override
103 public Object getBean() {
104 return listener.getBean();
105 }
106
107 /**
108 * {@inheritDoc}
109 */
110 @Override
|