Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
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.core.internal.databinding.beans;
 
 
This is a helper that will hook up and listen for PropertyChangeEvent events for a set of target JavaBeans

Since:
1.0
 
 public class BeanPropertyListenerSupport {
Start listen to target (if it supports the JavaBean property change listener pattern)

Parameters:
bean
propertyName
listener
 
 	public static void hookListener(Object beanString propertyName,
 			PropertyChangeListener 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(beanpropertyNamelistener,
 				"addPropertyChangeListener""Could not attach listener to ");//$NON-NLS-1$ //$NON-NLS-2$
 	}

Stop listen to target

Parameters:
bean
propertyName
listener
 
 	public static void unhookListener(Object beanString propertyName,
 			PropertyChangeListener 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$
 
 				bean,
 				propertyName,
 				listener,
 				"removePropertyChangeListener""Cound not remove listener from "); //$NON-NLS-1$ //$NON-NLS-2$
 	}

Invokes the method for the provided methodName attempting to first use the method with the property name and then the unnamed version.

Parameters:
bean object to invoke the method on
methodName either addPropertyChangeListener or removePropertyChangeListener
message string that will be prefixed to the target in an error message
Returns:
true if the method was invoked successfully
 
 	private static boolean processListener(Object beanString propertyName,
 			PropertyChangeListener listenerString methodNameString message) {
 		Method method = null;
 		Object[] parameters = null;
 
 		try {
 			try {
 				method = bean.getClass().getMethod(
 						methodName,
 						new Class[] { String.class,
 
 				parameters = new Object[] { propertyNamelistener };
 			} catch (NoSuchMethodException e) {
 				method = bean.getClass().getMethod(methodName,
 						new Class[] { PropertyChangeListener.class });
 
 				parameters = new Object[] { listener };
			}
catch (SecurityException e) {
			// ignore
catch (NoSuchMethodException e) {
			log(.message + beane);
		}
		if (method != null) {
			if (!method.isAccessible()) {
				method.setAccessible(true);
			}
			try {
				method.invoke(beanparameters);
				return true;
catch (IllegalArgumentException e) {
				log(.message + beane);
catch (IllegalAccessException e) {
				log(.message + beane);
catch (InvocationTargetException e) {
				log(.message + beane);
			}
		}
		return false;
	}

Logs a message to the Data Binding logger.
	private static void log(int severityString messageThrowable throwable) {
			Policy.getLog().log(
							messagethrowable));
		}
	}
New to GrepCode? Check out our FAQ X