/*
 * FontMaker.java  $Revision: 1.2 $  $Date: 2007/08/07 05:18:03 $
 *
 * Copyright (c) Meldcraft, Inc. 2006
 */
package com.meldcraft.mvcexample;

import java.awt.Font;
import java.awt.font.TextAttribute;
import java.util.HashMap;

import com.meldcraft.application.AbstractApplicationElement;

/**
 * @version $Revision: 1.2 $ $Date: 2007/08/07 05:18:03 $
 * @author Rob Richards
 */
public class FontMaker extends AbstractApplicationElement
{
    private String  mFontFamily;
    private String  mFontStyle;
    private int     mFontSize;
    private String  mFontDecoration;
    private String  mFontPosition;
    private boolean mFontKerning;
    private boolean mFontStrikeThrough;

    public void setFontDecoration(String fontDecoration)
    {
        mFontDecoration = fontDecoration;
        makeFont();
    }
    public void setFontFamily(String fontFamily)
    {
        mFontFamily = fontFamily;
        makeFont();
    }
    public void setFontKerning(boolean fontKerning)
    {
        mFontKerning = fontKerning;
        makeFont();
    }
    public void setFontPosition(String fontPosition)
    {
        mFontPosition = fontPosition;
        makeFont();
    }
    public void setFontSize(int fontSize)
    {
        mFontSize = fontSize;
        makeFont();
    }
    public void setFontStrikeThrough(boolean fontStrikeThrough)
    {
        mFontStrikeThrough = fontStrikeThrough;
        makeFont();
    }
    public void setFontStyle(String fontStyle)
    {
        mFontStyle = fontStyle;
        makeFont();
    }

    protected void makeFont()
    {
        HashMap map = new HashMap();

        if (mFontKerning)
        {
            map.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
        }

        if (mFontStrikeThrough)
        {
            map.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
        }

        if (mFontPosition != null)
        {
            String str = this.mFontPosition.toLowerCase();
            if (str.indexOf("supersc") != -1)
            {
                map.put(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER);
            }
            else if (str.indexOf("subsc") != -1)
            {
                map.put(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUB);
            }
        }
        if (mFontDecoration != null)
        {
            String position = mFontDecoration.toLowerCase();
            if (position.indexOf("nounder") == -1)
            {
                if (position.indexOf("thin") != -1)
                {
                    map.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_LOW_ONE_PIXEL);
                }
                else if (position.indexOf("thick") != -1)
                {
                    map.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_LOW_TWO_PIXEL);
                }
                else if (position.indexOf("dash") != -1)
                {
                    map.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_LOW_DASHED);
                }
                else if (position.indexOf("dotted") != -1)
                {
                    map.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_LOW_DOTTED);
                }
                else if (position.indexOf("gray") != -1)
                {
                    map.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_LOW_GRAY);
                }
                else
                {
                    map.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
                }
            }
        }

        int fontStyle = Font.PLAIN;
        if (mFontStyle != null)
        {
            String str = mFontStyle.toLowerCase();
            if (str.indexOf("bold") != -1)
            {
                fontStyle |= Font.BOLD;
            }
            if (str.indexOf("italic") != -1)
            {
                fontStyle |= Font.ITALIC;
            }
        }

        Font font = new Font(mFontFamily, fontStyle, mFontSize);
        font = font.deriveFont(map);

        mApplicationElementContext.getApplicationContext().contextChanged(mApplicationElementContext.getElementId() + ".font", font);
    }
}



