/*
 * SwingApplicationContext.java  $Revision: 1.8 $  $Date: 2007/10/03 04:39:40 $
 *
 * 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;

import java.awt.Component;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;

import com.meldcraft.application.Application;
import com.meldcraft.application.DefaultApplicationContext;
import com.meldcraft.application.ManagedElementFactory;
import com.meldcraft.application.swing.action.ActionManager;
import com.meldcraft.application.swing.action.DefaultActionManager;
import com.meldcraft.util.ConfigurationManager;
import com.meldcraft.util.PropertiesHelper;

/**
 * @version $Revision: 1.8 $ $Date: 2007/10/03 04:39:40 $
 * @author Rob Richards
 */
public class SwingApplicationContext extends DefaultApplicationContext
{
    // Static variables
    private static final WeakHashMap    sContextMap = new WeakHashMap();

// Member variables
    private Component       mUIRoot;
    private ActionManager   mActionManager;

// Constructors and initialization
    public SwingApplicationContext(Application app, Map config)
    {
        super(app, config);
        mActionManager = createActionManager();

        Map def = new HashMap();
        def.put("className", "javax.swing.JFrame");

        String title = PropertiesHelper.getString(config, ConfigurationManager.CONFIGURATION_BASE_NAME, app.getClass().getName());
        title = title.substring(title.lastIndexOf(".")+1, title.length());
        title = PropertiesHelper.getString(config, SwingApplicationConstants.APP_NAME_KEY, title);
        Map map = PropertiesHelper.getMap(config, "Application.UIRoot.elementProperties");
        if (map != null)
        {
            String str = PropertiesHelper.getString(config, "Application.UIRoot.elementProperty.title", null);
            if (str == null)
            {
                str = PropertiesHelper.getString(map, "title", null);
            }
            if (str == null)
            {
                def.put("elementProperty.title", title);
            }
        }
        else
        {
            def.put("elementProperties", "title="+title);
        }

        mUIRoot = (Component)ManagedElementFactory.getInstance(this).createManagedElement("Application.UIRoot", def, null);
        setSwingApplicationContext(mUIRoot, this);
    }

// Interface implemenations
// Public methods
    /**
     * Returns the SwingApplicationContext from the nearest ancestor of comp.
     */
    public static SwingApplicationContext fromAncestor(Component comp)
    {
        SwingApplicationContext ret = null;
        while (comp != null)
        {
            ret = (SwingApplicationContext)sContextMap.get(comp);
            if (ret != null)
            {
                break;
            }
            comp = comp.getParent();
        }
        return ret;
    }

    public ActionManager getActionManager()
    {
        return mActionManager;
    }

    public Component getUIRoot()
    {
        return mUIRoot;
    }

// Protected methods
    protected ActionManager createActionManager()
    {
        return new DefaultActionManager(this);
    }

    protected static void setSwingApplicationContext(Component ui, SwingApplicationContext context)
    {
        sContextMap.put(ui, context);
    }

    protected static SwingApplicationContext removeSwingApplicationContext(Component ui)
    {
        return (SwingApplicationContext)sContextMap.remove(ui);
    }

// Private methods
// Inner classes
}
