Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package org.uqbar.commons.model;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import org.uqbar.commons.utils.Observable;
import org.uqbar.commons.utils.ReflectionUtils;
/**
* Utility class for {@link Observable} object functions, for example to force
* triggering a property change method.
*
* @author jfernandes
* @author flbulgarelli
*/
public class ObservableUtils {
/**
* Set ups one or more dependencies for the given property in
* the given model. That is, the property property
* will fire a propertyChanged event whenever any of the
* dependencies get updated.
*
* @since 3.4
* @param model
* the model with the given properties
* @param property
* the property to which dependencies will be added
* @param dependencies
* the the dependencies to be added to the given property
*/
public static void dependencyOf(final Object model, final String property, String... dependencies) {
addPropertyListener(model, new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
firePropertyChanged(model, property);
}
}, dependencies);
}
public static void firePropertyChanged(Object model, String propertyName, Object newValue) {
Method firePropertyChangedMethod = ReflectionUtils.findMethod(model.getClass(), "firePropertyChange", true, new Class[] { String.class, Object.class, Object.class });
if(firePropertyChangedMethod != null){
ReflectionUtils.invoke(model, firePropertyChangedMethod, propertyName, null, newValue);
}
}
public static void firePropertyChanged(Object model, String property) {
firePropertyChanged(model, property, ReflectionUtils.invokeGetter(model, property));
}
public static void addPropertyListener(Object model, PropertyChangeListener listener, String...properties) {
addPropertyListener(model, listener, Arrays.asList(properties));
}
public static void addPropertyListener(Object model, PropertyChangeListener listener, List properties) {
for (String property: properties) {
ReflectionUtils.invokeMethod(model, "addPropertyChangeListener", new Object[]{property, listener});
}
}
}