/*
 * AbstractContextCheckingAction.java  $Revision: 1.8 $  $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.util.Map;

import com.meldcraft.application.ApplicationContextEvent;
import com.meldcraft.application.ApplicationContextListener;
import com.meldcraft.application.ApplicationElementContext;
import com.meldcraft.gui.swing.SwingUtil;
import com.meldcraft.util.PropertiesHelper;

/**
 * @version $Revision: 1.8 $ $Date: 2007/10/03 04:39:39 $
 * @author rob
 */
abstract public class AbstractContextCheckingAction extends AbstractApplicationAction
    implements ApplicationContextListener
{
// Static variables
    public static final String  CONTEXT_CHECKING_ACTION_KEY = "ContextCheckingAction.key";

// Member variables
    private boolean     mEnabled = true;
    private Enabler     mEnabler;

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

        // TODO: listen for something, but not everything
        //context.addApplicationContextListener(this);

        mEnabler = new Enabler(this);

        Object key = getContextKey();
        if (key != null)
        {
            context.getApplicationContext().addApplicationContextListener(this, key);
        }
    }

// Interface implemenations
    // ApplicationListener interface
    public void applicationContextChanged(ApplicationContextEvent e)
    {
        mEnabled = canEnable(e);

        if (mEnabled != isEnabled())
        {
            SwingUtil.waitForInvokeLater(mEnabler);
        }
    }

// Public methods
    public String toString()
    {
        String str = getClass().getName() + " config=" + getApplicationElementContext().getConfiguration();
        return str;
    }

// Protected methods
    protected boolean canEnable(ApplicationContextEvent e)
    {
        boolean canEnable = true;

        Object key = getContextKey();
        Object ekey = e.getKey();
        if ((key != null) && (ekey != null) && (key.equals(ekey)))
        {
            canEnable = canEnableForValue(key, e.getValue());
        }

        return canEnable;
    }

    protected boolean canEnableForValue(Object key, Object value)
    {
        boolean ret = (value != null);
        if (ret)
        {
            if (value instanceof String)
            {
                String str = ((String)value).toLowerCase();
                ret = !str.equals("") && !str.equals("null") && !str.equals("false") && !str.equals("<null>"); 
            }
        }
        return ret;
    }

// Private methods
    private Object getContextKey()
    {
        Object key = null;

        Map config = getApplicationElementContext().getConfiguration();
        if (config != null)
        {
            key = PropertiesHelper.getString(config, CONTEXT_CHECKING_ACTION_KEY, getApplicationElementContext().getElementId() + ".enabled");
        }

        return key;
    }

// Inner classes
    public static class Enabler implements Runnable
    {
        private AbstractContextCheckingAction   mAction;

        public Enabler(AbstractContextCheckingAction action)
        {
            mAction = action;
        }

        public void run()
        {
            mAction.setEnabled(mAction.mEnabled);
        }
    }
}
