/*
 * XmlFont.java
 *
 * Created on June 8, 2006, 10:54 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package usda.weru.util.table;

import java.awt.Font;
import org.jdom2.Element;

/**
 * A wrapper object for a Font specified in a jdom Element.
 * @author Joseph Levin
 */
public class XmlFont{    
    
    
    /**
     * Load configuration settings for this Font from a jdom Element.  Uses "SanSerif" as a default font name if one is not specified, Font.Plain as a default font style if one is not specified, and 10 as a default font size if one is not specified.
     * @param node An org.jdom2.Element containing configuration information about this Font.
     * @return The Font created by the information in the org.jdom2.Element supplied.
     */
    public static Font fromXml(Element node){
        //Get the values
        String name = "SansSerif";
        int style = Font.PLAIN;
        int size = 10;        
        String fontName = node.getValue();       
        try {
            int fontSize= Integer.parseInt(node.getAttributeValue(WepsTableEnum.XML_size));      
            size = fontSize;
        } 
        catch (NumberFormatException ex) {}        
        boolean fontBold = Helper.isTrue(node.getAttributeValue(WepsTableEnum.XML_bold));        
        boolean fontItalic = Helper.isTrue(node.getAttributeValue(WepsTableEnum.XML_italic));
        
        //Set the values 
        if (fontName != null) name = fontName;
        if (fontBold) style |= Font.BOLD;
        if (fontItalic) style |= Font.ITALIC;
        
        Font font = new Font(name, style, size);
        return font;
    }
    
    /*
    public Element toXml(){
        Element node = new Element(XML_font);
        node.setText(name);
        node.setAttribute(XML_size, Integer.toString(size));
        
        if ((style & Font.BOLD) == Font.BOLD){
            node.setAttribute(XML_bold, "true");    
        }
        
        if ((style & Font.ITALIC) == Font.ITALIC){
            node.setAttribute(XML_italic, "true");    
        }
        
        return node;
    }
     */
    
}
