package scenegraphdemo; import java.beans.PropertyChangeListener; /** * Static methods for manipulating PCL for standard java beans. * @author Mr. Java * */ public class BeanPropertyUtils { protected final static Class>[] NO_ARGS = new Class>[] { PropertyChangeListener.class }; protected final static Class>[] ARGS = new Class>[] { String.class, PropertyChangeListener.class }; protected final static String pclAddMethodName = "addPropertyChangeListener"; protected final static String pclRemoveMethodName = "removePropertyChangeListener"; /** * Return true if the class has PCL methods. Does not check for remove. * *
* addPropertyChangeListener(PropertyChangeListener) *
* addPropertyChangeListener(String, PropertyChangeListener) * * @param bean * @return */ public static boolean hasPCL(Class> clazz) { try { if (clazz.getMethod(pclAddMethodName, NO_ARGS) != null) { return true; } } catch (Exception e) { // silently fail } try { if (clazz.getMethod(pclAddMethodName, ARGS) != null) { return true; } } catch (Exception e) { // silently fail } return false; } /** * Add a listener. * * @param bean * @param listener * @param propertyName */ public static void addPCL(Object bean, PropertyChangeListener listener, String propertyName) { try { if (propertyName == null) { bean.getClass().getMethod(pclAddMethodName, NO_ARGS) .invoke(bean, new Object[] { listener }); } else { bean.getClass() .getMethod(pclAddMethodName, ARGS) .invoke(bean, new Object[] { propertyName, listener }); } } catch (Exception e) { e.printStackTrace(); } } /** * Remove a listener. * * @param bean * @param listener * @param propertyName */ public static void removePCL(Object bean, PropertyChangeListener listener, String propertyName) { try { if (propertyName == null) { bean.getClass().getMethod(pclRemoveMethodName, NO_ARGS) .invoke(bean, new Object[] { listener }); } else { bean.getClass() .getMethod(pclRemoveMethodName, ARGS) .invoke(bean, new Object[] { propertyName, listener }); } } catch (Exception e) { e.printStackTrace(); } } }