/*
 * MVCSupport.java  $Revision: 1.5 $  $Date: 2007/10/03 04:39:39 $
 *
 * 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.util.Map;
import java.util.logging.Level;

import com.meldcraft.application.ApplicationContext;
import com.meldcraft.application.ApplicationContextEvent;
import com.meldcraft.application.util.BeanContext;
import com.meldcraft.application.util.BeanUtil;
import com.meldcraft.gui.swing.SwingUtil;
import com.meldcraft.util.PropertiesHelper;

/**
 * @version $Revision: 1.5 $ $Date: 2007/10/03 04:39:39 $
 * @author Rob Richards
 */
public class MVCSupport
{
    private static boolean    sSyncing = false;

    private String  mEmitValue;
    private String  mReceiveValue;

    public MVCSupport(ApplicationContext context, String elementId)
    {
        Map config = context.getConfiguration();
        config = PropertiesHelper.getSubset(config, elementId + ".");
        mEmitValue = PropertiesHelper.getString(config, "emitApplicationContext.value",
                        PropertiesHelper.getString(config, "mediateApplicationContext.value", null));
        mReceiveValue = PropertiesHelper.getString(config, "receiveApplicationContext.value",
                            PropertiesHelper.getString(config, "mediateApplicationContext.value", null));
    }

    public void applicationContextChanged(final ApplicationContextEvent e)
    {
        SwingUtil.waitForInvokeLater(new Runnable(){
            public void run()
            {
                try
                {
                    sSyncing = true;
                    handleApplicationContextChanged(e);
                }
                finally
                {
                    sSyncing = false;
                }
            }
        });
    }

    protected void handleApplicationContextChanged(ApplicationContextEvent e)
    {
        throw new RuntimeException("handleApplicationContextChanged not implemented");
    }

    protected Object getEmitValue(ApplicationContext context, Object value)
    {
        Object ret = value;

        if (mEmitValue != null)
        {
            try
            {
                ret = BeanUtil.translateValue(new BeanContext(context, value, mEmitValue));
            }
            catch (Throwable e)
            {
                handleGetValueError(context, e);
            }
        }

        return ret;
    }

    protected Object getReceiveValue(ApplicationContext context, Object value)
    {
        Object ret = value;

        if (mReceiveValue != null)
        {
            try
            {
                ret = BeanUtil.translateValue(new BeanContext(context, value, mReceiveValue));
            }
            catch (Throwable e)
            {
                handleGetValueError(context, e);
            }
        }

        return ret;
    }

    protected void handleGetValueError(ApplicationContext context, Throwable error)
    {
        context.getLogger().log(Level.WARNING, "Error translating value", error);
    }

    protected void emitContextChanged(ApplicationContext context, String key, Object value)
    {
        if (!sSyncing)
        {
            context.contextChanged(key, value);
        }
    }
}
