/*
 * SelectionMediator.java  $Revision: 1.4 $  $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.awt.Component;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;

import javax.swing.event.ListSelectionEvent;

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.4 $ $Date: 2007/10/03 04:39:39 $
 * @author Rob Richards
 */
public class SelectionMediator extends SelectionEmitter implements ApplicationContextListener
{
    private static ThreadLocal  sLocal = new ThreadLocal();
    private Component   mTarget;

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

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

    public void applicationContextChanged(final ApplicationContextEvent e)
    {
        Runnable run = new Runnable() {
            public void run()
            {
                sLocal.set(new Object());
                try
                {
                    String str = String.valueOf(e.getKey()).toLowerCase();

                    if (str.endsWith(".selectindex"))
                    {
                        int[] newIndicies = (int[])e.getValue();
                        int[] curIndicies = (int[])ReflectUtil.invokeMethod(mTarget, "getSelectedIndices");

                        if (!Arrays.equals(newIndicies, curIndicies))
                        {
                            ReflectUtil.invokeMethod(mTarget, "setSelectedIndices", newIndicies);
                        }
                    }
                    else if (str.endsWith(".selectvalue"))
                    {
                        Object value = e.getValue();
                        ReflectUtil.invokeMethod(mTarget, "setSelectedValue", new Object[] {value, Boolean.TRUE});
                    }
                }
                catch (NoSuchMethodException e2)
                {
                }
                catch (IllegalAccessException e2)
                {
                }
                catch (InvocationTargetException e2)
                {
                }
                finally
                {
                    sLocal.set(null);
                }
            }
        };
        SwingUtil.waitForInvokeLater(run);
    }
}



