1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
/*******************************************************************************
* Copyright (c) 2005, 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
* Matthew Hall - bug 118516
*******************************************************************************/
package org.eclipse.fx.databinding.internal;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IStatus;
/**
* This is a helper that will hook up and listen for
* <code>PropertyChangeEvent</code> events for a set of target JavaBeans
*
* @since 1.0
*/
public class FXBeanPropertyListenerSupport {
/**
* Start listen to target (if it supports the JavaBean property change
* listener pattern)
*
* @param bean
* @param propertyName
* @param listener
*/
public static void hookListener(Object bean, String propertyName,
ChangeListener<Object> listener) {
Assert.isNotNull(bean, "Bean cannot be null"); //$NON-NLS-1$
Assert.isNotNull(listener, "Listener cannot be null"); //$NON-NLS-1$
Assert.isNotNull(propertyName, "Property name cannot be null"); //$NON-NLS-1$
processListener(bean, propertyName, listener,
/*"addPropertyChangeListener",*/ "Could not attach listener to ");//$NON-NLS-1$ //$NON-NLS-2$
}
/**
* Stop listen to target
*
* @param bean
* @param propertyName
* @param listener
*/
public static void unhookListener(Object bean, String propertyName,
ChangeListener<Object> listener) {
Assert.isNotNull(bean, "Bean cannot be null"); //$NON-NLS-1$
Assert.isNotNull(listener, "Listener cannot be null"); //$NON-NLS-1$
Assert.isNotNull(propertyName, "Property name cannot be null"); //$NON-NLS-1$
processListener(
bean,
propertyName,
listener,
/*"removePropertyChangeListener",*/ "Cound not remove listener from "); //$NON-NLS-1$ //$NON-NLS-2$
}
/**
* Invokes the method for the provided <code>methodName</code> attempting to
* first use the method with the property name and then the unnamed version.
*
* @param bean
* object to invoke the method on
* @param methodName
* either addPropertyChangeListener or
* removePropertyChangeListener
* @param message
* string that will be prefixed to the target in an error message
*
* @return <code>true</code> if the method was invoked successfully
*/
@SuppressWarnings("unchecked")
private static boolean processListener(Object bean, String propertyName,
ChangeListener<Object> listener, /*String methodName,*/ String message) {
try {
Method method = bean.getClass().getMethod(propertyName+"Property");
Object observable = method.invoke(bean);
if( observable instanceof ObservableValue<?> ) {
((ObservableValue<Object>) observable).addListener(listener);
return true;
} else {
log(IStatus.WARNING, message, null);
}
} catch (SecurityException e) {
// ignore
} catch (NoSuchMethodException e) {
log(IStatus.WARNING, message + bean, e);
} catch (IllegalArgumentException e) {
log(IStatus.WARNING, message + bean, e);
} catch (IllegalAccessException e) {
log(IStatus.WARNING, message + bean, e);
} catch (InvocationTargetException e) {
log(IStatus.WARNING, message + bean, e);
}
return false;
}
/**
* Logs a message to the Data Binding logger.
*/
private static void log(int severity, String message, Throwable throwable) {
/*if (BeansObservables.DEBUG) {
Policy.getLog().log(
new Status(severity, Policy.JFACE_DATABINDING, IStatus.OK,
message, throwable));
}*/
}
}
|