/*
 * SelectionEmitter.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.lang.reflect.InvocationTargetException;

import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.meldcraft.application.ApplicationContext;
import com.meldcraft.application.util.BeanUtil;
import com.meldcraft.util.PropertiesHelper;
import com.meldcraft.util.ReflectUtil;

/**
 * @version $Revision: 1.5 $ $Date: 2007/10/03 04:59:32 $
 * @author Rob Richards
 */
public class SelectionEmitter implements ListSelectionListener
{
    private ApplicationContext  mContext;
    private String              mElementId;
    private String              mContextKey;
    private Component           mTarget;
    private boolean             mFlattenValueArray;

    public SelectionEmitter(ApplicationContext context, String elementId, String contextKey, Component comp)
    {
        mContext = context;
        mElementId = elementId;
        mContextKey = contextKey;
        mTarget = comp;
        mFlattenValueArray = PropertiesHelper.getBoolean(context.getConfiguration(), elementId + ".flattenValueArray", false);

        try
        {
            ReflectUtil.invokeMethod(comp, "addListSelectionListener", this);
        }
        catch (NoSuchMethodException e)
        {
            try
            {
                Object obj = BeanUtil.getPropertyValue(comp, "selectionModel");
                if (obj != null)
                {
                    ReflectUtil.invokeMethod(obj, "addListSelectionListener", this);
                }
            }
            catch (NoSuchMethodException e2)
            {
                throw new RuntimeException("Could not addListSelectionListener", e);
            }
            catch (IllegalAccessException e2)
            {
                throw new RuntimeException("Could not addListSelectionListener", e2);
            }
            catch (InvocationTargetException e2)
            {
                throw new RuntimeException("Could not addListSelectionListener", e2);
            }
        }
        catch (IllegalAccessException e)
        {
            throw new RuntimeException("Could not addListSelectionListener", e);
        }
        catch (InvocationTargetException e)
        {
            throw new RuntimeException("Could not addListSelectionListener", e);
        }
    }

    public void valueChanged(ListSelectionEvent e)
    {
        if (!e.getValueIsAdjusting())
        {
            try
            {
                Object items = ReflectUtil.invokeMethod(mTarget, "getSelectedValues");
                Object indices = ReflectUtil.invokeMethod(mTarget, "getSelectedIndices");
                if (mFlattenValueArray && (items != null))
                {
                    Object[] arr = (Object[])items;
                    if (arr.length == 0)
                    {
                        items = null;
                    }
                    else if (arr.length == 1)
                    {
                        items = arr[0];
                    }
                }
                /*
                if (items != null)
                {
                    Integer mode = (Integer)ReflectUtil.invokeMethod(mTarget, "getSelectionMode");
                    if (mode.intValue() == ListSelectionModel.SINGLE_SELECTION)
                    {
                        if (((Object[])items).length > 0)
                        {
                            items = ((Object[])items)[0];
                        }
                        else
                        {
                            items = null;
                        }
                    }
                }
                */

                mContext.contextChanged(mContextKey + ".selectIndex", indices);
                mContext.contextChanged(mContextKey + ".selectValue", items);
            }
            catch (NoSuchMethodException e1)
            {
                mContext.contextChanged(mContextKey + ".selectTarget", mTarget);
            }
            catch (IllegalAccessException e1)
            {
            }
            catch (InvocationTargetException e1)
            {
            } 
        }
    }
}



