1 /*
2 * Copyright (c) 2011, 2013, 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 com.sun.javafx.property.adapter;
27
28 import javafx.beans.property.Property;
29 import javafx.beans.property.adapter.ReadOnlyJavaBeanProperty;
30 import javafx.beans.value.ChangeListener;
31 import javafx.beans.value.ObservableValue;
32
33 import java.beans.PropertyChangeEvent;
34 import java.beans.PropertyVetoException;
35 import java.beans.VetoableChangeListener;
36 import java.lang.reflect.InvocationTargetException;
37 import java.lang.reflect.Method;
38
39 import sun.reflect.misc.MethodUtil;
40
41 /**
42 */
43 public class PropertyDescriptor extends ReadOnlyPropertyDescriptor {
44
45 private static final String ADD_VETOABLE_LISTENER_METHOD_NAME = "addVetoableChangeListener";
46 private static final String REMOVE_VETOABLE_LISTENER_METHOD_NAME = "removeVetoableChangeListener";
47 private static final String ADD_PREFIX = "add";
48 private static final String REMOVE_PREFIX = "remove";
49 private static final String SUFFIX = "Listener";
50
51 private static final int ADD_VETOABLE_LISTENER_TAKES_NAME = 1;
52 private static final int REMOVE_VETOABLE_LISTENER_TAKES_NAME = 2;
53
54 private final Method setter;
55 private final Method addVetoListener;
56 private final Method removeVetoListener;
57 private final int flags;
58
59 public Method getSetter() {return setter;}
60
142 }
143 }
144 }
145
146 public class Listener<T> extends ReadOnlyListener<T> implements ChangeListener<T>, VetoableChangeListener {
147
148 private boolean updating;
149
150 public Listener(Object bean, ReadOnlyJavaBeanProperty<T> property) {
151 super(bean, property);
152 }
153
154 @Override
155 public void changed(ObservableValue<? extends T> observable, T oldValue, T newValue) {
156 final ReadOnlyJavaBeanProperty<T> property = checkRef();
157 if (property == null) {
158 observable.removeListener(this);
159 } else if (!updating) {
160 updating = true;
161 try {
162 MethodUtil.invoke(setter, bean, new Object[] {newValue});
163 property.fireValueChangedEvent();
164 } catch (IllegalAccessException e) {
165 // ignore
166 } catch (InvocationTargetException e) {
167 // ignore
168 } finally {
169 updating = false;
170 }
171 }
172 }
173
174 @Override
175 public void vetoableChange(PropertyChangeEvent propertyChangeEvent) throws PropertyVetoException {
176 if (bean.equals(propertyChangeEvent.getSource()) && name.equals(propertyChangeEvent.getPropertyName())) {
177 final ReadOnlyJavaBeanProperty<T> property = checkRef();
178 if ((property instanceof Property) && (((Property)property).isBound()) && !updating) {
179 throw new PropertyVetoException("A bound value cannot be set.", propertyChangeEvent);
180 }
181 }
182 }
|
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 com.sun.javafx.property.adapter;
27
28 import com.sun.javafx.property.MethodHelper;
29 import javafx.beans.property.Property;
30 import javafx.beans.property.adapter.ReadOnlyJavaBeanProperty;
31 import javafx.beans.value.ChangeListener;
32 import javafx.beans.value.ObservableValue;
33
34 import java.beans.PropertyChangeEvent;
35 import java.beans.PropertyVetoException;
36 import java.beans.VetoableChangeListener;
37 import java.lang.reflect.InvocationTargetException;
38 import java.lang.reflect.Method;
39
40 /**
41 */
42 public class PropertyDescriptor extends ReadOnlyPropertyDescriptor {
43
44 private static final String ADD_VETOABLE_LISTENER_METHOD_NAME = "addVetoableChangeListener";
45 private static final String REMOVE_VETOABLE_LISTENER_METHOD_NAME = "removeVetoableChangeListener";
46 private static final String ADD_PREFIX = "add";
47 private static final String REMOVE_PREFIX = "remove";
48 private static final String SUFFIX = "Listener";
49
50 private static final int ADD_VETOABLE_LISTENER_TAKES_NAME = 1;
51 private static final int REMOVE_VETOABLE_LISTENER_TAKES_NAME = 2;
52
53 private final Method setter;
54 private final Method addVetoListener;
55 private final Method removeVetoListener;
56 private final int flags;
57
58 public Method getSetter() {return setter;}
59
141 }
142 }
143 }
144
145 public class Listener<T> extends ReadOnlyListener<T> implements ChangeListener<T>, VetoableChangeListener {
146
147 private boolean updating;
148
149 public Listener(Object bean, ReadOnlyJavaBeanProperty<T> property) {
150 super(bean, property);
151 }
152
153 @Override
154 public void changed(ObservableValue<? extends T> observable, T oldValue, T newValue) {
155 final ReadOnlyJavaBeanProperty<T> property = checkRef();
156 if (property == null) {
157 observable.removeListener(this);
158 } else if (!updating) {
159 updating = true;
160 try {
161 MethodHelper.invoke(setter, bean, new Object[] {newValue});
162 property.fireValueChangedEvent();
163 } catch (IllegalAccessException e) {
164 // ignore
165 } catch (InvocationTargetException e) {
166 // ignore
167 } finally {
168 updating = false;
169 }
170 }
171 }
172
173 @Override
174 public void vetoableChange(PropertyChangeEvent propertyChangeEvent) throws PropertyVetoException {
175 if (bean.equals(propertyChangeEvent.getSource()) && name.equals(propertyChangeEvent.getPropertyName())) {
176 final ReadOnlyJavaBeanProperty<T> property = checkRef();
177 if ((property instanceof Property) && (((Property)property).isBound()) && !updating) {
178 throw new PropertyVetoException("A bound value cannot be set.", propertyChangeEvent);
179 }
180 }
181 }
|