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

import java.awt.Font;
import java.awt.FontFormatException;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import com.meldcraft.application.ApplicationContext;
import com.meldcraft.application.ApplicationElementContext;
import com.meldcraft.application.ManagedElementFactory;
import com.meldcraft.application.util.BeanContext;
import com.meldcraft.application.util.BeanUtil;
import com.meldcraft.util.PropertiesHelper;

/**
 * @version $Revision: 1.6 $ $Date: 2007/10/03 04:59:32 $
 * @author Rob Richards
 */
public class SSSupport
{
    protected SSSupport()
    {
    }

    public static Object createInstance(ApplicationElementContext elementContext)
    {
        Object ret = null;

        ApplicationContext context = elementContext.getApplicationContext();
        String elementId = elementContext.getElementId();
        Map config = PropertiesHelper.getSubset(context.getConfiguration(), elementId + ".");
        String type = PropertiesHelper.getString(config, "type", null);
        if (type != null)
        {
            if (type.equals("font"))
            {
                Font font = null;
                String res = PropertiesHelper.getString(config, "resource", null);
                if (res != null)
                {
                    URL url = context.getApplication().getClass().getResource(res);
                    if (url != null)
                    {
                        int format = res.toLowerCase().endsWith("ttf") ? Font.TRUETYPE_FONT : Font.TYPE1_FONT;
                        try
                        {
                            font = Font.createFont(format, url.openStream());
                        }
                        catch (FontFormatException e)
                        {
                        }
                        catch (IOException e)
                        {
                        }
                    }
                }

                if (font != null)
                {
                    float size = PropertiesHelper.getFloat(config, "size", -1.0f);
                    if (size != -1.0f)
                    {
                        font = font.deriveFont(size);
                    }
                    // TODO style, affine transform, etc.
                }

                ret = font;
            }
        }
        else if ((type = PropertiesHelper.getString(config, "typeClassName", null)) != null)
        {
            HashMap over = new HashMap();
            over.put("className", type);
            ret = ManagedElementFactory.getInstance(context).createManagedElement(elementId, null, over);
        }

        if (ret != null)
        {
            configureProperties(context, elementId, ret);
        }

        return ret;
    }

    protected static void configureProperties(ApplicationContext context, String elementId, Object obj)
    {
        // TODO - is this obsolete per ManagedElementFactory.configureElementProperties()???
        Map config = PropertiesHelper.getSubset(context.getConfiguration(), elementId + ".");
        String[] props = PropertiesHelper.getStringArray(config, "elementProperties", ";");
        if (props != null)
        {
            for (int i=0 ; i < props.length ; i++)
            {
                String[] nameVal = PropertiesHelper.getStringArray(props[i], "=");
                if ((nameVal != null) && (nameVal.length == 2))
                {
                    BeanUtil.setPropertyValue(new BeanContext(elementId, context, obj, nameVal[1]), nameVal[0]);
                }
            }
        }
    }

}



