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

import java.awt.event.ActionEvent;
import java.util.HashMap;

import javax.swing.AbstractButton;
import javax.swing.Action;

import com.meldcraft.application.ApplicationContextListener;
import com.meldcraft.application.ApplicationElementContext;
import com.meldcraft.application.ManagedElementFactory;
import com.meldcraft.application.swing.SwingApplicationContextChangedInvoker;
import com.meldcraft.util.StringUtil;

/**
 * @version $Revision: 1.8 $ $Date: 2007/10/03 04:59:32 $
 * @author Rob Richards
 */
public class DefaultApplicationAction extends AbstractContextCheckingAction
{
//  Static variables
// Member variables
    private boolean     mIsToggle = false;

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

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

        Object method = context.getConfiguration().get("method");
        if ((method != null) &&
            (!(method instanceof String) || !StringUtil.isEmpty((String)method)))
        {
            HashMap defaults = new HashMap();
            defaults.put("className", SwingApplicationContextChangedInvoker.class.getName());
            String elementId = context.getElementId();
            ApplicationContextListener listener = (ApplicationContextListener)ManagedElementFactory.getInstance(context.getApplicationContext()).createManagedElement(elementId, defaults, null);
            context.getApplicationContext().addApplicationContextListener(listener, getContextKey(elementId) + ".action");
            context.getApplicationContext().addApplicationContextListener(listener, getContextKey(elementId) + ".selectValue");
        }
    }

// Interface implemenations
    public void actionPerformed(ActionEvent e)
    {
        Object value = this.getValue(Action.ACTION_COMMAND_KEY);
        String suffix = ".action";
        if (mIsToggle)
        {
            Object src = e.getSource();
            if (src instanceof AbstractButton)
            {
               value = String.valueOf(((AbstractButton)src).isSelected());
               suffix = ".selectValue";
            }
        }
        getApplicationElementContext().getApplicationContext().contextChanged(getContextKey(getApplicationElementContext().getElementId()) + suffix, value);
    }

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

// Protected methods
    protected String getContextKey(String elementId)
    {
        return getApplicationElementContext().getElementId();
    }

// Private methods
// Inner classes
}
