/*
 * ItemMediator.java  $Revision: 1.5 $  $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 java.awt.event.ItemEvent;
import java.lang.reflect.InvocationTargetException;

import com.meldcraft.application.ApplicationContext;
import com.meldcraft.application.ApplicationContextEvent;
import com.meldcraft.application.ApplicationContextListener;
import com.meldcraft.gui.swing.SwingUtil;
import com.meldcraft.util.ReflectUtil;

/**
 * @version $Revision: 1.5 $ $Date: 2007/10/03 04:59:32 $
 * @author Rob Richards
 */
public class ItemMediator extends ItemEmitter implements ApplicationContextListener
{
    private static ThreadLocal  sLocal = new ThreadLocal();
    private Component   mTarget;

    public ItemMediator(ApplicationContext context, String elementId, Component comp)
    {
        super(context, elementId, comp);
        mTarget = comp;
        context.addApplicationContextListener(this, elementId + ".selectValue");
    }

    @Override
    public void itemStateChanged(ItemEvent e)
    {
        if (sLocal.get() == null)
        {
            super.itemStateChanged(e);
        }
    }

    public void applicationContextChanged(final ApplicationContextEvent e)
    {
        Runnable run = new Runnable() {
            public void run()
            {
                sLocal.set(new Object());
                try
                {
                    Object curVal = ReflectUtil.invokeMethod(mTarget, "getSelectedItem");
                    Object newVal = e.getValue();
                    if (curVal != newVal)
                    {
                        if ((curVal == null) ||
                            (newVal == null) ||
                            !curVal.equals(newVal))
                        {
                            Object[] val = new Object[]{newVal};
                            ReflectUtil.invokeMethod(mTarget, "setSelectedItem", val);
                        }
                    }
                }
                catch (NoSuchMethodException e1)
                {
                }
                catch (IllegalAccessException e1)
                {
                }
                catch (InvocationTargetException e1)
                {
                }
                finally
                {
                    sLocal.set(null);
                }
            }
        };
        SwingUtil.waitForInvokeLater(run);
    }
}



