/*
 * DefaultReceiver.java  $Revision: 1.7 $  $Date: 2007/10/03 04:59:32 $
 *
 * Copyright (C) 2007 Meldcraft Corporation
 *
 * This file is part of Scaffold.
 * 
 * Scaffold is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Scaffold is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with Scaffold.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.meldcraft.application.guis.mvc;

import java.awt.Component;

import com.meldcraft.application.ApplicationContext;
import com.meldcraft.application.ApplicationContextEvent;
import com.meldcraft.application.ApplicationContextListener;
import com.meldcraft.application.util.BeanContext;
import com.meldcraft.application.util.BeanUtil;

/**
 * @version $Revision: 1.7 $ $Date: 2007/10/03 04:59:32 $
 * @author Rob Richards
 */
public class DefaultReceiver implements ApplicationContextListener
{
    private Component   mTarget;
    private String      mProperty;
    private String      mValueProperty;

    public DefaultReceiver(ApplicationContext context, String elementId, String property, String valueProperty, Component comp)
    {
        mTarget = comp;
        mProperty = property;
        mValueProperty = valueProperty;
        context.addApplicationContextListener(this, elementId);
    }

    public void applicationContextChanged(ApplicationContextEvent e)
    {
        Object value = e.getValue();
        if (mValueProperty != null)
        {
            value = BeanUtil.getPropertyValue(value, mValueProperty);
        }
        BeanUtil.setPropertyValue(new BeanContext(e.getApplicationContext(), mTarget, value), mProperty);
    }
}



