Author: tchemit Date: 2013-07-24 12:20:14 +0200 (Wed, 24 Jul 2013) New Revision: 2598 Url: http://nuiton.org/projects/nuiton-utils/repository/revisions/2598 Log: fixes #2795: Can change the BeanMonitor properties to watch Modified: trunk/src/main/java/org/nuiton/util/beans/BeanMonitor.java Modified: trunk/src/main/java/org/nuiton/util/beans/BeanMonitor.java =================================================================== --- trunk/src/main/java/org/nuiton/util/beans/BeanMonitor.java 2013-07-23 16:54:05 UTC (rev 2597) +++ trunk/src/main/java/org/nuiton/util/beans/BeanMonitor.java 2013-07-24 10:20:14 UTC (rev 2598) @@ -30,6 +30,7 @@ import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.LinkedHashMap; @@ -38,8 +39,6 @@ import java.util.Set; import java.util.TreeMap; -import static org.nuiton.i18n.I18n._; - /** * A monitor of beans. * <p/> @@ -87,7 +86,7 @@ * @param propertyNames the names of properties to monitor */ public BeanMonitor(String... propertyNames) { - this.propertyNames = Arrays.asList(propertyNames); + this.propertyNames = new ArrayList<String>(Arrays.asList(propertyNames)); propertyDiffs = new LinkedHashMap<String, PropertyDiff>(); listener = new PropertyChangeListener() { @@ -139,32 +138,6 @@ } } - -// if (modifiedProperties.contains(propertyName)) { -// -// // property already modified -// // check if value did not come back to original -// Object originalValue = originalValues.get(propertyName); -// -// if (originalValue == null && newValue == null || -// originalValue != null && originalValue.equals(newValue)) { -// -// // coming back to original value -// // the property is no more modified -// modifiedProperties.remove(propertyName); -// originalValues.remove(propertyName); -// return; -// } -// -// // can safely return since original is already known -// // and property is already known to be modified -// return; -// } -// -// // the property was not modified before -// // just mark it and keep the original value -// modifiedProperties.add(propertyName); -// originalValues.put(propertyName, oldValue); } }; } @@ -253,8 +226,8 @@ try { BeanUtil.removePropertyChangeListener(listener, oldBean); } catch (Exception eee) { - log.error(_("nuitonutil.error.could.not.removePCL", - listener, oldBean, eee.getMessage())); + log.error(String.format("Could remove the PropertychangeListener %1$s from object %2$s for following reason \\: %3$s", + listener, oldBean, eee.getMessage())); } } if (bean != null) { @@ -263,8 +236,8 @@ try { BeanUtil.addPropertyChangeListener(listener, bean); } catch (Exception eee) { - log.error(_("nuitonutil.error.could.not.addPCL", - listener, bean, eee.getMessage())); + log.error(String.format("Could not add the PropertychangeListener %1$s on object %2$s for following reason \\: %3$s", + listener, bean, eee.getMessage())); } } } @@ -272,7 +245,21 @@ /** To clear the modified states. */ public void clearModified() { propertyDiffs.clear(); -// modifiedProperties.clear(); -// originalValues.clear(); } + + /** + * To change the list of properties to watch. + * <p/> + * <strong>Note:</strong> As a side-effect, we call a + * {@link #clearModified()} method after having changed the list of + * properties to watch. + * + * @param properties the list of properties to watch on the bean. + * @since 3.0 + */ + public void setProperties(String... properties) { + propertyNames.clear(); + propertyNames.addAll(Arrays.asList(properties)); + clearModified(); + } }