001    package org.springframework.binding.form.support;
002    
003    import org.apache.commons.beanutils.BeanUtilsBean;
004    import org.apache.commons.beanutils.ConvertUtilsBean;
005    import org.apache.commons.beanutils.PropertyUtilsBean;
006    import org.apache.commons.beanutils.Converter;
007    import org.apache.commons.lang.time.DateFormatUtils;
008    
009    import javax.swing.*;
010    import java.beans.PropertyChangeListener;
011    import java.beans.PropertyChangeEvent;
012    import java.util.Date;
013    
014    /**
015     * Listener to register on a property of a bean which has a nested property shown in the JTextField. If the
016     * listener is registered on property X of a bean and the nestedProperty Y of X is delivered with the
017     * constructor, the given textfield will be updated when X changes.
018     *
019     * @author Jan Hoskens
020     *
021     */
022    public class NestedPropertyChangeListener implements PropertyChangeListener
023    {
024    
025        private JTextField nestedField;
026        private String nestedProperty;
027        private static BeanUtilsBean beanUtilsBean;
028    
029        static
030        {
031            ConvertUtilsBean convertUtilsBean = new ConvertUtilsBean();
032            convertUtilsBean.register(new StringConverter(), String.class);
033            beanUtilsBean = new BeanUtilsBean(convertUtilsBean, new PropertyUtilsBean());
034        }
035    
036        public static final class StringConverter implements Converter
037        {
038    
039            public Object convert(Class type, Object value)
040            {
041    
042                if (value == null)
043                {
044                    return ((String) null);
045                }
046                else if (value instanceof Date)
047                {
048                    return DateFormatUtils.format((Date)value, "dd/MM/yyyy");
049                }
050                return (value.toString());
051            }
052    
053        }
054    
055        public NestedPropertyChangeListener(JTextField nestedPropertyField, String nestedProperty)
056        {
057            this.nestedField = nestedPropertyField;
058            this.nestedProperty = nestedProperty;
059        }
060    
061        public void propertyChange(PropertyChangeEvent evt)
062        {
063            Object newValue = evt.getNewValue();
064            if (newValue == null)
065            {
066                nestedField.setText("");
067            }
068            else
069            {
070                String nestedValue;
071                try
072                {
073                    nestedValue = beanUtilsBean.getProperty(newValue, nestedProperty);
074                }
075                catch (Exception ex)
076                {
077                    nestedValue = "";
078                };
079                nestedField.setText(nestedValue);
080                nestedField.setToolTipText(nestedValue);
081            }
082            nestedField.select(0, 0); // move cursor to front
083        }
084    }