/*
 * ReflectApplicationAction.java  $Revision: 1.6 $  $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.swing.action;

import java.awt.event.ActionEvent;
import java.util.Map;
import java.util.logging.Level;

import com.meldcraft.util.PropertiesHelper;
import com.meldcraft.application.ApplicationContext;
import com.meldcraft.application.ApplicationElementContext;
import com.meldcraft.application.ReflectInvoker;

/**
 * @version $Revision: 1.6 $ $Date: 2007/10/03 04:39:39 $
 * @author Rob Richards
 */
public class ReflectApplicationAction extends AbstractContextCheckingAction
{
//  Static variables
    private static final String TARGET_KEY = "target";

// Member variables
    private boolean mIsToggle = false;
    private ReflectInvoker  mReflectInvoker;

// Constructors and initialization
    public ReflectApplicationAction(ApplicationElementContext context)
    {
        super(context);

        Object[] info = ActionGUIFactory.getToggleActionInfo(context.getConfiguration());
        mIsToggle = (info != null);

        mReflectInvoker = ReflectInvoker.createInstance(context);
    }

// Interface implemenations
    public void actionPerformed(ActionEvent e)
    {
        Map config = getApplicationElementContext().getConfiguration();

        String method = PropertiesHelper.getString(config, "method", null);

        Object[] args = null;
        String arg = PropertiesHelper.getString(config, "method.arg", null);
        if (arg != null)
        {
            args = new String[] {arg};
        }
        else
        {
            args = PropertiesHelper.getEnumeratedStrings(config, "method.arg");
            if ((args == null) && mIsToggle)
            {
                Object src = e.getSource();
                if (src instanceof javax.swing.AbstractButton)
                {
                    args = new String[] {""+((javax.swing.AbstractButton)src).isSelected()};
                }
            }
        }

        ApplicationContext appContext = getApplicationElementContext().getApplicationContext();
        try
        {
            Object target = null;

            Object key = config.get(TARGET_KEY);
            if (key != null)
            {
                target = appContext.getElementManager().getApplicationElement(String.valueOf(key));
            }

            if (target == null)
            {
                key = appContext.getConfiguration().get(TARGET_KEY);
                if (key != null)
                {
                    target = appContext.getElementManager().getApplicationElement(String.valueOf(key));
                }

                if (target == null)
                {
                    target = appContext.getApplication();
                }
            }

            if ((args != null) && (args instanceof String[]))
            {
                Object[] ar = new Object[args.length];
                System.arraycopy(args,0, ar,0,ar.length);
                args = ar;
            }
            mReflectInvoker.invokeMethod(target, method, args, args);
        }
        catch (Throwable ex)
        {
            appContext.getLogger().log(Level.WARNING, "Error calling method: "+method, ex);
        }
    }

// Public methods
    public String toString()
    {
        String str = super.toString() + " isToggle=" + mIsToggle;
        return str;
    }

// Protected methods
// Private methods
// Inner classes
}
